Rmarkdown 子文档未检测到它们的“params”

Rmarkdown child documents do not detect their `params`

我有一个 Rmarkdown 主文档,我使用 knitrchild 选项将我的各个章节包含在其中。每章都使用rmarkdown parameters in its own YAML。 每一章单独编译都很好,但是当放在这个主文档中时,我得到错误

object: 'params' not found

我相信是因为knitr在knitr的时候并没有读取YAML里面的参数(是rmarkdown的特性,不是knitr的特性)

有什么方法可以让 knitr 可以使用它们吗?是否有 "rmarkdown" 方式放入子文档?

---
title: My thesis
---

blah blah.

# Introduction

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

# Mathematical background

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

示例01-introduction.rmd

---
title: Introduction
params:
    dataset: ABC
---

据我了解knitr,当你编织一个子文档时,这个文档是在父文档的上下文(即环境)中评估的。

所以,我看到了 4 个解决方案。

在主文档中设置参数

使用此解决方案,参数在主文档的 YAML front-matter 中进行控制。我认为这是自然的解决方案。

---
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)挂钩临时分配参数

在这里,我为新的 use.params 块选项定义了一个钩子:这个解决方案扩展了前一个。当使用 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}
```