Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get breaks .SDcols #2647

Open
MichaelChirico opened this issue Feb 27, 2018 · 1 comment
Open

get breaks .SDcols #2647

MichaelChirico opened this issue Feb 27, 2018 · 1 comment
Labels
programming parameterizing queries: get, mget, eval, env

Comments

@MichaelChirico
Copy link
Member

This may be related to #2036 and/or #1548. Sorry for the over-long example, the last command is the one that matters:

library(data.table)
iris = data.table(iris)
iris = iris[ , !'Species']
iris[ , (names(iris)) := lapply(.SD, function(x) {
  (x - mean(x))/sd(x)
})]

xnm = c('x1', 'x2', 'x3')
setnames(iris, c('y', xnm))
y = iris$y

iris[ , c('r', 'y') := .(y, NULL)]
all_cor = iris[ , sapply(.SD, cor, r), .SDcols = xnm]
active = names(which.max(abs(all_cor)))
path = seq(0, all_cor[active], length.out = 100L)
cor_path = sapply(path, function(beta) {
  iris[ , { print(length(.SD))
    sapply(.SD, cor, r - beta*get(active))
    }, .SDcols = xnm]
})

Despite explicitly setting .SDcols = xnm (i.e., x1:x3), .SD has picked up r again (as seen from nrow(cor_path) == 4L.

If we replace get(active) with its current value (x2), the code works as expected. The fix of #1548 (to set envir = as.environment(.SD)) doesn't work here.

@jangorecki
Copy link
Member

this might be related to evaluation frame in which .SDcols is evaluated, this should be changed to be evaluated in parent.frame rather than data.table frame.

@jangorecki jangorecki added this to the 1.12.9 milestone Mar 22, 2020
@jangorecki jangorecki added the programming parameterizing queries: get, mget, eval, env label Apr 5, 2020
@mattdowle mattdowle modified the milestones: 1.13.1, 1.13.3 Oct 17, 2020
@jangorecki jangorecki modified the milestones: 1.14.3, 1.14.5 Jul 19, 2022
@jangorecki jangorecki modified the milestones: 1.14.11, 1.15.1 Oct 29, 2023
@jangorecki jangorecki removed this from the 1.16.0 milestone Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
programming parameterizing queries: get, mget, eval, env
Projects
None yet
Development

No branches or pull requests

3 participants