Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmarkdown child documents do not detect their `params`

Tags:

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
---
like image 361
mathematical.coffee Avatar asked Mar 25 '18 11:03

mathematical.coffee


People also ask

How do I use parameters in RMarkdown?

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.

What does ## do in R markdown?

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* .

What is the difference between markdown and RMarkdown?

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.

What type of documents are generated by RMarkdown?

The rmarkdown package helps you create dynamic analysis documents that combine code, rendered output (such as figures), and prose.


1 Answers

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.

Set the params in the main document

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'}
```

Assign the params in the global environment

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'}
```

Retrieve params of the child document

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'}
```

Use a (hacky) hook to assign params temporarily

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}
```
like image 102
RLesur Avatar answered Oct 11 '22 15:10

RLesur