Learn R Programming

epoxy (version 1.0.0)

epoxy_mustache: 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:

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

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.

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.

See Also

Other Mustache-style template functions: ui_epoxy_mustache()

Examples

Run this code
# 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
  )
)


Run the code above in your browser using DataLab