且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 YAML 中使用 R 代码或 Windows 用户变量(“%userprofile%")?

更新时间:2023-10-25 17:39:58

问题是output字段中的参数只有rmarkdown能理解.Pandoc 不理解它们,因此您需要确保 rmarkdown 可以评估表达式.由于 rmarkdown 使用 yaml 包来读取 YAML 元数据,而 yaml 的 R 表达式语法是 !expr,可以把R表达式放在!expr之后,eg

The problem is that the parameters in the output field is only understood by rmarkdown. Pandoc does not understand them, so you need to make sure rmarkdown can evaluate the expression. Since rmarkdown uses the yaml package to read the YAML metadata, and yaml's syntax for R expressions is !expr, you can put the R expression after !expr, e.g.

output:
  word_document:
    fig_caption: yes
    fig_height: 4
    fig_width: 7
    reference_docx: !expr file.path(Sys.getenv('USERPROFILE'), 'Documents', 'template.docx')