Passing Parameters to R Markdown

Passing Parameters to R Markdown

In my case it worked, just had to change the indentation in the header and some names which are available in my folder...

Here my jnk.Rmd

---
title: "Liquidity Report"
output: pdf_document
params:
client: "NAMESPACE"
---
```{r plot, echo=FALSE, warning=FALSE}
cftest <- read.csv(params$client)
```

And this is what I called in the console : render('jnk.Rmd',params= list( client= "NAMESPACE"))

Passing parameters through an RMarkdown document?

I think the step you were missing was specifying the output filename so that each "vendor" would have its own file; otherwise, the same filename is overwritten each time leaving you with a single HTML document.

An example:

---
title: mtcars cyl
author: r2evans
params:
cyl: null
---

We have chosen to plot a histogram of `mtcars` where `$cyl` is equal to `r params$cyl`:

```{r}
dat <- subset(mtcars, cyl == params$cyl)
if (nrow(dat) > 0) {
hist(dat$disp)
}
```

Calling this with:

for (cy in c(4,6,8)) {
rmarkdown::render("~/StackOverflow/10466439/67525642.Rmd",
output_file = sprintf("cyl_%s.html", cy),
params = list(cyl = cy))
}

will render three HTML files, cyl_4.html, cyl_6.html, and cyl_8.html, each with differing content:

Sample Image

How can I pass variables into an R markdown .Rmd file?

I think you can use knit2html from knitr package to do the "magic".

  1. You define your markdown file like this and save it as mydoc.Rmd

     ```{r}
    source('test.R')
    ```
    ```{r}
    library(ggplot2)
    for (v in union(ivs, dvs))
    {
    hist <- ggplot(myDF, aes_string(x=v)) + geom_histogram()
    print(hist)
    }
  2. In test.R you prepare your data :

    myDF <- read.delim("mydata.tab")
    ivs <- c("iv1", "iv2", "iv3")
    dvs <- c("dv1", "dv2", "dv3")
  3. You compile using knitr

    Knit2html('mydoc.Rmd')

Pass Parameters from Command line into R markdown document

A few ways to do it.

You can use the backtick-R chunk in your YAML and specify the variables before executing render:

---
title: "`r thetitle`"
author: "`r theauthor`"
date: "July 14, 2015"
---

foo bar.

Then:

R -e "thetitle='My title'; theauthor='me'; rmarkdown::render('test.rmd')"

Or you can use commandArgs() directly in the RMD and feed them in after --args:

---
title: "`r commandArgs(trailingOnly=T)[1]`"
author: "`r commandArgs(trailingOnly=T)[2]`"
date: "July 14, 2015"
---

foo bar.

Then:

 R -e "rmarkdown::render('test.rmd')" --args "thetitle" "me"

Here if you do named args R -e ... --args --name='the title', your commandArgs(trailingOnly=T)[1] is the string "--name=foo" - it's not very smart.

In either case I guess you would want some sort of error checking/default checking. I usually make a compile-script, e.g.

# compile.r
args <- commandArgs(trailingOnly=T)
# do some sort of processing/error checking
# e.g. you could investigate the optparse/getopt packages which
# allow for much more sophisticated arguments e.g. named ones.
thetitle <- ...
theauthor <- ...
rmarkdown::render('test.rmd')

And then run R compile.r --args ... supplying the arguments in whichever format I've written my script to handle.

Passing multiple parameters in RMarkdown document

RMD file -

---
title: "`r params$MVNDR_NBR`"
author: "Santiago Canon"
date: "5/26/2021"
output:
html_document:
highlight: monochrome
theme: flatly
params:
MVNDR_NBR: NA
MVNDR_NM: NA

---

<font size="4"> This document will provide a summary of "`r params$MVNDR_NM`" performance within in QC: </font>

for loop -

mvndr_nm <- c('name1', 'name2', 'name3', 'name4', 'name5', 'name6')
mvndr_nbr<- c('60031167', '60688509', '60074051', '60148060', '60086898', '60080204')

for (i in seq_along(mvndr_nm)) {
rmarkdown::render("C:/Users/santi/Documents/R Scripts/Export_Data_CSV.Rmd",
output_file = sprintf("MVNDR_%s.html", mvndr_nbr[i]),
params = list(MVNDR_NBR = mvndr_nbr[i], MVNDR_NM = mvndr_nm[i]))
}

Output -

MVNDR_60031167.html

Sample Image

MVNDR_60688509.html -

Sample Image

How to pass a parameter when rendering an R script with spin?

If you want to pass some parameters from the params argument of rmarkdown::render() function to knitr, it's a bit complicated as the knitr::spin() function overrides parameters. You can specify a commented header in your R script (as seen in the documentation), with empty parameter value, e.g. test.R:

#' ---
#' params:
#' wanted_cyl: null
#' ---

library(tidyverse)

mtcars %>%
filter(cyl == params$wanted_cyl)

Then you can call:

rmarkdown::render(input = "test.R", params = list("wanted_cyl" = 6))

How do I knit child documents with parameters into a main RMarkdown document?

What worked for me (derived from the documentation once I properly understood it.):

Instead of using the params field in the YAML header, set the values of the parameters inside the main document and call cat on the output of knitr::knit_child. The below files achieved the desired result.

parameterized.Rmd

---
title: "Parameterized report"
output: html_document
---

```{r}
head(df[, 1:2])
```

main.Rmd

---
title: "Main report"
output: html_document
---

# mtcars
```{r mtcars, echo=FALSE, results='asis'}
df <- mtcars
cat(
knitr::knit_child('parameterized.Rmd', envir = environment(), quiet = TRUE)
)
```

# iris
```{r iris, echo=FALSE, results='asis'}
df <- iris
cat(
knitr::knit_child('parameterized.Rmd', envir = environment(), quiet = TRUE)
)
```

Knitting main.Rmd applied the parameterized report to each dataframe.



Related Topics



Leave a reply



Submit