R/rmarkdown_helpers.R
asis_knit_child.Rd
This slightly modifies the knitr::knit_child()
function to have different defaults.
the environment defaults to the calling environment.
the output receives the class knit_asis
, so that the output will be rendered "as is" by knitr when calling inside a chunk (no need to set results='asis'
as a chunk option).
defaults to quiet = TRUE
asis_knit_child(input = NULL, text = NULL, ..., quiet = TRUE, options = NULL, envir = parent.frame(), use_strings = TRUE)
input | if you specify a file path here, it will be read in before being passed to knitr (to avoid a working directory mess) |
---|---|
text | passed to |
... | passed to |
quiet | passed to |
options | defaults to NULL. |
envir | passed to |
use_strings | whether to read in the child file as a character string (solves working directory problems but harder to debug) |
Why default to the calling environment? Typically this function defaults to the global environment. This makes sense if you want to use knit children in the same context as the rest of the document. However, you may also want to use knit children inside functions to e.g. summarise a regression using a set of commands (e.g. plot some diagnostic graphs and a summary for a regression nicely formatted).
Some caveats:
the function has to return to the top-level. There's no way to cat()
this from loops or an if-condition without without setting results='asis'
. You can however concatenate these objects with paste.knit_asis()
# NOT RUN { # an example of a wrapper function that calls asis_knit_child with an argument # ensures distinct paths for cache and figures, so that these calls can be looped in parallel regression_summary <- function(model) { hash <- digest::digest(model) options <- list( fig.path = paste0(knitr::opts_chunk$get("fig.path"), hash, "-"), cache.path = paste0(knitr::opts_chunk$get("cache.path"), hash, "-")) asis_knit_child("_regression_summary.Rmd", options = options) } # }