【问题标题】:Rmarkdown child documents do not detect their `params`Rmarkdown 子文档未检测到它们的“参数”
【发布时间】:2018-09-03 15:27:33
【问题描述】:

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

object: 'params' not found

我相信是因为当孩子被编织时,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
---

【问题讨论】:

    标签: r r-markdown knitr


    【解决方案1】:

    据我了解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}
    ```
    

    【讨论】:

    • 谢谢 - 我会尝试选项 2/3。我必须在每个子文档中保留单独的参数,否则它们不能单独编织(并且每个子文档的参数都不相同)。我认为可能有一种方法可以在不手动分配参数的情况下做到这一点。
    • @mathematical.coffee 由于these lines,我认为必须分配参数。
    • @mathematical.coffee 我找到了一种使用knitr 钩子的方法。我更新了我的答案。我认为这已接近您的预期。
    • 这似乎不起作用,至少在使用 RStudio 中的“编织”按钮时,因为 params 处于锁定环境中? params &lt;&lt;- &lt;something&gt; 因此是一个错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2015-10-29
    • 1970-01-01
    • 2013-01-09
    • 2018-03-22
    • 2021-04-11
    • 2016-11-29
    相关资源
    最近更新 更多