在命令行上使用参数运行RMarkdown [英] Run RMarkdown with arguments on the command line

查看:108
本文介绍了在命令行上使用参数运行RMarkdown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to run a Rmarkdown file (myfile.Rmd) from the command line terminal. 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
```

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

So, first of all, is it possible to run a Rmarkdown file by reading arguments as done for Rscripts? I mean, not by reading input files as described in this question.

如果是,该怎么办?我希望该作品此处曾经运行过Rmarkdown文件对我有用,但这不是因为参数.我正在尝试运行类似的内容:

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. I'm trying to run something like:

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

编辑:但它出现以下错误:

But it gives the following error:

tools :: file_path_as_absolute(input)中的错误:文件'myfile.Rmd_myarg'不存在调用:-> setwd->目录名->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":不存在任何导演跨界大叶蜂

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 对象作为参数是一种方法:

Adding the myarg object as a parameter is the way to go:

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

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

And then add the parameter to your Rmd file:

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

此处有关参数化报告的文档: https://rmarkdown.rstudio.com/developer_parameterized_reports.html

Documentation on parameterized reports here: https://rmarkdown.rstudio.com/developer_parameterized_reports.html

这篇关于在命令行上使用参数运行RMarkdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆