litedown::reactor(print = NA)
super is a fork / reimplementation of the glue package with a focus on efficiency and simplicity at a cost of flexibility.
::: {.callout-important data-legend="Differences from glue"}
super::glue()
takes only two arguments: A character string that is used as
a template and an environment to look up the embraced inputs. There is no
option to change the delimiters from being a pair of braces (e.g. {name}
).
Embraced inputs are always looked up in the provided environment (akin to
calling get()
) and are not evaluated. This is similar to glue::glue_safe()
and glue::glue_data_safe()
but note that this is not the default behaviour
of glue::glue()
.
super::glue()
does not trim inputs. Instead this is done by either the
function super::glut()
or explicitly by glue(trim(x))
.
super::glue()
returns an unclassed character vector whereas glue::glue()
returns a <glue>
object and provides a variety of methods to work on that
object.
At present, super::glue()
does not provide any allowance for different
handling of NA
and NULL
values, instead following the behaviour of
paste()
.
Recycling of embraced arguments currently follows standard R recycling rules where the arguments will be recycled to the length of the longest one.
glue::glue()
arguments (and their associated behaviours) .comment
,
.literal
and .transformer
are not implemented.
:::
library(super)
bar <- "baz" glue("foo{bar}")
dat <- head(cbind(car = rownames(mtcars), mtcars)) glue("{car} does {mpg} mpg.", dat)
name <- "Fred" age <- 50 anniversary <- as.Date("1991-10-12") out <- glut(" My name is {name}, my age next year is {age}, my anniversary is {anniversary}. ") cat(out)
Over embraced arguments
head(glue("Item {LETTERS}"))
But not over input strings (yet)
glue(letters)
library(microbenchmark)
bar <- "baz" bob <- 20 microbenchmark( sprintf = sprintf("foo%s %d", bar, bob), paste0 = paste0("foo", bar, " ", bob), super = super::glue("foo{bar} {bob}"), glue = as.character(glue::glue_safe("foo{bar} {bob}", .trim = FALSE)), unit = "relative", check = "identical" )
dat <- head(cbind(car = rownames(mtcars), mtcars)) microbenchmark( sprintf = with(dat, sprintf("%s does %.3g mpg.", car, mpg)), paste0 = with(dat, paste(car, "does", mpg, "mpg.")), super = super::glue("{car} does {mpg} mpg.", dat), glue = as.character(glue::glue_data(dat, "{car} does {mpg} mpg.")), unit = "relative", check = "identical" )
microbenchmark( super = super::glut(" My name is {name}, my age next year is {age}, my anniversary is {anniversary}. "), glue = as.character(glue::glue(" My name is {name}, my age next year is {age}, my anniversary is {anniversary}. ")), unit = "relative", check = "identical" )
For larger input with both glue::glue()
and super::glue()
, the performance
becomes dominated by the internally constructed call to paste0()
, hence the
convergence observed below.
bar <- rep("baz", 1e5) microbenchmark( sprintf = sprintf("foo%s %d", bar, bob), paste0 = paste0("foo", bar, " ", bob), super = super::glue("foo{bar} {bob}"), glue = as.character(glue::glue_safe("foo{bar} {bob}", .trim = FALSE)), unit = "relative", check = "identical" )
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.