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

shallow copy don't work as expected #1666

Closed
Yunuuuu opened this issue Nov 6, 2023 · 4 comments
Closed

shallow copy don't work as expected #1666

Yunuuuu opened this issue Nov 6, 2023 · 4 comments

Comments

@Yunuuuu
Copy link

Yunuuuu commented Nov 6, 2023

I have followed this thread to use shallow copy Rdatatable/data.table#3214 (comment)

  dt1 <- data.table::as.data.table(mtcars)
  (x <- names(dt1))
  # for shallow copy, mpg shouldn't be removed
  dt2 <- rlang::duplicate(dt1, shallow = TRUE)
  # for shallow copy, mpg shouldn't be removed
  dt3 <- dt1[TRUE]
  dt4 <- rlang::duplicate(dt1)

  dt4[, mpg := NULL]
  x
  names(dt1)
  dt3[, mpg := NULL]
  x
  names(dt1)
  dt2[, mpg := NULL]
  x
  names(dt1)
dt1 <- data.table::as.data.table(mtcars)
(x <- names(dt1))
#>  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
#> [11] "carb"
# for shallow copy, mpg shouldn't be removed
dt2 <- rlang::duplicate(dt1, shallow = TRUE)
# for shallow copy, mpg shouldn't be removed
dt3 <- dt1[TRUE]
dt4 <- rlang::duplicate(dt1)

dt4[, mpg := NULL]
x
#>  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
#> [11] "carb"
names(dt1)
#>  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
#> [11] "carb"
dt3[, mpg := NULL]
x
#>  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
#> [11] "carb"
names(dt1)
#>  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
#> [11] "carb"
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"

Created on 2023-11-06 with reprex v2.0.2

@lionel-
Copy link
Member

lionel- commented Nov 6, 2023

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.

@Yunuuuu
Copy link
Author

Yunuuuu commented Nov 6, 2023

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.

@Yunuuuu
Copy link
Author

Yunuuuu commented Nov 6, 2023

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"

@lionel-
Copy link
Member

lionel- commented Nov 6, 2023

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.

@lionel- lionel- closed this as completed Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants