2

I have an Report.Rmd file, that generates a pdf. Inside the .Rmd file I have some code with optparse, that receives a parameter from Rscript to use:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(optparse)
# get user as option 
option_list <- list(
  make_option(
  c("-u", "--u"), 
  type = "character", 
  default = "username"))
# parse accountId
parser <- OptionParser(
  usage = "%prog [options] file",
  option_list = option_list,
  add_help_option = F)
args <- parse_args(parser, positional_arguments = TRUE)
opt <- args$options
```

When I put this code (minus the ``` and knitr) in file.R and run it with Rscript like this:

Rscript file.R -u abcd

The code excecutes with abcd as value of the variable opt$u.

The problem is, I need to create a pdf from commandline and use the optparse argument. I can generate the pdf this with:

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

Which works fine, just not with the right value for opt$u. So, how to add the -u argument to the Rscript? I tried things like Rscript -e "-u abcd;rmarkdown::render('Report.Rmd'), that gives me ERROR: option '-e' requires a non-empty argument. Rscript -e "-u abcd" -e "markdown::render('Rapportage.Rmd')" gives me the same error. How to fix this...?

EDIT: a workaround might be to call file.R, write the username to .RData, and load .RData in the .Rmd. But I hope there is an easier, cleaner way...

raoul
  • 197
  • 3
  • 14
  • You could do it in two steps, call an R script with your argument that generates/alters the Rmd, and then knit the Rmd. (https://stackoverflow.com/questions/28507693/call-rmarkdown-on-command-line-using-a-r-that-is-passed-a-file) – Martin Schmelzer Nov 21 '17 at 10:10
  • That would mean I have to create an file.R , that creates the .Rmd... Problem is, I don't know how to do that, and I'm not sure it is easier than generating the right value with an file.R, write it to .RData, and load the .RData as environment when the .Rmd renders...? – raoul Nov 21 '17 at 10:26
  • 1
    And using the `params` argument of render? `render("file.Rmd", params = list(myArg = "abcd"))`. Inside the Rmd you can fetch that using `params$myArg`. – Martin Schmelzer Nov 21 '17 at 10:45
  • If i do `Rscript -e "rmarkdown::render('Rapportage.Rmd', params = list(myArg="raoul"))"`, I get `Error in filter_impl(.data, quo) : Evaluation error: object 'params' not found. Calls: ... -> filter -> filter.tbl_df -> filter_impl -> .Call` – raoul Nov 21 '17 at 11:20
  • 1
    Not sure, but maybe you have to define myArg in the YAML as well: `List of named parameters that override custom params specified within the YAML front-matter ` – Martin Schmelzer Nov 21 '17 at 11:39
  • thanks, that did it! If you make it an answer, I can accept it as an solution :) `Rscript -e "rmarkdown::render('Rapportage.Rmd', params = list(name=\"abcd\"))"` finally did the trick. Pay attention to the extra backslashes before the `"`. (http://rmarkdown.rstudio.com/developer_parameterized_reports.html) – raoul Nov 21 '17 at 19:49

1 Answers1

2

So as the OP and me figured out together, one can pass arguments in the rendering process using the params argument of rmarkdown::render(). It takes a list of named parameters. In order for it to work, you have to set up the same parameters in your YAML header.

Example:

This is our document file.Rmd:

---
title: "MWE"
output: pdf_document
params:
  myName: ""
---

```{r color, results='markup'}
print(params$myName)
```

Now we can pass a value for our myName parameter using

rmarkdown::render("file.Rmd", params = list(myName = "Martin"))

Notice that when you render in the console, that you have to escape quotes inside the R command you execute:

Rscript -e "rmarkdown::render(\"file.Rmd\", params = list(myName = \"abcd\"))"
Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98