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

rename() and rename_with() trigger copy() despite immutable = FALSE #485

Open
tinygreen opened this issue Feb 27, 2025 · 0 comments · May be fixed by #486
Open

rename() and rename_with() trigger copy() despite immutable = FALSE #485

tinygreen opened this issue Feb 27, 2025 · 0 comments · May be fixed by #486

Comments

@tinygreen
Copy link

Even with lazy_dt(..., immutable = FALSE), a copy() is triggered by rename() and rename_with(). I would expect them to modify in place, like mutate() and relocate().

library(data.table)
library(dtplyr)
library(dplyr, warn.conflicts = FALSE)

# Simple sample data
dt <- data.table(a = 1:5, b = 6:10)
immutable <- lazy_dt(dt)
mutable <- lazy_dt(dt, immutable = FALSE)

# mutate() works as expected
immutable %>% mutate(c = a + b) %>% show_query()
#> copy(`_DT1`)[, `:=`(c = a + b)]
mutable %>% mutate(c = a + b) %>% show_query()
#> `_DT2`[, `:=`(c = a + b)]

# relocate() works as expected
immutable %>% relocate(b) %>% show_query()
#> setcolorder(copy(`_DT1`), c("b", "a"))
mutable %>% relocate(b) %>% show_query()
#> setcolorder(`_DT2`, c("b", "a"))

# rename() triggers copy
immutable %>% rename(A = a, B = b) %>% show_query()
#> setnames(copy(`_DT1`), c("a", "b"), c("A", "B"))
mutable %>% rename(A = a, B = b) %>% show_query()
#> setnames(copy(`_DT2`), c("a", "b"), c("A", "B"))

# rename_with() triggers copy
immutable %>% rename_with(toupper) %>% show_query()
#> setnames(copy(`_DT1`), toupper)
mutable %>% rename_with(toupper) %>% show_query()
#> setnames(copy(`_DT2`), toupper)

Created on 2025-02-27 with reprex v2.1.1

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

Successfully merging a pull request may close this issue.

1 participant