保存到新位置时,图表图像无法出现在带有 RMarkdown 的 Word 文档中

Chart images fail to appear in Word document with RMarkdown when saving to a new location

我正在创建一个复杂的 RMarkdown 文档,其中包含一些在 ggplot2 中制作的图表。我运行以下代码:

rmarkdown::render(input="U:/John/R/aa_app/output/template.Rmd")

这会毫无问题地创建一个名为 "template.docx" 的 Word 文档。

但是,我需要能够为此文档指定不同的文件名和位置。所以我 运行:

rmarkdown::render(input="U:/John/R/aa_app/output/template.Rmd",output_file="U:/John/R/aa_app/output/test.docx")

rmarkdown::render(input="U:/John/R/aa_app/output/template.Rmd",output_file="U:/John/R/aa_app/output/temp/test.docx")

在这两种情况下,都会创建一个名为 "test.docx" 的 Word 文档,但文件中缺少图表。我收到一系列错误消息:

"C:/Program Files/RStudio/bin/pandoc/pandoc" template.utf8.md --to docx --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures
--output U:/John/R/aa_app/output/test.docx --highlight-style tango   
pandoc.exe: Could not find image `U:/John/R/aa_app/output/test_files/figure-docx/unnamed-chunk-3-1.png', skipping...  
pandoc.exe: Could not find image `U:/John/R/aa_app/output/test_files/figure-docx/unnamed-chunk-3-2.png', skipping...  
pandoc.exe: Could not find image `U:/John/R/aa_app/output/test_files/figure-docx/unnamed-chunk-4-1.png', skipping...  
pandoc.exe: Could not find image `U:/John/R/aa_app/output/test_files/figure-docx/unnamed-chunk-7-1.png', skipping...  
pandoc.exe: Could not find image `U:/John/R/aa_app/output/test_files/figure-docx/unnamed-chunk-8-1.png', skipping...  
pandoc.exe: Could not find image `U:/John/R/aa_app/output/test_files/figure-docx/unnamed-chunk-8-2.png', skipping...  
pandoc.exe: Could not find image `U:/John/R/aa_app/output/test_files/figure-docx/unnamed-chunk-8-3.png', skipping...  
pandoc.exe: Could not find image `U:/John/R/aa_app/output/test_files/figure-docx/unnamed-chunk-8-4.png', skipping...

有谁知道出了什么问题吗?

更新:这是一个可重现的例子。

Repro.Rmd的代码:

---
title: "Repro"
author: "John Butters"
date: "Tuesday, April 28, 2015"
output: word_document
---


```{r, echo=FALSE}
plot(cars)
```

如果我 运行 以下代码,将生成一个名为 "Repro.docx" 的文档,其中包含 "cars" 数据集的图表。

rmarkdown::render(input="U:/John/R/Repro.Rmd")

但是,如果我 运行 下面的代码,会生成一个名为 "aaargh.docx" 的文档,其中不包含该图,我会得到与上面相同的 pandoc 错误。

rmarkdown::render(input="U:/John/R/Repro.Rmd",output_file="U:/John/R/aaargh.docx")

"C:/Program Files/RStudio/bin/pandoc/pandoc" Repro.utf8.md --to docx --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output U:/John/R/aaargh.docx --highlight-style tango 
pandoc.exe: Could not find image `U:/John/R/aaargh_files/figure-docx/unnamed-chunk-1-1.png', skipping...

知道了。您不能为 output_file 参数提供完整的文件路径。还有一个 output_dir 参数也不会采用完整的文件路径。

所以,这有效:

rmarkdown::render(input="U:/John/R/Repro.Rmd",output_file="aaargh.docx")

这也有效,只要 "Temp" 是 "R" 的子文件夹即可。

rmarkdown::render(input="U:/John/R/Repro.Rmd",output_file="aaargh.docx",output_dir="Temp")