epoxy_mustache {epoxy} | R Documentation |
Mustache-style string interpolation
Description
A wrapper around the mustache templating language, provided by the
whisker package. Under the
hood, epoxy_mustache()
uses whisker::whisker.render()
to render the
template, but adds a few conveniences:
The template can be passed in
...
as a single string, several strings or as a vector of strings. If multiple strings are passed, they are collapsed with.sep
("\n"
by default).-
epoxy_mustache()
can be vectorized over the items in the.data
argument. If.data
is a data frame, vectorization is turned on by default so that you can iterate over the rows of the data frame. The output will be a character vector of the same length as the number of rows in the data frame.
Usage
epoxy_mustache(
...,
.data = parent.frame(),
.sep = "\n",
.vectorized = inherits(.data, "data.frame"),
.partials = list()
)
Arguments
... |
A string or a vector of strings containing the template(s). Refer
to the mustache documentation for an overview
of the syntax. If multiple strings are passed, they are collapsed with
|
.data |
A data frame or a list. If |
.sep |
The separator to use when collapsing multiple strings passed in
|
.vectorized |
If |
.partials |
A named list with partial templates. See
|
Value
A character vector of length 1 if .vectorized
is FALSE
or a
character vector of the same length as the number of rows or items in
.data
if .vectorized
is TRUE
.
See Also
Other Mustache-style template functions:
ui_epoxy_mustache()
Examples
# The canonical mustache example
epoxy_mustache(
"Hello {{name}}!",
"You have just won {{value}} dollars!",
"{{#in_ca}}",
"Well, {{taxed_value}} dollars, after taxes.",
"{{/in_ca}}",
.data = list(
name = "Chris",
value = 10000,
taxed_value = 10000 - (10000 * 0.4),
in_ca = TRUE
)
)
# Vectorized over the rows of .data
epoxy_mustache(
"mpg: {{ mpg }}",
"hp: {{ hp }}",
"wt: {{ wt }}\n",
.data = mtcars[1:2, ]
)
# Non-vectorized
epoxy_mustache(
"mpg: {{ mpg }}",
"hp: {{ hp }}",
"wt: {{ wt }}",
.data = mtcars[1:2, ],
.vectorized = FALSE
)
# With mustache partials
epoxy_mustache(
"Hello {{name}}!",
"{{> salutation }}",
"You have just won {{value}} dollars!",
"{{#in_ca}}",
"Well, {{taxed_value}} dollars, after taxes.",
"{{/in_ca}}",
.partials = list(
salutation = c("Hope you are well, {{name}}.")
),
.sep = " ",
.data = list(
name = "Chris",
value = 10000,
taxed_value = 10000 - (10000 * 0.4),
in_ca = TRUE
)
)