1

This is very closely related to: Adding new columns to a data.table by-reference within a function not always working

How do you make setalloccol work on data tables (data.table_1.16.0) that are a field of a R6 object loaded from disk. For example, following closely the code from the linked post:

library(R6)
library(data.table)
testR6 <- R6Class("testR6", 
                   public = list(initialize = function(test_) {
                       self$test <- test_
                   }),
                   private = list(.test = data.table()),
                   active = list(test = function(val) if (missing(val)) private$.test else private$.test <- val
                                 )
)


foobar <- function(dt, col) {
    dt[, (col) := 1]
    invisible(dt)
}


test <- data.table(id = letters[1:2], val=1:2)
test_R6 <- testR6$new(test)
saveRDS(test_R6, "data_20251008_test_R6.rds")
test_R6_2 <- readRDS("data_20251008_test_R6.rds")

all.equal(test_R6, test_R6_2)
#[1] TRUE
truelength(test_R6$test)
# [1] 1026
truelength(test_R6_2$test)
# [1] 0

setalloccol(test_R6_2$test)
truelength(test_R6_2$test)
# [1] 0 ## DID NOT WORK!




foobar(test_R6$test, "new")
test_R6$test
#  id val new
#1: a   1   1
#2: b   2   1
foobar(test_R6_2$test, "new")
test_R6_2$test
# id   val
# <char> <int>
#     1:      a     1
# 2:      b     2

It is possible to use setDT on this simple data.table, and the subsequent call to foobar works, i.e.:

test_R6_3 <- readRDS("data_20251008_test_R6.rds")
setDT(test_R6_3$test)
truelength(test_R6_3$test)
# [1] 1026

This workaround doesn't work for my very complicated dataset (which I cannot easily reproduce here). I obtain a different type of error when I call setDT:

Error in stop() : argument "..1" is missing, with no default

1 Answer 1

0

Add:

test_R6_2$test <- as.data.table(test_R6_2$test)

Then test_R6_2 will yield the same result:

foobar(test_R6$test, "new")

test_R6$test
       id   val   new
   <char> <int> <num>
1:      a     1     1
2:      b     2     1

foobar(test_R6_2$test, "new")

test_R6_2$test
       id   val   new
   <char> <int> <num>
1:      a     1     1
2:      b     2     1
Sign up to request clarification or add additional context in comments.

1 Comment

This copies the object in memory though?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.