我正在尝试从命令行终端运行 Rmarkdown 文件 (myfile.Rmd
)。该文件需要带参数才能工作。我们可以使用这个简单的文件作为示例:
---
title: "Simple example"
output:
pdf_document: default
---
```{r read_arg, include=FALSE}
args = commandArgs(TRUE)
VAR = args[1]
```
```{r show_var}
VAR
```
那么,首先,是否可以像 Rscripts 那样通过读取参数来运行 Rmarkdown 文件?我的意思是,不是像 this 中描述的那样读取输入文件问题。
如果可以,如何实现?希望作品here用于运行对我有用的 Rmarkdown 文件,但不是因为参数。我正在尝试运行类似的东西:
Rscript -e "rmarkdown::render('myfile.Rmd myarg')"
编辑:但它给出了以下错误:
Error in tools::file_path_as_absolute(input) : file 'myfile.Rmd_myarg' does not exist Calls: -> setwd -> dirname -> Además: Warning messages: 1: In normalizePath(path, winslash = winslash, mustWork = mustWork) : path[1]="myfile.Rmd myarg": No existe el fichero o el directorio 2: In normalizePath(path, winslash = winslash, mustWork = mustWork) : path[1]="myfile.Rmd_myarg": No existe el fichero o el directorio Ejecución interrumpida
有什么想法吗?谢谢你!
最佳答案
添加 myarg
对象作为参数是可行的方法:
Rscript -e "rmarkdown::render('example.Rmd',params=list(args = myarg))"
然后将参数添加到您的 Rmd
文件中:
---
title: "Simple example"
output:
pdf_document: default
params:
args: myarg
---
此处有关参数化报告的文档:https://rmarkdown.rstudio.com/developer_parameterized_reports.html
关于在命令行上使用参数运行 RMarkdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49904943/