Reputation: 10156
I wish to create a Reference Class that extends a data.table
.
The motivation being
data.table
with custom methods and mutable fieldsProblem is I have so far failed.
I tried:
MyDataTable <- setRefClass("MyDataTable",
methods = list(
clearCell = function(i, j) { # A trivial custom method - sets a cell to NA
.self[i, (j) := NA]
}
),
contains = "data.table"
)
MyDataTable(a = 1:26, b = letters)$clearCell(1, 1)
But got the error:
Error in envRefSetField(.Object, field, classDef, selfEnv, elements[[field]]) :
‘a’ is not a field in class “MyDataTable”
However, I was expecting something like:
a b
1: NA a
2: 2 b
3: 3 c
4: 4 d
5: 5 e
6: ... etc etc
What's going wrong?
Upvotes: 2
Views: 505
Reputation: 49448
You're not initializing your reference class object correctly. I don't know how contains
is supposed to work, but a simple field works:
MyDataTable <- setRefClass("MyDataTable",
fields = list(.dt = 'data.table'),
methods = list(initialize = function(...){
.self$.dt <- data.table(...)
},
clearCell = function(i, j){
.self$.dt[i, (j) := NA]
},
bracket = function(...){
.self$.dt[...]
}
))
MyDataTable(a = 1:5, b = letters[1:5])$clearCell(1,1)
# a b
#1: NA a
#2: 2 b
#3: 3 c
#4: 4 d
#5: 5 e
MyDataTable(a = 1:5, b = letters[1:5])$bracket(b == 'c', a := 10)
# a b
#1: 1 a
#2: 2 b
#3: 10 c
#4: 4 d
#5: 5 e
Upvotes: 1