Rmarkdown child documents do not find their "options"

I have a basic Rmarkdown document in which I include my individual chapters using the option knitr child. Each chapter uses rmarkdown options in its own YAML . Each chapter is compiled individually, but when it is placed in this main document, I get an error

object: 'params' not found

I believe this is due to the fact that when the child is knitted, the scribe does not read the parameters in YAML (which is the rmarkdown function, not the knitr function).

Is there a way to make them available for knitr? Is there a "rmarkdown" entry for children’s 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
---
+4
source share
1 answer

knitr, , (.. ) .

, 4 .

YAML . , .

---
title: My thesis
params:
  dataset: ABC
---

blah blah.

# Introduction

```{r child='01-introduction.rmd'}
```

# Mathematical background

```{r child='02-mathsy-maths.rmd'}
```

R .

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

. .
knitr::knit_params(), .

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

(hacky)

hook use.params chunk: . use.params=TRUE, .
, params .

---
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}
```
+2

Source: https://habr.com/ru/post/1695302/


All Articles