Issue
Consider the following reprex:
library(data.table)
f <- function(l) l[, t := 1]
d <- data.table(i = 1:2, l = list(data.table(x=1), data.table(x=2)))
k <- "l"
lapply(d[, ..k][, l], f)
will raise the following warning
In `[.data.table`(l, , `:=`(t, 1)) :
Invalid .internal.selfref detected and fixed by taking a (shallow) copy of the data.table so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or was created manually using structure() or similar). Avoid names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. If this message doesn't help, please report your use case to the data.table issue tracker so the root cause can be fixed or this message improved.
Same results for
lapply(d[, .SD, .SDcols = "l"][, l], f)
while the following code does not issue a warning but has the severe consequence that I have to hard code the column names:
lapply(d[, .(l)][, l], f)
lapply(d[, l], f)
Use Case
In my real code I want to use purrr::pmap on a data.table, but the data.table contains more columns than the function has arguments. I could add ... to capture the unnecessary columns (which I will do as a workaround now). But my attempt to filter the data.table via ..necessary_cols upfront, failed and it took me frankly quite a while to hunt the error down.
Thus, overall I am looking for a solution where I can select a dynamic subset of columns (..x would do the trick but does throw the warning) in conjunction with a lapply alike function.
Session Info
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
attached packages:
[1] data.table_1.13.2
loaded via a namespace (and not attached):
[1] compiler_3.6.3
Issue
Consider the following
reprex:will raise the following warning
Same results for
while the following code does not issue a warning but has the severe consequence that I have to hard code the column names:
Use Case
In my real code I want to use
purrr::pmapon adata.table, but thedata.tablecontains more columns than the function has arguments. I could add...to capture the unnecessary columns (which I will do as a workaround now). But my attempt to filter thedata.tablevia..necessary_colsupfront, failed and it took me frankly quite a while to hunt the error down.Thus, overall I am looking for a solution where I can select a dynamic subset of columns (
..xwould do the trick but does throw the warning) in conjunction with alapplyalike function.Session Info