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()
|
|
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
|
|
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
|
|
With this method, one can conditionally include a document given that all chunk options can take values from arbitrary R expressions. For example:
|
|
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:
|
|
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
- Rmarkdown-cookbook: https://bookdown.org/yihui/rmarkdown-cookbook/managing-projects.html
Last modified on 2022-03-22