[datatable-help] Playing nice(r) with S4?

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Nov 23 16:33:46 CET 2010


Hi all,

Can we include some functions/declarations so that data.table objects
can be used in S4 classes?

One could do this simply by adding an "R/AllS4.R" file with the
following contents:

====== R/AllS4.R ======
setOldClass(c('data.table', 'data.frame'))

setAs("data.frame", "data.table", function(from) {
  data.table(from)
})

====================

Apparently there's no need to modify NAMESPACE. Now the following works:


R> df <- data.frame(a=1:10, b=11:20)
R> dt <- as(df, 'data.table') ## Via setAs(..)

And classes can include data.table objects:

R> setClass("Something",
         representation(name="character",
                        info="data.table"),
         prototype(name=character(),
                   info=data.table(height=0, weight=0)))
R> s = new("Something")

I currently have these two definitions/declarations embedded in a
package I'm developing that imports data.table, but I figure it makes
more sense to have in data.table itself ...

Thanks,
-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact


More information about the datatable-help mailing list