diff --git a/R/data.table.R b/R/data.table.R index bdc92e0aa..f05220a62 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -2862,6 +2862,9 @@ set = function(x,i=NULL,j,value) # low overhead, loopable setalloccol(x, verbose=FALSE) if (is.name(name)) { assign(as.character(name), x, parent.frame(), inherits=TRUE) + } else if (is.call(name)) { + # handle subexpressions like self$dt, foo[["bar"]], etc. by evaluating the assignment in parent frame + eval(call("<-", name, x), parent.frame()) } } .Call(Cassign,x,i,j,NULL,value) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index b9d9a3ba5..53b7ea898 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21906,7 +21906,13 @@ null_in_value <- NULL test(2351.3, "cyl" %notin% names(DT[, cyl := null_in_value])) DT = unserialize(serialize(as.data.table(mtcars), NULL)) test(2351.4, ncol(DT[, c("cyl", "mpg") := .(null_in_value, null_in_value)]), ncol(mtcars) - 2L) -rm(DT) +l = list(DT = unserialize(serialize(as.data.table(mtcars), NULL))) +test(2351.5, set(l$DT, j="carb", value=NULL), as.data.table(mtcars)[,carb:=NULL]) +test(2351.6, "carb" %notin% names(l$DT)) +l = list(DT = unserialize(serialize(as.data.table(mtcars), NULL))) +test(2351.7, set(l[["DT"]], j="carb", value=NULL), as.data.table(mtcars)[,carb:=NULL]) +test(2351.8, "carb" %notin% names(l$DT)) +rm(DT, l) # rbindlist did not protect the temporary UTF-8 strings, #7452 DTn = apply(matrix(as.raw(rep(0xa1:0xff, length.out = 100)), 10), 2, rawToChar)