在pandoc_args中引用“参数”列表(rmarkdown)

时间:2019-04-14 20:04:54

标签: r-markdown pandoc

我想在rmarkdown yaml块的输出部分中对输出文件名进行参数化。我在下面显示一些我尝试过的事情,以及它们是如何失败的(请参见注释行)。

params $ outfilename设置正确,但是我不知道以后如何引用它来设置pandoc_args。

我正在使用@tf.function构建输出文件。

这可能吗?谁能指出我做错了什么?

rmarkdown::render('mydocument.Rmd')

这是我的会话信息:

---
params:
  longtitle: "School Data: Aprendo Leyendo Efficacy Study"
  reportdate: !r Sys.Date()
  outfilename: !r paste0("Intelexia-school-data-test-", format(Sys.time(), "%Y%m%d"))

title: "`r params$longtitle`"
date: "`r params$reportdate`"
author: David Braze

output:
  html_document:
    highlight: tango
    pandoc_args:
    - --output
#    - "`r params$outfilename`"  # generates file named "`r params$outfilename`"
#    - `r params$outfilename`  # scanner error
#    - !r `params$outfilename`  # scanner error
#    - !r "params$outfilename"  # generates file named "params$outfilename"
#    - !r params$outfilename  # generates file named "params$outfilename"
#    - !expr params$outfilename # cannot evaluate expression
#    - !expr "params$outfilename" # cannot evaluate expression
#    - params$outfilename # generates file named "params$outfilename"
    - !expr paste0("Intelexia-school-data-", format(Sys.time(), "%Y%m%d"), ".html")
    theme: united
    toc: yes
    toc_depth: 2
    toc_float: yes
---

1 个答案:

答案 0 :(得分:0)

我不确定如何(或是否)使用参数值以所需的方式指定pandoc_args。您可以通过使用rmarkdown::render()可选参数来获得所需结果的另一种方法。

举一个更简单的例子,假设mydocument.Rmd的内容是:

---
params:
  outfilename: "myfile"
output: html_document
---

The name of the file is `r params$outfilename`

如果您当前对rmarkdown::render("mydocument.Rmd")的呼叫被替换为:

fname <- paste0("Intelexia-school-data-test-", format(Sys.time(), "%Y%m%d"))
rmarkdown::render(
  "mydocument.Rmd", 
  output_file = paste0(fname, ".html"), 
  params = list(outfilename = fname)
)

然后,渲染的文件将被命名为Intelexia-school-data-test-20190415.html,内容为The name of the file is Intelexia-school-data-test-20190415