r-markdown - 尝试将 kableExtra::kable 与 as_image() 结合使用以在 Word 中生成表格时,Rmarkdown 出现问题

标签 r-markdown docx kableextra

我正在尝试运行一些代码,希望它能让我在编织到 docx 时使用 kableExtra:

library(kableExtra)

kable(mtcars, "latex", booktabs = T) %>%
kable_styling(latex_options = c("striped", "scale_down")) %>%
row_spec(1, color = "red") %>%
as_image()

(此处借用代码:https://haozhu233.github.io/kableExtra/save_kable_and_as_image.html)

在运行上面的代码之前,我安装了 ghostscript 和这些包(按照说明):

install.packages("magick")
install.packages("webshot")
webshot::install_phantomjs()

当我按下 knit 时,我收到这条消息:

This is XeTeX, Version 3.14159265-2.6-0.999992 (TeX Live 2020/W32TeX) (preloaded format=xelatex)
restricted \write18 enabled.
entering extended mode
Error in save_kable_latex(x, file, latex_header_includes, keep_tex) : We hit an error when trying to 
use magick to read the generated PDF file. You may check your magick installation and try to use 
magick::image_read to read the PDF file manually. It's also possible that you didn't have ghostscript 
installed.

因此,我尝试使用 magick::image_read 手动读取 PDF 文件,这只会导致“R Session Aborted”(magick 中的类似功能:magick::image_read_pdf 工作得很好)。

我该如何解决?

最佳答案

当我运行时

```{r}
library(kableExtra)
library(magick)
library(webshot)
webshot::install_phantomjs()


    
 kable(mtcars, "latex", booktabs = T) %>%
   kable_styling(latex_options = c("striped", "scale_down")) %>%
   row_spec(1, color = "red") %>% 
      save_kable(file = "tests.pdf")
```

我得到了没有 kable 的单词 got,但我得到了 Rmarkdown 文件的文件名的 .PDF。我在尝试复制示例时遇到相同的错误,我想知道是否存在依赖关系中断。您到底想做什么,有什么特别的吗?

关于r-markdown - 尝试将 kableExtra::kable 与 as_image() 结合使用以在 Word 中生成表格时,Rmarkdown 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62819366/

相关文章:

python - 可以使用python-docx在word文档中插入页面吗?

r - 将自定义函数传递给 RMarkdown YAML df_print 以在编织 HTML 输出时打印数据帧

r - 在 rmarkdown pdf 输出中包装比例表的列名称的有效方法

html - 如何在 RMarkdown 中将 float 图像(环绕文本)与居中标题对齐到右侧?

r - R Markdown flex_dashboard 中的 Fig.width 和 Fig.height

java - Docx 到 Pdf 并替换字符

pdf - 编织 PDF 时如何更改 kableExtra::spec_pointrange 的线条颜色

latex - Rmarkdown/Bookdown : Separate figure numbering for Supplemental Section

pdf - rmarkdown中pdf和word的分页符

perl - 使用 Perl(模块)创建 docx(Word)文档