简体   繁体   English

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

[英]Run RMarkdown with arguments on the command line

I'm trying to run a Rmarkdown file ( myfile.Rmd ) from the command line terminal.我正在尝试从命令行终端运行 Rmarkdown 文件 ( myfile.Rmd )。 This file needs to take an argument to work.这个文件需要一个参数才能工作。 We can use this simple file as an example:我们可以使用这个简单的文件作为例子:

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

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

```{r show_var}
VAR
```

So, first of all, is it possible to run a Rmarkdown file by reading arguments as done for Rscripts?那么,首先,是否可以通过读取 Rscripts 的参数来运行 Rmarkdown 文件? I mean, not by reading input files as described in this question.我的意思是,不是通过读取这个问题中描述的输入文件。

If so, how can it be done?如果是这样,怎么做? I hope that the piece of work here used to run a Rmarkdown file worked for me, but it doesn't because of the argument.我希望这里用来运行 Rmarkdown 文件的工作对我有用,但不是因为参数。 I'm trying to run something like:我正在尝试运行类似的东西:

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

EDIT: But it gives the following error:编辑:但它给出了以下错误:

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 tools::file_path_as_absolute(input) 中的错误:文件“myfile.Rmd_myarg”不存在调用:-> setwd -> dirname -> Además:警告消息:1:在 normalizePath(path, winslash = winslash, mustWork = mustWork) 中: path[1]="myfile.Rmd myarg": Noexiste el fichero o el Directorio 2: In normalizePath(path, winslash = winslash, mustWork = mustWork) : path[1]="myfile.Rmd_myarg": Noexiste el fichero o el Directorio Ejecución interrumpida

Any ideas?有什么想法吗? Thank you!谢谢!

Adding the myarg object as a parameter is the way to go:添加myarg对象作为参数是要走的路:

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

And then add the parameter to your Rmd file:然后将参数添加到您的Rmd文件中:

---
title: "Simple example"
output:
  pdf_document: default
params:
  args: myarg
---

Documentation on parameterized reports here: https://rmarkdown.rstudio.com/developer_parameterized_reports.html此处参数化报告的文档: https : //rmarkdown.rstudio.com/developer_parameterized_reports.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM