将图像添加到要使用 knitr 生成的 flextable 失败但在 RMarkdown 块中有效
Posted
技术标签:
【中文标题】将图像添加到要使用 knitr 生成的 flextable 失败但在 RMarkdown 块中有效【英文标题】:Adding image to flextable to be generated with knitr fails but works in RMarkdown chunk 【发布时间】:2021-12-30 15:27:37 【问题描述】:运行 R4.1.2 和 Windows 10:
我正在尝试编织一个文档,该文档具有一个带有通过 ggsave 创建的 ggplot 图像的 flextable。当我在 RMarkdown 中运行代码块时,它可以正常工作,但是当我尝试编写 word 文档时,出现以下错误。如果我不包含图像,knitr 工作正常。
Quitting from lines 350-376 (RPOPS_Draft_Test2.0.Rmd)
Error in read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, :
xmlParseEntityRef: no name [68]
Calls: <Anonymous> ... as_xml_document -> as_xml_document.character -> read_xml.raw
Execution halted
以下是 yaml 标头。我正在使用 officedown,因为我知道这个包需要在 Word 中呈现 flextables 中的图像。
---
title: "something: `r params$program`"
output:
officedown::rdocx_document:
reference_docx: P:/Reference_doc
params:
program: "something"
---
这是导致问题的代码块。
```r overall1_flextable
# chart creation
plot_overall1 <- f_overall_cht(overall_chart1)
plot_overall1_img_out <- ggsave(filename = "plotoverall1img.png", plot = plot_overall1, width = 3.05, height = 0.37, dpi = 600, device = "png")
plot_overall1_in <- file.path(getwd(), "plotoverall1img.png")
example_tibble <- tibble(
col_name = "chart to the right",
chart = ""
)
ft <- flextable(example_tibble)
ft <- compose(ft, i=1, j=2,
value = as_paragraph(
as_image(src = plot_overall1_in, width = 3.05, height = 0.37),
as_chunk(chart)),
part = "body"
)
autofit(ft)
```
我在这个问题上找不到太多信息,因此我们将不胜感激。
【问题讨论】:
【参考方案1】:我无法重现您的错误。在生成表格的代码下方。有两种解决方案,我推荐的解决方案(记录在此:https://ardata-fr.github.io/flextable-book/cell-content-1.html#base-plots-and-ggplot-objects)和更接近您所写的解决方案。
---
title: "test"
output: officedown::rdocx_document
---
```r setup, include=FALSE
library(officedown)
library(officer)
library(flextable)
library(ggplot2)
library(tibble)
# chart creation
plot_overall1 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
geom_point()+
theme_void()
```
## Recommanded
The solution below is corresponding to the recommanded way.
```r
example_tibble <- tibble(
col_name = "chart to the right",
chart = list(plot_overall1)
)
ft <- flextable(example_tibble)
ft <- compose(ft,
j = "chart",
value = as_paragraph(
gg_chunk(value = chart, width = 3.05, height = 0.37, unit = "in")
),
part = "body"
)
autofit(ft)
```
## From scratch
The solution below is corresponding to the way you tried.
```r
png_file <- tempfile(fileext = ".png")
plot_overall1_img_out <- ggsave(
filename = png_file,
plot = plot_overall1,
width = 3.05, height = 0.37,
dpi = 600, device = "png")
example_tibble <- tibble(
col_name = "chart to the right"
)
ft <- flextable(example_tibble,
col_keys = c("col_name", "chart"))
ft <- compose(ft,
i = 1, j = "chart",
value = as_paragraph(
as_image(src = png_file, width = 3.05, height = 0.37, unit = "in")
),
part = "body"
)
autofit(ft)
```
【讨论】:
这太棒了,大卫!非常感谢您的回复和您的工作!我会尝试推荐的方式。以上是关于将图像添加到要使用 knitr 生成的 flextable 失败但在 RMarkdown 块中有效的主要内容,如果未能解决你的问题,请参考以下文章
使用 knitr 和 kable() 生成的表格控制零值的科学记数法和格式
R语言使用knitr生成机器学习模型全流程步骤示例:knitr与自动化结果报告knitr常用参数
如何将相机/照片库中的图像保存到要在 UICollectionView 中显示的数组中