首先,我在Windows10上使用的是R 3.6.0和Rstudio 1.2。
我正在使用flextable和Officer创建Word文档。我在这个表中插入了一些图像。为了做到这一点,我使用了flextable。当我将这段代码与R脚本和officer一起使用时,它是有效的。但是,当我在Rmarkdown中使用这段代码来生成Word文档时,它不起作用。Rmardown下的代码:
library(flextable)
library(officer)
img.file <- file.path( R.home("doc"), "html", "logo.jpg" )
myft <- flextable( head(iris))
myft <- compose( myft, i = 1:3, j = 1,
value = as_paragraph(
as_image(src = img.file, width = .20, height = .15),
" blah blah ",
as_chunk(Sepal.Length, props = fp_text(color = "red"))
),
part = "body")
myft我有一条消息告诉我:“很抱歉,我们无法打开文档,因为我们发现它的内容有问题。
我认为flextable中的图像有问题。当我删除这些图像时,它就起作用了。
发布于 2019-06-19 19:13:21
是的,rmarkdown::word_document不支持在flextable中插入图像。
您将需要包officedown能够嵌入图像与R Markdown为Word的flextable。您只需要用output: officedown::rdocx_document替换output: rmarkdown::word_document即可。
---
date: "`r Sys.Date()`"
author: "Your Name"
title: "Untitled"
output:
officedown::rdocx_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.cap = TRUE)
library(officedown)
```
```{r}
library(flextable)
library(officer)
img.file <- file.path( R.home("doc"), "html", "logo.jpg" )
myft <- flextable( head(iris))
myft <- compose( myft, i = 1:3, j = 1,
value = as_paragraph(
as_image(src = img.file, width = .20, height = .15),
" blah blah ",
as_chunk(Sepal.Length, props = fp_text(color = "red"))
),
part = "body")
autofit(myft)
```要安装该软件包,请运行以下命令(在CRAN上尚未安装):remotes::install_github("davidgohel/officedown")
https://stackoverflow.com/questions/56664939
复制相似问题