You can use the syntax used in str_interp / perl / shell with glue if you like.
sh <- function(..., .envir = parent.frame()) {
glue::glue(..., .envir = .envir, .open = "${")
}
foo = "bar"
sh("foo = ${foo}")
#> foo = bar
You can also create custom transformers to do things like collapse lists / vectors in whatever way you wish. See http://glue.tidyverse.org/articles/transformers.html for more information.
library(glue)
collapse_transformer <- function(regex = "[*]$", ...) {
function(code, envir, data) {
if (grepl(regex, code)) {
code <- sub(regex, "", code)
}
res <- evaluate(code, envir, data)
collapse(res, ...)
}
}
glue("{1:5*}\n{letters[1:5]*}", .transformer = collapse_transformer(sep = ", ", last = " and "))
#> 1, 2, 3, 4 and 5
#> a, b, c, d and e