Modify by reference fails with magrittr if `colnames()<-` was used before

Open
#4,827 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by running the reproducer with data.table and magrittr, comparing the colnames<-, attr<-, and direct data.table cases. Trace the data.table handling behind the Invalid .internal.selfref warning and add a regression test for the expected by-reference update after colnames<-. Done means the magrittr expression updates the original object consistently without an unexpected warning.

Written by the indexing model from the issue text.

Description

by-reference

This is a weird one that I hadn't encounter before. If

Normal data.table; modify by reference works with magrittr.

library(data.table)
library(magrittr)

data <- data.table(a = 1)

data %>% 
  .[, b := 2]

data$b
#> [1] 2

Now add a call to colnames()<-. Using magrittr now doesn't modify by reference, but if the resulting object is assigned, it does work

data <- data.table(a = 1)

colnames(data) <- "a"
data %>% 
  .[, b := 2]

data$b
#> NULL

data_2 <- data %>% 
  .[, b := 2]

data_2$b
#> [1] 2

However, using regular data.table syntax does modify by reference.

data[, b := 2]

data$b
#> [1] 2

Related to that, when using attr()<-, data.table throws the expected warning. It says that the problem was fixed, but, in fact, it's not! :(

data <- data.table(a = 1)

attr(data, "a") <- "a"
data %>% 
  .[, b := 2]
#> Warning in `[.data.table`(., , `:=`(b, 2)): Invalid .internal.selfref detected
#> and fixed by taking a (shallow) copy of the data.table so that := can add this
#> new column by reference. At an earlier point, this data.table has been copied
#> by R (or was created manually using structure() or similar). Avoid names<- and
#> attr<- which in R currently (and oddly) may copy the whole data.table. Use set*
#> syntax instead to avoid copying: ?set, ?setnames and ?setattr. If this message
#> doesn't help, please report your use case to the data.table issue tracker so the
#> root cause can be fixed or this message improved.
data$b
#> NULL
Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                         
#>  version  R version 4.0.3 (2020-10-10)  
#>  os       elementary OS 5.1.7 Hera      
#>  system   x86_64, linux-gnu             
#>  ui       X11                           
#>  language en_GB:en                      
#>  collate  en_GB.UTF-8                   
#>  ctype    en_GB.UTF-8                   
#>  tz       America/Argentina/Buenos_Aires
#>  date     2020-12-02                    
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                            
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.0.3)                    
#>  callr         3.5.1      2020-10-13 [1] CRAN (R 4.0.3)                    
#>  cli           2.2.0      2020-11-20 [1] CRAN (R 4.0.3)                    
#>  crayon        1.3.4.9000 2020-11-11 [1] Github (r-lib/crayon@4bceba8)     
#>  data.table  * 1.13.2     2020-10-19 [1] CRAN (R 4.0.3)                    
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 4.0.2)                    
#>  devtools      2.3.2      2020-09-18 [1] CRAN (R 4.0.2)                    
#>  digest        0.6.27     2020-10-24 [1] CRAN (R 4.0.3)                    
#>  ellipsis      0.3.1      2020-05-15 [1] RSPM (R 4.0.2)                    
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.0.2)                    
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 4.0.2)                    
#>  fs            1.5.0      2020-07-31 [1] CRAN (R 4.0.2)                    
#>  glue          1.4.2      2020-08-27 [1] CRAN (R 4.0.2)                    
#>  highr         0.8        2019-03-20 [1] CRAN (R 4.0.2)                    
#>  htmltools     0.5.0.9003 2020-11-25 [1] Github (rstudio/htmltools@636b95e)
#>  knitr         1.30.2     2020-11-25 [1] Github (yihui/knitr@a00710b)      
#>  magrittr    * 2.0.1      2020-11-17 [1] CRAN (R 4.0.3)                    
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 4.0.2)                    
#>  pkgbuild      1.1.0      2020-07-13 [1] CRAN (R 4.0.2)                    
#>  pkgload       1.1.0      2020-05-29 [1] CRAN (R 4.0.2)                    
#>  prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.0.2)                    
#>  processx      3.4.4      2020-09-03 [1] CRAN (R 4.0.2)                    
#>  ps            1.4.0      2020-10-07 [1] CRAN (R 4.0.3)                    
#>  R6            2.5.0      2020-10-28 [1] CRAN (R 4.0.3)                    
#>  remotes       2.2.0      2020-07-21 [1] CRAN (R 4.0.2)                    
#>  rlang         0.4.8.9002 2020-11-25 [1] Github (r-lib/rlang@b4e28cb)      
#>  rmarkdown     2.5        2020-10-21 [1] CRAN (R 4.0.3)                    
#>  rprojroot     2.0.2      2020-11-15 [1] CRAN (R 4.0.3)                    
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.0.2)                    
#>  stringi       1.5.3      2020-09-09 [1] CRAN (R 4.0.2)                    
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.0.2)                    
#>  testthat      3.0.0.9000 2020-11-23 [1] Github (r-lib/testthat@45a9c70)   
#>  usethis       1.6.3      2020-09-17 [1] CRAN (R 4.0.2)                    
#>  withr         2.3.0      2020-09-22 [1] CRAN (R 4.0.3)                    
#>  xfun          0.19       2020-10-30 [1] CRAN (R 4.0.3)                    
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.0.2)                    
#> 
#> [1] /home/elio/R/x86_64-pc-linux-gnu-library/4.0
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.