super

Overview

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

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)

glue(letters)
#> `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.000000   100
#>   paste0  2.782214  2.581320  2.489924  2.406657  2.151365  4.269248   100
#>    super  8.745917  8.348631  7.412320  7.793777  6.917494  4.105864   100
#>     glue 73.203267 66.622383 56.658633 60.714906 52.965261 20.190018   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.639398  1.605489  1.536967  1.556278  1.499879  1.063850   100
#>    super  2.707324  2.640144  2.575891  2.587211  2.470759  3.576688   100
#>     glue 17.022445 16.375381 15.450827 15.581622 14.816336 10.348410   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 4.150907 3.962442 3.808781 3.871885 3.847039 1.610143   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.258421 1.232784 1.245969 1.235738 1.231066 1.1916430   100
#>   paste0 1.000000 1.000000 1.000000 1.000000 1.000000 1.0000000   100
#>    super 1.018565 1.000552 0.997944 1.000508 1.000729 0.9185109   100
#>     glue 1.145637 1.142098 1.145423 1.142665 1.130846 1.1326411   100