Skip to content

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

@tinygreen

Description

@tinygreen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions