-
Notifications
You must be signed in to change notification settings - Fork 978
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
dplyr::mutate shadow copy #5557
Comments
I think that happens because library(lobstr)
library(data.table)
d = data.table(x = 1:4) %>% mutate(y = x)
obj_addrs(d)
[1] "0x26388b06688" "0x26388b069c8" You'll notice that the addresses for both as.data.table(d)[x==1, y:=NA_integer_][] There was some discussion around detecting if other packages had modified |
Another way is to incorporate reference counting. This would be a big change, and not easy to handle well. For example environment browser panel in RStudio IDE creates references to objects from global env. AFAIK when you create a variable in RStudio, it has 2 references instead of one. |
I have only skimmed the dplyr code but I don't think this is on them. This seems to be just standard R behaviour. One way to resolve this without reference counting would be to check for duplicate references in the top level list of data.table and then create copies for those. (A different way would be to nicely ask dplyr to sort this out for us. But that's really just a bandaid and I don't think they'd be too thrilled about this...) |
Yes, does not look like a library(dplyr)
library(lobstr)
library(data.table)
d = data.table(x = 1:4) %>% mutate(y = x)
obj_addrs(d)
#> [1] "0x107a85b48" "0x107a85b48"
df = data.frame(x = 1:4) %>% mutate(y = x)
obj_addrs(df)
#> [1] "0x1071cf9e0" "0x1071cf9e0" Created on 2024-07-07 with reprex v2.1.0 |
After create a new variable through
mutate(y = x)
, change y, x is also modified.Created on 2022-12-08 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: