将图像添加到由 knitr 生成的 flextable 失败但在 RMarkdown 块中有效
Adding image to flextable to be generated with knitr fails but works in RMarkdown chunk
运行 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 headers。我正在使用 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)
```
我在这个问题上找不到太多信息,如有任何帮助,我们将不胜感激。
我无法重现您的错误。在生成您的 table 的代码下方。有两种解决方案,我推荐的解决方案(记录在此处: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)
```
运行 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 headers。我正在使用 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)
```
我在这个问题上找不到太多信息,如有任何帮助,我们将不胜感激。
我无法重现您的错误。在生成您的 table 的代码下方。有两种解决方案,我推荐的解决方案(记录在此处: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)
```