使用knitr在RMarkdown中有条件地包含子文档列表 [英] Conditionally include a list of child documents in RMarkdown with knitr

查看:58
本文介绍了使用knitr在RMarkdown中有条件地包含子文档列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出子文档列表,如何根据某些条件选择要插入到主文档中的子文档?

Given a list of child documents, how can you choose which to insert into a master document based on some criteria?

在我的用例中,我将一个数据集中的未知条目与第二个数据集中的所需条目进行匹配.第二个数据集具有与每个条目关联的子文档.如果找到匹配项,我想包括其关联的子文档.

In my use case, I am matching the unknown entries in one dataset to the desired entries in a second dataset. The second dataset has child documents associated with each entry. If a match is found, I want to include its associated child document.

此伪代码以其最基本的形式显示了我要实现的目标(受此问题启发

In its most basic form, this psuedo-code shows the gist of what I am trying to achieve (inspired by this question here):

```{r, eval=TRUE}
child_docs <- setNames(c(TRUE, FALSE, TRUE, FALSE),
                       c("doc1.Rmd", "doc2.Rmd","doc3.Rmd","doc4.Rmd"))

for(i in seq_along(child_docs)){
    file <- names(child_docs)[i]
    eval_status <- child_docs[i]

    ```{r child = file, eval = eval_status}
    ```

}

```

或更简单地说:

```{r}
child_docs <- c("child/doc1.Rmd", "child/doc2.Rmd","child/doc3.Rmd","child/doc4.Rmd")
```

```{r child = child_docs}
```

我也尝试过这些,但是它们没有用 (RMarkdown代码块):

I have also tried these, but they did not work (RMarkdown code chunk):

```{r}
child_docs <- c("child/doc1.Rmd", "child/doc2.Rmd","child/doc3.Rmd","child/doc4.Rmd")

for(i in seq_along(child_docs)){
    doc <- child_docs[i]
    knit_child(doc)
}

```

(直接在RMarkdown文档中):

(Directly within the RMarkdown document):

`for(i in seq_along(child_doc)){ doc <- child_doc[i]; knit_child(doc)}`

作为参考,(在LaTeX中)执行此操作的手册是此处.

For reference, the manual for doing this (in LaTeX) is here.

推荐答案

我自己找到了解决方案;只需将子文档的向量传递给代码块即可.

Found the solution myself; simply pass a vector of child documents to the code chunk.

```{r}
child_docs <- c("doc1.Rmd", "doc2.Rmd","doc3.Rmd","doc4.Rmd")
```

```{r, child = child_docs}
```

实现我自己的代码后,以获得要包含的子文档列表.

After implementing my own code in order to obtain the list of child docs to include.

这篇关于使用knitr在RMarkdown中有条件地包含子文档列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆