繁体   English   中英

使用Rmarkdown将图像添加到flextable并在Word文档中生成我的flextable

[英]Adding image to flextable and generate my flextable in Word document using Rmarkdown

首先,我在Windows 10中使用R 3.6.0和Rstudio 1.2。

我正在使用flextable和Officer来创​​建Word文档。 在这张表中我插入了一些图片。 要做到这一点,我使用flextable。 当我使用这个代码与R脚本和官员工作。 但是,当我在Rmarkdown中使用此代码生成Word文档时,这不起作用。 代码Under 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中的图像存在问题。 当我删除这些图像时,那工作。

是的, rmarkdown::word_document不支持在flextable中插入图像。

您需要使用package officedown才能使用R Markdown for Word将图像嵌入到flextable中。 您只需要output: rmarkdown::word_document by output: officedown::rdocx_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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM