partial {rmdpartials} | R Documentation |
Knit a child document and output as is (render markup)
Description
This modifies and extends the knitr::knit_child()
function. Defaults change as follows:
the environment defaults to the calling environment, or if passed, to arguments passed via ...
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 setresults='asis'
as a chunk option).defaults to
quiet = TRUE
the package additionally renders
knit_asis
objects in the viewer when printed to make previewing partials easier. This is achieved usingrmarkdown::render()
and done in a temporary directory (only when used interactively/not in child mode).the package takes care of some troubles behind the scenes that you might find yourself in if you nest partials (by trying to resolve path ambiguities, using text instead of files for sources, and some functionality to prevent iteratively overwriting generated figures and other files)
Usage
partial(
input = NULL,
...,
text = NULL,
output = NULL,
quiet = TRUE,
options = NULL,
envir = parent.frame(),
name = NULL,
show_code = FALSE,
use_strings = TRUE,
render_preview = needs_preview(),
preview_output_format = NULL
)
Arguments
input |
if you specify a file path here, it will be read in before being passed to knitr (to avoid a working directory mess) |
... |
ignored, but you can use it to clarify which variables will be used in the rmd partial |
text |
passed to |
output |
if you specify a file path here, where to put the file |
quiet |
passed to |
options |
defaults to NULL. |
envir |
passed to |
name |
a name to use for cacheing and figure paths. Randomly generated if left unspecified. |
show_code |
whether to print the R code for the partial or just the results (sets the chunk option echo = FALSE while the chunk is being rendered) |
use_strings |
whether to read in the child file as a character string (solves working directory problems but harder to debug) |
render_preview |
true if interactive mode is auto-detected, false when actually knitting the partial as a child |
preview_output_format |
defaults to |
Details
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 to respect conventional scoping rules 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 settingresults='asis'
. You can however concatenate these objects withpaste.knit_asis()
currently not yet producing expected results in RStudio notebooks in interactive use
Value
Returns rendered markdown with the class "knit_asis". When used interactively, the knit_meta attributes will additionally contain the path of a rendered preview in a temporary directory.
Examples
# super simple partial example
partial(text = "Test")
# an example of a wrapper function that calls partial with an argument
# ensures distinct paths for cache and figures, so that these calls can be looped in parallel
regression_diagnostics <- function(regression, ...) {
partial(system.file("_regression_diagnostics.Rmd",
package = "rmdpartials", mustWork = TRUE),
regression = regression, ...)
}