是否可以为 R Markdown 设置多组共享选项?
这是我的问题:我有一个包含一堆 Markdown 文件的文件夹。这些文件可以分为两组:
html_document
和revealjs::revealjs_presentation
。
我想从这些组中提取出通用的 YAML 代码。现在我知道我可以创建一个 _output.yaml
文件来捕获常见的 YAML,但我基本上需要有这些文件中的两个,每个输出格式一个.
看到pandoc_args
的使用提示here我试了一下,如下所示:
---
title: Document Type 1
output:
html_document:
pandoc_args: './common-html.yaml'
---
和
---
title: Document Type 2
output:
revealjs::revealjs_presentation:
pandoc_args: './common-reveal.yaml'
---
但是使用此设置不会处理包含的 YAML 文件中的选项。
如有任何其他建议,我们将不胜感激!
最佳答案
您可以在同一个 _output.yaml
文件中指定多个输出格式,如下所示(只是一些示例选项):
html_document:
self_contained: false
revealjs::revealjs_presentation:
incremental: true
然后你必须render all output formats这不能直接使用 RStudio GUI 完成。相反,您必须在 R 控制台中输入以下内容:
rmarkdown::render(input = "your.Rmd",
output_format = "all")
理想情况下,确保 .Rmd
文档本身的 YAML 前端内容中没有 output
键。否则 _output.yaml
文件中的输出选项可能会被覆盖。不幸的是,我找不到关于确切行为的全面文档。到目前为止我的一些观察:
- 在
.Rmd
文档本身的 YAML 前端定义的输出选项总是 override those specified in the shared options file_output.yaml
. - 在
.Rmd
文档本身的 YAML 前端使用默认选项集(如pdf_document: default
)指定输出格式完全覆盖_output.yaml
。但是,如果您没有明确指定默认选项(如output: pdf_document
; 一次只能用于单一输出格式),则_output.yaml
内容为受到充分重视。 - 如果您在
_output.yaml
中指定了多种输出格式的选项,在 RStudio 中按下编织按钮时只会呈现第一个(即使您明确按下编织成 HTML/PDF/Word
)。您还必须使用rmarkdown::render(output_format = "all")
来呈现其他格式。
关于r - R Markdown 中的多组共享选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45385127/