I have a master Rmarkdown document into which I am including my individual chapters using knitr
's child
option. Each chapter makes use of rmarkdown parameters in its own YAML.
Each chapter compiles well individually, but when put in this master document, I get the error
object: 'params' not found
I believe it is because when the child is knitted, knitr does not read the parameters in the YAML (which is an rmarkdown feature, not a knitr feature).
Is there some way I can make these available to knitr? Is there an "rmarkdown"-way of putting in child documents?
---
title: My thesis
---
blah blah.
# Introduction
```{r child='01-introduction.rmd'}
```
# Mathematical background
```{r child='02-mathsy-maths.rmd'}
```
Example 01-introduction.rmd
---
title: Introduction
params:
dataset: ABC
---
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.
For example, # Say Hello to markdown . A single hashtag creates a first level header. Two hashtags, ## , creates a second level header, and so on. italicized and bold text - Surround italicized text with asterisks, like this *without realizing it* .
R Markdown is an extension of the markdown syntax. R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.
The rmarkdown package helps you create dynamic analysis documents that combine code, rendered output (such as figures), and prose.
As I understand knitr
, when you knit a child document, this document is evaluated in the context (ie environment) of the parent document.
So, I see 4 solutions.
With this solution, params are controlled inside the YAML
front-matter of the main document. I think it is the natural solution.
---
title: My thesis
params:
dataset: ABC
---
blah blah.
# Introduction
```{r child='01-introduction.rmd'}
```
# Mathematical background
```{r child='02-mathsy-maths.rmd'}
```
With this solution, params are controlled with R
code inside the main document.
---
title: My thesis
---
blah blah.
# Introduction
```{r set-params, include=FALSE}
params <- list(dataset = "ABC")
```
```{r child='01-introduction.rmd'}
```
# Mathematical background
```{r child='02-mathsy-maths.rmd'}
```
With this solution, params are controlled inside each child document. It is a variant of the previous solution.
In the main document, child document's params are read using knitr::knit_params()
and then assigned in the global environment.
---
title: My thesis
---
blah blah.
```{r def-assign-params, include=FALSE}
assign_params <- function(file) {
text <- readLines(file)
knit_params <- knitr::knit_params(text)
params <<- purrr::map(knit_params, "value")
}
```
# Introduction
```{r, include=FALSE}
assign_params('01-introduction.rmd')
```
```{r child='01-introduction.rmd'}
```
# Mathematical background
```{r child='02-mathsy-maths.rmd'}
```
Here, I define a hook for a new use.params
chunk option: this solution extends the previous one. When use.params=TRUE
is used, this hook is run for each chunk of the child document.
Note that with this solution, you cannot use params
in inline code.
---
title: "Main document"
---
```{r hook-def, include=FALSE}
params_cache <- new.env(parent = emptyenv())
knitr::knit_hooks$set(use.params = function(before, options, envir) {
if (before && options$use.params) {
if (exists("params", envir = envir)) {
params_cache$params <- envir$params
}
text <- readLines(knitr::current_input(dir = TRUE))
knit_params <- knitr::knit_params(text)
envir$params <- purrr::map(knit_params, "value")
}
if (!before && options$use.params) {
if (exists("params", envir = params_cache)) {
envir$params <- params_cache$params
rm("params", envir = params_cache)
} else {
rm("params", envir = envir)
}
}
})
```
blah blah.
# Introduction
```{r child='01-introduction.rmd', use.params=TRUE}
```
# Mathematical background
```{r child='02-mathsy-maths.rmd', use.params=TRUE}
```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With