28

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
```

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.

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')"

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

Any ideas? Thank you!

elcortegano
  • 2,444
  • 11
  • 40
  • 58
  • 1
    You pass parameters to `render` using the `params` argument. Also, put a comma between the file name and any arguments. – Ryan Morton Apr 18 '18 at 16:46
  • It seems I can run `Rscript -e "rmarkdown::render('example.Rmd',params=list('myarg'))" `. The PDF file is now generated. I'm just wondering why it prints "NA" instead of "myarg", as expected – elcortegano Apr 18 '18 at 16:56
  • Add `myarg` to the parameters of the `RMD` file so it knows to accept them. – Ryan Morton Apr 18 '18 at 17:05
  • 1
    Also, please read the documentation on how to properly pass parameters to a report: https://rmarkdown.rstudio.com/developer_parameterized_reports.html – Ryan Morton Apr 18 '18 at 17:11
  • Thank you very much, now it works. Please, feel free to write it into an answer. I will accept it – elcortegano Apr 18 '18 at 19:52

2 Answers2

35

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

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

And then add the parameter to your Rmd file:

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

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

Ryan Morton
  • 2,605
  • 1
  • 16
  • 19
  • 1
    Small error in the call to Rscript. Should be args for it to work your suggested Rmd code. – JamesR Sep 16 '19 at 14:29
  • 1
    You're also missing a quotation mark at the end of the `Rscript` command, I can't edit it because it's less than a 6 character edit – Ben Jeffrey Mar 18 '20 at 09:51
1

Just to clarify for those who were confused like me:

The accepted answer implies that the parameter’s value in the YAML header and the parameter’s value in the command line must match. They do not need to match. Whatever myarg is, you do not need to add it to the YAML header.

That would defeat the purpose of rendering the report from the command line. Every time you want to change the value of arg, you would also need to open the .Rmd and change it in the YAML header.

Simply initializing the parameter in the header, e.g. with an empty string or NA, will suffice. E.g.:

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

```{r read_arg, include=FALSE}
args <-  params$args
VAR <- args[1]
```

```{r show_var}
VAR
```

Arguments passed to the Rscript command can then assign the desired value to the parameter by overwriting. E.g.:

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

I would also note that myarg is an argument, whereas arg is a parameter.