-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Just encountered this...
idx = 5
DT = data.table(
ID = rep(1:3,each = 4),
flag = as.logical(rep(0:1, 6)),
N = 1:12,
count = 12:1
)
DT[ , dcast(.SD, ID ~ flag + N, value.var = 'count')
][ , {
idx = grep('^TRUE', names(.SD))
y1 = .SD[ , ..idx]
y2 = .SD[ , -..idx]
NULL
}]
Warning messages:
1: In[.data.table(.SD, , ..idx) :
Both 'idx' and '..idx' exist in calling scope. Please remove the '..idx' variable in calling scope for clarity.
2: In[.data.table(.SD, , -..idx) :
Both 'idx' and '..idx' exist in calling scope. Please remove the '..idx' variable in calling scope for clarity.
Yes, idx and ..idx exist in calling scope, but ..idx appears "down a level" (reverting to the analogy of .. as "up a level"), so the code as written seems pretty natural to me.
I get where the message comes from, and I'm not sure how much effort it's worth to fully account for this type of possible nesting, but wanted to raise attention anyway.
To get rid of this warning, I'm resorting to with = FALSE, so this is related to #2826
This becomes an error if there's no idx in globalenv:
rm(idx)
DT[ , dcast(.SD, ID ~ flag + N, value.var = 'count')
][ , {
idx = grep('^TRUE', names(.SD))
y1 = .SD[ , ..idx]
y2 = .SD[ , -..idx]
NULL
}]
Error in
[.data.table(DT[, dcast(.SD, ID ~ flag + N, value.var = "count")], :
Variable 'idx' is not found in calling scope. Looking in calling scope because this symbol was prefixed with .. in the j= parameter.