簡體   English   中英

在命令行上使用參數運行 RMarkdown

[英]Run RMarkdown with arguments on the command line

我正在嘗試從命令行終端運行 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 文件? 我的意思是,不是通過讀取這個問題中描述的輸入文件。

如果是這樣,怎么做? 我希望這里用來運行 Rmarkdown 文件的工作對我有用,但不是因為參數。 我正在嘗試運行類似的東西:

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

編輯:但它給出了以下錯誤:

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

有什么想法嗎? 謝謝!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM