Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run RMarkdown with arguments on the command line

Tags:

bash

r

r-markdown

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!

like image 601
elcortegano Avatar asked Apr 18 '18 16:04

elcortegano


People also ask

How do I use parameters in R Markdown?

Parameters in an R Markdown document are very simple to use. In the yaml header (the section at the top of the markdown document), you just have to add a couple new lines for your parameters to hardcode them in. Once they're coded in, they will be available in the params object for use in the rest of the analysis.

How do I run an RMD in RStudio?

To open a new file, click File > New File > R Markdown in the RStudio menu bar. A window will pop up that helps you build the YAML frontmatter for the . Rmd file. Use the radio buttons to select the specific type of output that you wish to build.

Can you run code in R Markdown?

You can open it here in RStudio Cloud. or by typing the chunk delimiters ```{r} and ``` . When you render your . Rmd file, R Markdown will run each code chunk and embed the results beneath the code chunk in your final report.

How do you set parameters in RStudio?

Setting Parameters values Better yet, click the “Knit with Parameters” option in the dropdown menu next to the RStudio IDE knit button to set parameters, render, and preview the report in a single user friendly step.


1 Answers

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

like image 71
Ryan Morton Avatar answered Sep 25 '22 02:09

Ryan Morton