epoxy_use {epoxy} | R Documentation |
Reuse a Template Chunk
Description
Reuse a template from another chunk or file. By calling epoxy_use_chunk()
in an R chunk or inline R expression, you can reuse a template defined in
another chunk in your document.
Alternatively, you can store the template in a separate file and use
epoxy_use_file()
to reuse it. When stored in a file, the template file can
contain YAML front matter (following the same rules as pandoc documents)
with options that should be applied when calling an epoxy function. The
specific function called by epoxy_use_file()
can be set via the engine
option in the YAML front matter; the default is epoxy()
.
Usage
epoxy_use_chunk(.data = NULL, label, ...)
epoxy_use_file(.data = NULL, file, ...)
Arguments
.data |
A data set |
label |
The chunk label, i.e. the human-readable name, of the chunk
containing the template string. This chunk should be an |
... |
Arguments passed on to
|
file |
The template file, i.e. a plain text file, containing the
template. An |
Value
A character string of the rendered template based on the label
chunk. The results are marked as "asis"
output so that they are treated
as regular text rather than being displayed as code results.
Use in R Markdown or Quarto
```{epoxy movie-release} {.emph title} was released in {year}. ``` ```{r} # Re-using the template we defined above epoxy_use_chunk(bechdel[1, ], "movie-release") ``` ```{r} # Using in a dplyr pipeline bechdel |> dplyr::filter(year == 1989) |> epoxy_use_chunk("movie-release") ```
Or you can even use it inline:
It's hard to believe that `r epoxy_use_chunk(bechdel[2, ], "movie-release")`.
It's hard to believe that Back to the Future Part II was released in 1989.
The same template could also be stored in a file, e.g. movie-release.md
:
--- engine: epoxy --- {.emph title} was released in {year}.
The YAML front matter is used in template files to set options for the
template. You can use the engine
option to choose the epoxy function to be
applied to the template, e.g. engine: epoxy_html
or engine: epoxy_latex
.
By default, engine: epoxy
is assumed unless otherwise specified.
Template Options
When rendering a template, epoxy_use_chunk()
and epoxy_use_file()
will
inherit the options set in a number of different ways. The final template
options are determined in the following order, ranked by importance. Options
set in a higher-ranked location will override options set in a lower-ranked
location.
The arguments passed to
epoxy_use_chunk()
, such as.data
or any arguments passed in the...
. These options always have preference over options set anywhere else.The chunk options from the chunk where
epoxy_use_chunk()
orepoxy_use_file()
is called.The chunk options from the template chunk or file. These options typically are relevant to the template itself, such as the engine used or the opening and closing delimiters.
Global knitr chunk options for the document. You can set these with
knitr::opts_chunk$set()
, see?knitr::opts_chunk
for more information.