Skip to content

names(dt) unexpectedly pass-by-reference #4573

Closed

Description

@myoung3

I've been using data.table for a while and I've never seen or expected names(x) to be pass-by-reference like this. Has it always been like this? This behavior seems unexpected, and that's coming from someone who is very comfortable with data.table's pass-by-reference semantics.

to be clear, what I expect to happen is for the object stored_names to be a static copy of the names of data.table when get_names() was called and assigned to stored_names, not a dynamic pass-by-reference to the current names of x.

get_names <- function(x){
  names_x <- names(x)
  names_x
}

dt <- data.table(y=3)

stored_names <- get_names(dt)
stored_names_original <- copy(stored_names)
stored_names
stored_names_original
dt[,z:=4]
stored_names
stored_names_original

stopifnot(identical(stored_names, stored_names_original))

output:

> get_names <- function(x){
+   names_x <- names(x)
+   names_x
+ }
> 
> dt <- data.table(y=3)
> 
> stored_names <- get_names(dt)
> stored_names_original <- copy(stored_names)
> stored_names
[1] "y"
> stored_names_original
[1] "y"
> dt[,z:=4]
> stored_names
[1] "y" "z"
> stored_names_original
[1] "y"
> 
> stopifnot(identical(stored_names, stored_names_original))
Error: identical(stored_names, stored_names_original) is not TRUE

SessionInfo

R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS:   /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.19.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] testthat_2.3.2    devtools_2.3.0    usethis_1.6.1     data.table_1.12.8

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2                 rstudioapi_0.11            magrittr_1.5              
 [4] rcmdcheck_1.3.3            pkgload_1.0.2              intervalaverage_0.0.0.9000
 [7] R6_2.4.1                   rlang_0.4.6                fansi_0.4.1               
[10] tools_3.6.2                pkgbuild_1.0.8             xopen_1.0.0               
[13] sessioninfo_1.1.1          cli_2.0.2                  withr_2.1.2               
[16] remotes_2.1.1              ellipsis_0.3.1             assertthat_0.2.1          
[19] rprojroot_1.3-2            digest_0.6.25              crayon_1.3.4              
[22] processx_3.4.1             callr_3.4.3                fs_1.3.1                  
[25] ps_1.3.0                   memoise_1.1.0              glue_1.4.1                
[28] compiler_3.6.2             desc_1.2.0                 backports_1.1.4           
[31] prettyunits_1.0.2

Activity

  1. smingerson commented on Jun 25, 2020

    @smingerson

    This is explained in the reference semantics vignette, relevant snippet below. This is the second case where they recommend to use copy

    When we store the column names on to a variable, e.g., DT_n = names(DT), and then add/update/delete column(s) by reference. It would also modify DT_n, unless we do copy(names(DT)).

    DT = data.table(x = 1L, y = 2L)
    DT_n = names(DT)
    DT_n
    # [1] "x" "y"
    
    ## add a new column by reference
    DT[, z := 3L]
    
    ## DT_n also gets updated
    DT_n
    # [1] "x" "y" "z"
    
    ## use `copy()`
    DT_n = copy(names(DT))
    DT[, w := 4L]
    
    ## DT_n doesn't get updated
    DT_n
    # [1] "x" "y" "z"
    
  2. myoung3 commented on Jun 25, 2020

    @myoung3
    ContributorAuthor

    Wow ok somehow I've missed that all that time. It still seems unexpected to me. Somehow a character vector being pass-by-refeerence seems way less expected than an object of class data.table being pass-by-reference. A character vector you expect to act like a standard R object rather than a pointer whereas data.tables being pointers is like the explicitly stated purpose of the package. I assume it's too late to change this behavior and since it's intentional I guess we can close this?

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions