Looks up a text string from a data frame based on a chunk name and
position (before/after). Optionally expands knitr templating syntax
({{...}}) found in the text.
Arguments
- data
A data frame with columns
chunk,before, andtext.- chunk
Character. The chunk name to look up in
data. If the file extension is"rmarkdown", it is stripped automatically.- before
Logical. Whether to retrieve the text marked as "before" (
TRUE, default) or "after" (FALSE) the chunk.- error_on_empty
Controls behaviour when no matching text is found:
TRUE: throws an error viacli::cli_abort().FALSE: issues a warning viacli::cli_warn().NULL(default): silently returns an emptyAsIsobject.
- enabled
Logical. If
FALSE, the function returnsI(character(0))immediately without any lookup. Can be set globally viaglobal_settings_set(new = list(enabled = FALSE), fn_name = "insert_text").
Value
An I()-wrapped character string. If no match is found and
error_on_empty is NULL, an empty AsIs character vector.
Details
The function filters data for rows matching the given chunk and
before values. If exactly one row matches, its text column is
returned. If the text contains knitr templating delimiters ({{),
it is expanded with knitr::knit_expand().
Examples
if (FALSE) { # \dontrun{
texts <- data.frame(
chunk = c("intro", "intro"),
before = c(TRUE, FALSE),
text = c("Before the chunk.", "After the chunk.")
)
insert_text(texts, "intro", before = TRUE)
insert_text(texts, "intro", before = FALSE)
} # }