Reputation: 573
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?
Upvotes: 10
Views: 3113
Reputation: 11
You can actually also "overwrite" parameters in the child document if you run it in a child environment where you manually add the params
variable. It looks like the params set in YAML in the child document won't be used if you knitr::knit_child()
. So this should work:
---
title: "Parameterized report"
output: html_document
params:
input_df: NULL
---
```{r sec1}
head(input_df[, 1:2])
```
---
title: "Main report"
output: html_document
---
# mtcars
```{r mtcars, echo=FALSE, results='asis'}
e1 <- new.env()
e1$params$input_df <- mtcars
cat(
knitr::knit_child('parameterized.Rmd', envir = e1, quiet = TRUE)
)
```
# iris
```{r iris, echo=FALSE, results='asis'}
e2 <- new.env()
e2$params$input_df <- iris
cat(
knitr::knit_child('parameterized.Rmd', envir = e2, quiet = TRUE)
)
```
The outcome is quite similar. However, the child report will now be evaluated in a child environment. If you want to have variables from the child report to be available in the main report you have to set them via "<<-". Like this your global environment won't get flushed with variables that you might not need there. I use this for a child report that can also be knitted as a standalone version:
---
title: "Separate Report"
output: html_document
params:
fullprint: TRUE
---
```{r, eval=fullprint}
# do this only if knitting the full version is intended
...
```
```{r}
# always include this chunk
...
some_var <<- some_local_result
```
---
title: "Main Report"
output: html_document
---
```{r child1}
e <- new.env()
e$params$fullprint <- FALSE
knitr::knit_child("SeparateReport.Rmd", envir = e)
print(some_var)
```
Or you can pass the parameter from the main file
---
title: "Main Report"
output: html_document
params:
fullprint: FALSE
---
```{r child1}
knitr::knit_child("SeparateReport.Rmd")
print(some_var)
```
Upvotes: 0
Reputation: 573
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.
Upvotes: 12