super

super is a fork / reimplementation of the glue package with a focus on efficiency and simplicity at a cost of flexibility.

As of the 0.0.1 release it should be considered ‘experimental’.

Differences from glue

Examples

library(super)

Simple concatenation

bar <- "baz"
glue("foo{bar}")
#> [1] "foobaz"

list-like input

dat <- head(cbind(car = rownames(mtcars), mtcars))
glue("{car} does {mpg} mpg.", dat)
#> [1] "Mazda RX4 does 21 mpg."           "Mazda RX4 Wag does 21 mpg."      
#> [3] "Datsun 710 does 22.8 mpg."        "Hornet 4 Drive does 21.4 mpg."   
#> [5] "Hornet Sportabout does 18.7 mpg." "Valiant does 18.1 mpg."          

Trimmed output

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)
#> My name is Fred,
#> my age next year is 50,
#> my anniversary is 1991-10-12.

Partially vectorised

Over embraced arguments

head(glue("Item {LETTERS}"))
#> [1] "Item A" "Item B" "Item C" "Item D" "Item E" "Item F"

But not over input strings (yet)

tryCatch(
    glue(letters),
    error = function(e) conditionMessage(e)
)
#> [1] "`x` must be a character vector of length <= 1."

Relative timing benchmarks

library(microbenchmark)

Simple concatenation

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"
)
#> Unit: relative
#>     expr       min        lq      mean    median        uq        max neval
#>  sprintf  1.000000  1.000000  1.000000  1.000000  1.000000  1.0000000   100
#>   paste0  2.866290  2.569132  2.159127  2.329529  2.231865  0.2971673   100
#>    super  9.414313  8.375402  7.268404  7.667618  7.229275  3.5622662   100
#>     glue 75.150659 66.176849 54.693050 59.325250 54.804404 11.8180118   100

Data frame input

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"
)
#> Unit: relative
#>     expr       min        lq      mean    median        uq       max neval
#>  sprintf  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000   100
#>   paste0  1.649578  1.578361  1.521773  1.543841  1.499143  1.391470   100
#>    super  2.797648  2.688480  2.664031  2.631524  2.577517  4.938053   100
#>     glue 17.433052 16.299423 15.545597 15.687109 14.964364 15.086037   100

Trimmed output

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"
)
#> Unit: relative
#>   expr      min       lq     mean   median       uq      max neval
#>  super 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000   100
#>   glue 3.923181 3.873648 3.716813 3.764615 3.730457 1.699942   100

Vectorized performance

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"
)
#> Unit: relative
#>     expr      min        lq     mean   median       uq       max neval
#>  sprintf 1.339721 1.2750059 1.280832 1.281514 1.268931 1.0520881   100
#>   paste0 1.007447 0.9996978 1.005688 1.003862 1.001624 0.9811512   100
#>    super 1.000000 1.0000000 1.000000 1.000000 1.000000 1.0000000   100
#>     glue 1.161911 1.1459430 1.165390 1.147413 1.202649 0.9998095   100