epoxy_transform_inline {epoxy} | R Documentation |
Epoxy Inline Transformer
Description
This epoxy transformer is heavily inspired by the inline formatters in the cli package. The syntax is quite similar, but epoxy's syntax is slightly different to accommodate reporting use cases.
To transform a template expression inline, you include a keyword, prefixed
with a dot (.
) that is used to format the value of the template expression
in place.
epoxy("It cost {.dollar 123456}.", .transformer = "inline") #> It cost $123,456.
The formatters, e.g. .dollar
in the example above, can be customized using
the arguments of epoxy_transform_inline()
. Pass a customized
scales::label_dollar()
to .dollar
to achieve a different transformation.
dollars_nzd <- scales::label_dollar(suffix = " NZD") epoxy( "It cost {.dollar 123456}.", .transformer = epoxy_transform_inline(.dollar = dollars_nzd) ) #> It cost $123,456 NZD.
Note that, unlike inline markup with cli, the text within the template expression, other than the keyword, is treated as an R expression.
money <- 123456 epoxy("It cost {.dollar money}.", .transformer = "inline") #> It cost $123,456.
You can also nest inline markup expressions.
money <- c(123.456, 234.567) epoxy("It will cost either {.or {.dollar money}}.", .transformer = "inline") #> It will cost either $123.46 or $234.57.
Finally, you can provide your own functions that are applied to the evaluated
expression. In this example, I add a .runif
inline formatter that generates
n
random numbers (taken from the template expression) and sorts them.
set.seed(4242) epoxy( "Here are three random percentages: {.and {.pct {.runif 3}}}.", .transformer = epoxy_transform_inline( .runif = function(n) sort(runif(n)) ) ) #> Here are three random percentages: 23%, 35%, and 99%.
Usage
epoxy_transform_inline(
...,
transformer = glue::identity_transformer,
.and = and::and,
.or = and::or,
.incr = sort,
.decr = function(x) sort(x, decreasing = TRUE),
.bytes = scales::label_bytes(),
.date = function(x) format(x, format = "%F"),
.time = function(x) format(x, format = "%T"),
.datetime = function(x) format(x, format = "%F %T"),
.dollar = scales::label_dollar(prefix = engine_pick("$", "$", "\\$")),
.number = scales::label_number(),
.comma = scales::label_comma(),
.ordinal = scales::label_ordinal(),
.percent = scales::label_percent(suffix = engine_pick("%", "%", "\\%")),
.pvalue = scales::label_pvalue(),
.scientific = scales::label_scientific(),
.uppercase = toupper,
.lowercase = tolower,
.titlecase = function(x) tools::toTitleCase(as.character(x)),
.sentence = function(x) `substr<-`(x, 1, 1, toupper(substr(x, 1, 1))),
.squote = function(x) sQuote(x, q = getOption("epoxy.fancy_quotes", FALSE)),
.dquote = function(x) dQuote(x, q = getOption("epoxy.fancy_quotes", FALSE)),
.strong = NULL,
.emph = NULL,
.code = NULL
)
Arguments
... |
Additional named inline transformers as functions taking at least
one argument. The evaluated expression from the template expression is
passed as the first argument to the function. The argument names must
include the leading |
transformer |
The transformer to apply to the replacement string. This
argument is used for chaining the transformer functions. By providing a
function to this argument you can apply an additional transformation after
the current transformation. In nearly all cases, you can let
|
.and |
The function to apply to |
.or |
The function to apply to |
.incr |
The function to apply to |
.decr |
The function to apply to |
.bytes |
The function to apply to |
.date |
The function to apply to |
.time |
The function to apply to |
.datetime |
The function to apply to |
.dollar |
The function to apply to |
.number |
The function to apply to |
.comma |
The function to apply to |
.ordinal |
The function to apply to |
.percent |
The function to apply to |
.pvalue |
The function to apply to |
.scientific |
The function to apply to |
.uppercase |
The function to apply to |
.lowercase |
The function to apply to |
.titlecase |
The function to apply to |
.sentence |
The function to apply to |
.squote |
The function to apply to |
.dquote |
The function to apply to |
.strong |
The function to apply to |
.emph |
The function to apply to |
.code |
The function to apply to |
Value
A function of text
and envir
suitable for the .transformer
argument of
glue::glue()
.
See Also
epoxy_transform()
, epoxy_transform_set()
Other epoxy's glue transformers:
epoxy_transform_html()
,
epoxy_transform()
Examples
revenue <- 0.2123
sales <- 42000.134
# ---- Basic Example with Inline Formatting --------------------------------
epoxy(
'{.pct revenue} of revenue generates {.dollar sales} in profits.'
)
# With standard {glue} (`epoxy_transform_inline()` is a glue transformer)
glue::glue(
'{.pct revenue} of revenue generates {.dollar sales} in profits.',
.transformer = epoxy_transform_inline()
)
# ---- Setting Inline Formatting Options ----------------------------------
# To set inline format options, provide `scales::label_*()` to the supporting
# epoxy_transform_inline arguments.
epoxy(
'{.pct revenue} of revenue generates {.dollar sales} in profits.',
.transformer = epoxy_transform_inline(
.percent = scales::label_percent(accuracy = 0.1),
.dollar = scales::label_dollar(accuracy = 10)
)
)
glue::glue(
'{.pct revenue} of revenue generates {.dollar sales} in profits.',
.transformer = epoxy_transform_inline(
.percent = scales::label_percent(accuracy = 0.1),
.dollar = scales::label_dollar(accuracy = 10)
)
)
# ---- Custom Inline Formatting ------------------------------------------
# Add your own formatting functions
search <- "why are cats scared of cucumbers"
epoxy_html(
"https://example.com?q={{ .url_encode search }}>",
.transformer = epoxy_transform_inline(
.url_encode = utils::URLencode
)
)