Search code examples
rdata.tablereference-class

Call ReferenceClass methods in data.table's :=


I am having problems to call methods of ReferenceClass objects and assign the return values directly by reference to a data.table column (data.table version 1.9.3, R version 3.1) as this minimal example shows:

RF <- setRefClass(
  Class = "RF",
  methods = list(
    get = function() { "/foo/bar" }
  )
)
rf <- RF$new()

mdt <- data.table( x= c("a", "b"), y = 1:2 )
mdt[ , z := rf$get() ] # gives warning

> mdt$z
[[1]]
`$`

[[2]]
rf

mdt[ , rf$get() ] works as expected, while mdt[ , list( z = rf$get()) ][ , z] gives a weird result as well and mdt[ , unlist(list( z = rf$get())) ] gives an error.

I do not need a solution like evaluate rf$get() outside mdt and then assign the result. I'd rather like to understand what is going on here, since I am making vast use of data.tables and ReferenceClasses and would like to use them properly together.


Solution

  • Thanks for filing the issue. This has been now fixed in 1.9.5. From NEWS:

    j-expressions in DT[, col := x$y()] (or) DT[, col := x[[1]]()] are now (re)constructed properly. Thanks to @ihaddad-md for reporting. Closes #774.

    Your code gives the result:

    #    x y        z
    # 1: a 1 /foo/bar
    # 2: b 2 /foo/bar
    

    without any warnings.