Conditionally enable list of child documents in RMarkdown with knitr

Given the list of child documents, how can you choose which insertion into the main document based on some criteria?

In my use case, I match unknown records in one data set with the desired data in the second data set. The second data set contains the child documents associated with each record. If a match is found, I want to include the associated child document.

In its most basic form, this pseudo code shows the essence of what I'm 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 simply put:

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

I tried it too, but they did not work (RMarkdown code snippet):

 ```{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 in the RMarkdown document):

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

For reference, a guide for this (in LaTeX) is here .

+6
source share
1 answer

Found a solution myself; just pass the vector of the child documents into a piece of code.

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

After implementing my own code, to get a list of child documents to include.

+11
source

All Articles