【问题标题】:Run RMarkdown with arguments on the command line在命令行上使用参数运行 RMarkdown
【发布时间】:2018-09-29 00:06:57
【问题描述】:

我正在尝试从命令行终端运行 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 问题中描述的输入文件。

如果是这样,怎么做?我希望用于运行 Rmarkdown 文件的工作 here 对我有用,但这不是因为争论。我正在尝试运行类似:

Rscript -e "rmarkdown::render('myfile.Rmd myarg')"

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

工具错误::file_path_as_absolute(input) : 文件“myfile.Rmd_myarg”不存在 调用: -> setwd -> 目录名 -> Además:警告信息: 1:在 normalizePath(path, winslash = winslash, mustWork = mustWork) 中: path[1]="myfile.Rmd myarg": 不存在 el fichero o el Directorio 2:在 normalizePath(path, winslash = winslash, mustWork = mustWork) 中: path[1]="myfile.Rmd_myarg": 不存在 el fichero o el Directorio Ejecución interrumpida

有什么想法吗?谢谢!

【问题讨论】:

  • 您使用params 参数将参数传递给render。另外,在文件名和任何参数之间加一个逗号。
  • 看来我可以运行Rscript -e "rmarkdown::render('example.Rmd',params=list('myarg'))" 。现在生成 PDF 文件。我只是想知道为什么它按预期打印“NA”而不是“myarg”
  • myarg 添加到RMD 文件的参数中,以便它知道接受它们。
  • 另外,请阅读有关如何正确将参数传递给报告的文档:rmarkdown.rstudio.com/developer_parameterized_reports.html
  • 非常感谢,现在可以了。请随时将其写成答案。我会接受的

标签: r bash r-markdown


【解决方案1】:

添加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

【讨论】:

  • Rscript 调用中的小错误。应该是 args 让它工作你建议的 Rmd 代码。
  • Rscript 命令末尾还缺少引号,我无法编辑它,因为它少于 6 个字符的编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
  • 2014-07-20
  • 2014-06-23
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
相关资源
最近更新 更多