I have a parameterized RMarkdown file, parameterized.Rmd
, with the following contents.
---
title: "Parameterized report"
output: html_document
params:
input_df: NULL
---
```{r sec1}
head(params$input_df[, 1:2])
```
I can knit it from the console using rmarkdown::render
, generating distinct documents for distinct dataframe inputs. This works as expected.
rmarkdown::render("parameterized.Rmd",
params = list(input_df = mtcars),
output_file = "cars.html")
rmarkdown::render("parameterized.Rmd",
params = list(input_df = iris),
output_file = "iris.html")
I would like to have each of these results be child documents to a common document. My first attempt is with knitr::knit_child
, but it does not take params
as an argument. So this failed.
---
title: "Main report"
output: html_document
---
```{r test-cars}
knitr::knit_child("parameterized.Rmd", envir = environment(), quiet = T,
params = list(input_df = mtcars))
```
How may I knit together child documents which require parameters?
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.
---
title: "Parameterized report"
output: html_document
---
```{r}
head(df[, 1:2])
```
---
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.