Closed Yunuuuu closed 11 months ago
I'm not sure what you're expecting here. But please note that rlang::duplicate()
is for expert usage only. It's a direct wrapper around the C-level function of the R API which most certainly works as expected according to its specifications.
Thank you for the clarification. I understand that rlang::duplicate()
is a powerful function that directly wraps a C-level function in the R API. I mean the names of dt1
shouldn't change when I change dt2 by reference just like what dt3
worked. dt3
is a data.table created via dt1[TRUE]
which has been revealed to do shallow copy.
dt2
has removed the name "mpg" in dt1
dt2[, mpg := NULL]
x
#> [1] "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
names(dt1)
#> [1] "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
It's not that it's powerful, it's that it's extremely low level, and you shouldn't expect any high level semantics from it. Also it's mostly meant for prototyping C code from R and should generally not be used in user or package code.
dt2 has removed the name "mpg" in dt1
Yes since it's a shallow copy the attributes list is not duplicated, which means that if you modify the names in place later on you'll get side effects. It's all working as expected from the perspective of these very low level tools.
I have followed this thread to use shallow copy https://github.com/Rdatatable/data.table/issues/3214#issuecomment-462490046
Created on 2023-11-06 with reprex v2.0.2