I am trying to figure out if there is a good way of using glue()
in j
of a data.table:
library(data.table)
library(glue)
data(iris)
dt.iris <- data.table(iris)
dt.iris[, myText := glue('The species is {Species} with sepal length of {Sepal.Length}')]
# Error in eval(parse(text = text, keep.source = FALSE), envir) :
# object 'Species' not found
I can use it if I indicate .envir = .SD
:
dt.iris[, myText := glue('The species is {Species} with sepal length of {Sepal.Length}', .envir = .SD)]
# works OK
but I am wondering if I can find some way without adding this every time. Maybe something like that:
glue1 <- function(...) glue(..., .envir = ???)
Use
transform.data.table
(from data.table) giving an all data.table solution ormutate
(from dplyr) orfmutate
(from collapse) instead of[.data.table
. We will still get a data.table result if we provide a data.table input.