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

Matthew Dowle mdowle at mdowle.plus.com
Tue Nov 23 19:32:17 CET 2010


Hi Tom,

About as.data.table I had to remind myself about auto-conversion.

data.table(df) drops through to as.data.table() anyway so doesn't do
auto-conversions of character to factor. There is some overhead checking
names and probably a copy is taken, so yes agree as.data.table is the
right one for setAs.

It seems it's just the 'data.table(letters[1:5],letters[6:10])' form that
does do auto-conversion of atomic character vectors to factor. Should we
remove the auto-conversion in that form then, for consistency? I think we
said setkey() would be the only place auto-conversion happens going
forward since that's where it's required.

Matthew


> 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
>>
> _______________________________________________
> 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