Assigning variables after load of DT when in slot of class

Open
#1,969 0 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 reproducible R example in the issue with data.table 1.10.0, focusing on the saved S4 object whose x slot contains a data.table. Compare := after load with the working $ and reassignment cases; done means adding V2 through := persists in test@x after loading.

Written by the indexing model from the issue text.

Description

by-reference

Hey,

I ran into a problem with assignments from a loaded data table that was a slot object of another class. I saw this issue prior https://github.com/Rdatatable/data.table/issues/1478 but I though that this was different enough to warrant a new report as the normal methods did not work for my case.

rm(list=ls())
library(data.table)

# create new class with a single slot that is a data table
objDT <- setClass("objDT", slots=c(x="data.table"))

# assign new variable
test <- objDT(x=as.data.table(sleep))
test@x[,V1:=rnorm(nrow(test@x))]
head(test@x)
# extra group ID          V1
# 1:   0.7     1  1 -0.78501901
# 2:  -1.6     1  2  1.94554492
# 3:  -0.2     1  3 -0.28007910
# 4:  -1.2     1  4 -0.01913156
# 5:  -0.1     1  5  0.27176328
# 6:   3.4     1  6 -0.24678313

# save to a temp file
tf1 <- tempfile()
save(test, file = tf1)

# remove everything except temp file reference
rm(list=setdiff(ls(), "tf1"))

# load and try adding a column to the data table
load(tf1)
test@x[,V2:=rnorm(nrow(test@x))]
# no v2 here
head(test@x)
# extra group ID          V1
# 1:   0.7     1  1 -0.78501901
# 2:  -1.6     1  2  1.94554492
# 3:  -0.2     1  3 -0.28007910
# 4:  -1.2     1  4 -0.01913156
# 5:  -0.1     1  5  0.27176328
# 6:   3.4     1  6 -0.24678313

# try again using DT's fix
rm(list=setdiff(ls(), "tf1"))
load(tf1)
truelength(test@x)
setDT(test@x)
truelength(test@x) == 0
# [1] TRUE
# still no luck
test@x[,V2:=rnorm(nrow(test@x))]
head(test@x)
# extra group ID         V1
# 1:   0.7     1  1  0.5541635
# 2:  -1.6     1  2 -0.2843569
# 3:  -0.2     1  3 -0.5678257
# 4:  -1.2     1  4 -1.3885973
# 5:  -0.1     1  5 -0.6021489
# 6:   3.4     1  6 -0.8610402

# this works
test@x$V2 <- rnorm(nrow(test@x))
head(test@x)
# extra group ID         V1         V2
# 1:   0.7     1  1  0.5541635 -0.1777580
# 2:  -1.6     1  2 -0.2843569  0.4318110
# 3:  -0.2     1  3 -0.5678257 -0.5022729
# 4:  -1.2     1  4 -1.3885973  0.6393736
# 5:  -0.1     1  5 -0.6021489 -0.1264319
# 6:   3.4     1  6 -0.8610402 -0.4931743

# so does this
test@x <- as.data.table(test@x)
test@x[,V3:=rnorm(nrow(test@x))]
head(test@x)
# extra group ID         V1         V2          V3
# 1:   0.7     1  1  0.5541635 -0.1777580  1.13936304
# 2:  -1.6     1  2 -0.2843569  0.4318110 -1.07292471
# 3:  -0.2     1  3 -0.5678257 -0.5022729  0.26723085
# 4:  -1.2     1  4 -1.3885973  0.6393736  0.96776500
# 5:  -0.1     1  5 -0.6021489 -0.1264319 -0.01993914
# 6:   3.4     1  6 -0.8610402 -0.4931743  1.77553716

here is my session info

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

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

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

other attached packages:
[1] data.table_1.10.0

loaded via a namespace (and not attached):
[1] tools_3.3.2

Thanks

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.