在命令行上使用参数运行RMarkdown

elc*_*ano 4 bash r r-markdown

我正在尝试myfile.Rmd从命令行终端运行Rmarkdown文件()。该文件需要带一个参数才能起作用。我们可以使用以下简单文件作为示例:

---
title: "Simple example"
output:
  pdf_document: default
---

```{r read_arg, include=FALSE}
args = commandArgs(TRUE)
VAR = args[1]
```

```{r show_var}
VAR
```
Run Code Online (Sandbox Code Playgroud)

因此,首先,是否可以像读取Rscripts一样通过读取参数来运行Rmarkdown文件?我的意思是,不是按此问题所述读取输入文件。

如果是这样,怎么办?我希望这里用来运行Rmarkdown文件的工作对我有用,但这不是因为参数。我正在尝试运行类似的东西:

Rscript -e "rmarkdown::render('myfile.Rmd myarg')"
Run Code Online (Sandbox Code Playgroud)

编辑:但是它给出以下错误:

工具中的错误:: file_path_as_absolute(input):文件'myfile.Rmd_myarg'不存在调用:-> setwd-> dirname->Además:警告消息:1:在normalizePath(path,winslash = winslash,mustWork = mustWork): path [1] =“ myfile.Rmd myarg”:不存在目录2:在normalizePath(path,winslash = winslash,mustWork = mustWork)中:path [1] =“ myfile.Rmd_myarg”:不存在互惠生导演

有任何想法吗?谢谢!

Rya*_*ton 5

添加myarg对象作为参数是一种方法:

Rscript -e "rmarkdown::render('example.Rmd',params=list(args = myarg))

然后将参数添加到您的Rmd文件中:

---
title: "Simple example"
output:
  pdf_document: default
params:
  args: myarg
---
Run Code Online (Sandbox Code Playgroud)

有关参数化报告的文档,请访问:https : //rmarkdown.rstudio.com/developer_parameterized_reports.html