Skip to content

Using names(obj) <- names(dt) could be problematic #5079

Open

Description

@renkun-ken

Since set* and := set by reference, it seems to alter the names() character vector by reference too. Then the assignment like names(obj) <- names(dt) might lead to unexpected error.

Consider the following example:

library(data.table)

dt <- data.table(x = 1:10, y = 10:1)
dt

p <- list(1, 2)
names(p) <- names(dt)

p

dt[, z := 1:10]

p
names(p)

p <- list(1, 2, 3)
names(p) <- names(dt)
p

setcolorder(dt, c("z", "y", "x"))
p
> library(data.table)
> dt <- data.table(x = 1:10, y = 10:1)
> dt
        x     y
    <int> <int>
 1:     1    10
 2:     2     9
 3:     3     8
 4:     4     7
 5:     5     6
 6:     6     5
 7:     7     4
 8:     8     3
 9:     9     2
10:    10     1

> p <- list(1, 2)
> names(p) <- names(dt)
> p
$x
[1] 1

$y
[1] 2


> dt[, z := 1:10]
'names' attribute [3] must be the same length as the vector [2]
> p
$x
[1] 1

$y
[1] 2

'names' attribute [3] must be the same length as the vector [2]
> names(p)
[1] "x" "y" "z"
'names' attribute [3] must be the same length as the vector [2]
> p <- list(1, 2, 3)
> names(p) <- names(dt)
> p
$x
[1] 1

$y
[1] 2

$z
[1] 3

> setcolorder(dt, c("z", "y", "x"))
> p
$z
[1] 1

$y
[1] 2

$x
[1] 3

I don't see a "don't do this" in wiki and the only thing about this is in the vignette Reference Semantics.

image

Should we somehow emphasize such problems more somewhere?

Activity

  1. iMissile commented on Aug 19, 2021

    @iMissile

    I have the same issue.
    It's not convenient that this object stays reference further even in the tibble/data.frame stack.
    This is a small sample with a crash.

    dt <- data.table::data.table(a = rnorm(100), b = rnorm(100))
    nt <- names(dt)
    df <- tibble::tibble(n = nt)
    .Internal(inspect(nt))
    #> @0x000000001891c840 16 STRSXP g0c7 [REF(65535)] (len=2, tl=1026)
    #>   @0x0000000012e09e48 09 CHARSXP g1c1 [MARK,REF(54),gp=0x61] [ASCII] [cached] "a"
    #>   @0x00000000131e2db8 09 CHARSXP g1c1 [MARK,REF(42),gp=0x61] [ASCII] [cached] "b"
    dt[, c := 1]
    nt
    #> [1] "a" "b" "c"
    .Internal(inspect(nt))
    #> @0x000000001891c840 16 STRSXP g0c7 [REF(65535)] (len=3, tl=1026)
    #>   @0x0000000012e09e48 09 CHARSXP g1c1 [MARK,REF(55),gp=0x61] [ASCII] [cached] "a"
    #>   @0x00000000131e2db8 09 CHARSXP g1c1 [MARK,REF(43),gp=0x61] [ASCII] [cached] "b"
    #>   @0x00000000045629f0 09 CHARSXP g1c1 [MARK,REF(332),gp=0x61] [ASCII] [cached] "c"
    df
    #> Error: Internal error in `df_slice()`: Columns must match the data frame size.
    
    sessionInfo()
    #> R version 4.1.0 Patched (2021-05-29 r80411)
    #> Platform: x86_64-w64-mingw32/x64 (64-bit)
    #> Running under: Windows 10 x64 (build 19043)
    #> 
    #> Matrix products: default
    #> 
    #> locale:
    #> [1] LC_COLLATE=Russian_Russia.1251  LC_CTYPE=Russian_Russia.1251   
    #> [3] LC_MONETARY=Russian_Russia.1251 LC_NUMERIC=C                   
    #> [5] LC_TIME=Russian_Russia.1251    
    #> 
    #> attached base packages:
    #> [1] stats     graphics  grDevices utils     datasets  methods   base     
    #> 
    #> loaded via a namespace (and not attached):
    #>  [1] knitr_1.33        magrittr_2.0.1    rlang_0.4.11      fansi_0.5.0      
    #>  [5] stringr_1.4.0     styler_1.5.1      highr_0.9         tools_4.1.0      
    #>  [9] data.table_1.14.0 xfun_0.25         utf8_1.2.2        withr_2.4.2      
    #> [13] htmltools_0.5.1.1 ellipsis_0.3.2    yaml_2.2.1        digest_0.6.27    
    #> [17] tibble_3.1.3      lifecycle_1.0.0   crayon_1.4.1      purrr_0.3.4      
    #> [21] vctrs_0.3.8       fs_1.5.0          glue_1.4.2        evaluate_0.14    
    #> [25] rmarkdown_2.10    reprex_2.0.1      stringi_1.7.3     compiler_4.1.0   
    #> [29] pillar_1.6.2      backports_1.2.1   pkgconfig_2.0.3

    Created on 2021-08-19 by the reprex package (v2.0.1)

  2. tlapak commented on Sep 2, 2021

    @tlapak
    Contributor

    Would it actually be terrible to have names and names<- always copy? I've only ever seen the current behaviour to lead to confusion without any real upside. There'd be some unnecessary allocations in existing code but the impact should be negligible?

  3. wesnm commented on Sep 21, 2021

    @wesnm

    Glad I'm not nuts or doing something dumb. Just noticed this yesterday, and I'm sure I've done it before. Has this always been the case? Going to have to go through all my code now.

  4. MilesMcBain commented on Aug 30, 2022

    @MilesMcBain

    Encountered this today, highly surprising.

    It's somewhat disturbing that the reference semantics are completely opaque. I.e. if I do str(names(DT)) the thing I get back appears to be a plain R character vector, with no indication that it has special reference semantics. Unlike say a classed object of data.table which is known to have this properties. Perhaps the names should have a special class?

  5. added this to the 2.0.0 milestone on Nov 24, 2023
  6. added
    by-reference
    on Jul 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    by-reference

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions