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}
```
}
```
Or, more simply:
或者,更简单地说:
```{r}
child_docs <- c("child/doc1.Rmd", "child/doc2.Rmd","child/doc3.Rmd","child/doc4.Rmd")
```
```{r child = child_docs}
```
I have also tried these, but they did not work (RMarkdown code chunk):
我也尝试了这些,但它们没有用(RMarkdown代码块):
```{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)
}
```
(Directly within the RMarkdown document):
(直接在RMarkdown文档中):
`for(i in seq_along(child_doc)){ doc <- child_doc[i]; knit_child(doc)}`
For reference, the manual for doing this (in LaTeX) is here.
作为参考,这里的手册(在LaTeX中)就在这里。
2 个解决方案
#1
11
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.
在实现我自己的代码之后,为了获得要包含的子文档列表。
#2
1
You can dynamically render a child document
您可以动态呈现子文档
```{r, result='asis'}
cat(knitr::knit_child("child.Rmd", quiet=TRUE))
```
which gives you great flexibility.
这给你很大的灵活性。
The "trick" is the result='asis'
so that the output is then passed on for conversion (as opposed to "knitting").
“技巧”是结果='asis',然后传递输出以进行转换(而不是“编织”)。
Alternatively, you can tag the object and avoid the "asis" in the chunk options:
或者,您可以标记对象并避免块选项中的“asis”:
```{r}
cat(knitr::asis_output(knitr::knit_child("child.Rmd", quiet=TRUE)))
```
This is more robust, as it allows you to use this in nested functions, etc., and not have to rely on the caller having set the "correct" chunk options.
这更加健壮,因为它允许您在嵌套函数等中使用它,而不必依赖于调用者设置了“正确”的块选项。
If you want to pass in "current" variables and libraries, pass in the current environment too:
如果要传入“当前”变量和库,也要传入当前环境:
```{r, result='asis'}
cat(knitr::knit_child("child.Rmd", quiet=TRUE, envir=environment()))
```
#1
11
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.
在实现我自己的代码之后,为了获得要包含的子文档列表。
#2
1
You can dynamically render a child document
您可以动态呈现子文档
```{r, result='asis'}
cat(knitr::knit_child("child.Rmd", quiet=TRUE))
```
which gives you great flexibility.
这给你很大的灵活性。
The "trick" is the result='asis'
so that the output is then passed on for conversion (as opposed to "knitting").
“技巧”是结果='asis',然后传递输出以进行转换(而不是“编织”)。
Alternatively, you can tag the object and avoid the "asis" in the chunk options:
或者,您可以标记对象并避免块选项中的“asis”:
```{r}
cat(knitr::asis_output(knitr::knit_child("child.Rmd", quiet=TRUE)))
```
This is more robust, as it allows you to use this in nested functions, etc., and not have to rely on the caller having set the "correct" chunk options.
这更加健壮,因为它允许您在嵌套函数等中使用它,而不必依赖于调用者设置了“正确”的块选项。
If you want to pass in "current" variables and libraries, pass in the current environment too:
如果要传入“当前”变量和库,也要传入当前环境:
```{r, result='asis'}
cat(knitr::knit_child("child.Rmd", quiet=TRUE, envir=environment()))
```