epoxy_mustache {epoxy}R Documentation

Mustache-style string interpolation

Description

[Experimental] 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:

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 .sep ("\n" by default).

.data

A data frame or a list. If .data is a data frame, epoxy_mustache() will transform the data frame so that the template can be applied to each row of the data frame. To avoid this transformation, wrap the .data value in I().

.sep

The separator to use when collapsing multiple strings passed in ... into a single template. Defaults to "\n".

.vectorized

If TRUE , epoxy_mustache() will vectorize over the items in .data. In other words, each item or row of .data will be used to render the template once. By default, .vectorized is set to TRUE if .data is a data frame and FALSE otherwise.

.partials

A named list with partial templates. See whisker::whisker.render() or the mustache documentation for details.

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
  )
)



[Package epoxy version 1.0.0 Index]