10) 'child1.Rmd' else 'child2.Rmd'} ``` Use the function knitr::knit_child() knitr::knit_child() can convert an Rmarkdown document to a character vector of markdown format, which can be printed out into current document directly, like below: 1 2 3 4 ```{r, echo=FALSE, results='asis'} knitStr " />

[R] Include external script or child documents in Rmarkdown

[This article was first published on R on Zhenguo Zhang's Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Zhenguo Zhang’s Blog /2022/03/22/r-include-external-script-or-child-documents-in-rmarkdown/ –

When one Rmarkdown document is too big, one can consider split the file into smaller ones. In this post, I will introduce the ways for this purpose.

Include external R scripts

There are two ways to include external scripts: using the functions source() or sys.source() or chunk option file.

Method 1: use source() or sys.source()

1
2
3
4
```{r, include=F}
source("external.R", local=knitr::knit_global())
# or sys.source("external.R", envir=knitr::knit_global())
```

Note that one needs to provide local environment explicitly with knitr::knit_global(); otherwise it may report errors of objects not found.

This method has a disadvantage: the input source code can’t be displayed or not formatted properly if this is a feature needed, and this issue goes away if one use the chunk option file.

Method 2: use chunk option file

1
2
```{r, file="external.R"}
```

With this method, one can also include code from other languages such as python, C++, etc.

Include external Rmarkdown

To include a child rmarkdown document, there are alsoe two ways: using the chunk option child and use the function knitr::knit_child().

Use chunk option child

1
2
```{r, child="child.Rmd"}
```

With this method, one can conditionally include a document given that all chunk options can take values from arbitrary R expressions. For example:

1
2
```{r, child=if(x>10) 'child1.Rmd' else 'child2.Rmd'}
```

Use the function knitr::knit_child()

knitr::knit_child() can convert an Rmarkdown document to a character vector of markdown format, which can be printed out into current document directly, like below:

1
2
3
4
```{r, echo=FALSE, results='asis'}
knitStr <- knitr::knit_child('child.Rmd', quiet = TRUE)
cat(knitStr, sep = '\n')
```

I hope that this summary gives you some ideas of how to include external documents in an Rmarkdown document. From here, you can dig deeper by reading other documents such as the link listed at Reference.

Reference

  1. Rmarkdown-cookbook: https://bookdown.org/yihui/rmarkdown-cookbook/managing-projects.html
- /2022/03/22/r-include-external-script-or-child-documents-in-rmarkdown/ -
To leave a comment for the author, please follow the link and comment on their blog: R on Zhenguo Zhang's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)