Let us take a simple example
data <- data.table::data.table(a = 1:10, b = 2:11)
j <- quote(c("c") := list(a + 1))
data[, j, env = list(j = j)][]
# a b c
# <int> <int> <num>
# 1: 1 2 2
# 2: 2 3 3
# 3: 3 4 4
# 4: 4 5 5
# 5: 5 6 6
The above works and produces the correct output. However if I place this inside a function I get a very different output.
data <- data.table::data.table(a = 1:5, b = 2:6)
test <- function(data, ...) {
dots <- eval(substitute(alist(...)))
j <- call(":=", call("c", names(dots)), unname(dots))
print(j)
data[, j, env = list(j = j)][]
}
test(data = data, c = a + 1)
# `:=`(c("c"), list(a + 1))
# a b c
# <int> <int> <list>
# 1: 1 2 <call[3]>
# 2: 2 3 <call[3]>
# 3: 3 4 <call[3]>
# 4: 4 5 <call[3]>
# 5: 5 6 <call[3]>
I assume that the c = a + 1
is just not being evaluated in the correct environment (i.e. the data.table
itself).
EDIT: I am using data.table 1.14.3