Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally include a list of child documents in RMarkdown with knitr

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):

```{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):

`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.

like image 679
user5359531 Avatar asked Sep 07 '16 19:09

user5359531


1 Answers

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.

like image 127
user5359531 Avatar answered Oct 05 '22 01:10

user5359531