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

Tom Short tshort.rlists at gmail.com
Tue Nov 23 16:53:05 CET 2010


On Tue, Nov 23, 2010 at 10:33 AM, Steve Lianoglou
<mailinglist.honeypot at gmail.com> wrote:
> Hi all,
>
> Can we include some functions/declarations so that data.table objects
> can be used in S4 classes?

Steve, S4 has always scared me off, but if we can do something easy
like that to help users, we probably should. Do we have to require any
other packages?

My only comment is related to the data.table(from) in the setAs
function. Do you want that to be as.data.table(from)? The latter is
probably faster but does less error checking and auto-conversions of
columns.

> 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
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>


More information about the datatable-help mailing list