I want to create a function that itself uses the awesome glue::glue
function.
However, I came to find myself dealing with some namespace issue when I want to glue a variable that exists in both function and global environments:
x=1
my_glue <- function(x, ...) {
glue::glue(x, ...)
}
my_glue("foobar x={x}") #not the expected output
# foobar x=foobar x={x}
I'd rather keep the variable named x
for package consistency.
I ended up doing something like this, which works pretty well so far but only postpone the problem (a lot, but still):
my_glue2 <- function(x, ...) {
x___=x; rm(x)
glue::glue(x___, ...)
}
my_glue2("foobar x={x}") #problem is gone!
# foobar x=1
my_glue2("foobar x={x___}") #very unlikely but still...
# foobar x=foobar x={x___}
Is there a better/cleaner way to do this?