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

标签 r r-markdown knitr

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

object: 'params' not found

我相信这是因为当 child 被编织时,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)钩子(Hook)临时分配参数

在这里,我为新的 use.params 定义了一个钩子(Hook)。 block 选项:此解决方案扩展了前一个解决方案。当use.params=TRUE使用时,这个钩子(Hook)会针对子文档的每个 block 运行。
请注意,使用此解决方案,您不能使用 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}
```

关于Rmarkdown 子文档未检测到它们的 `params`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49475303/

相关文章:

R 无法保存到 PRN.rData

r - 将矢量图像置于网格中以在 R 中进行克里金法

r - 如何更改 rmarkdown 中段落之间的间距?

r - knitr:无法创建带有 utf-8 字符的图形

r - knitr markdown 不显示 r 中的基本图

r - 根据成对相等对列进行分组

r - 自己计算协方差矩阵(不使用 `cov` )

r - 为什么 RMarkdown 无法正确转义第二个 LaTeX 表?

r - 如何从 R 获取当前 git commit 的 sha

format - 在 Markdown 中指定 docx 布局