[datatable-help] Weird behavior with S4 subclasses of data.table after loading RCurl

Jeffrey Arnold jeffrey.arnold at gmail.com
Tue Oct 2 05:17:34 CEST 2012


Hi Steve,

For some reason I didn't think to look at the tests; I did take a browse at
AllS4.R to see what was being defined. At least from my perspective, there
isn't anything you have to do. At this point, I know what the problem is
and how to work around it.  There shouldn't be problems when data.table
objects are used as slots for S4 classes, because R will know it is an S3
object and only look at x when dispatching [.

But it really appears to me that as data.table is written, there is no way
for S4 classes directly extending data.table to use [ with the exact same
syntax. As far as I can tell (and I could be wrong), as long as data.table
allows for unquoted expressions in i and j, the S4 method [ will never be
able to work. But there is too much code that relies on that syntax for
data.table to change it, so there really isn't much that can be done.  The
next best thing is adding the S4 definitions for [ methods that at least
allow the use of quoted expressions, and then warn people about in the FAQ.

I think the following method definitions will catch any case with quoted
expressions in i or j and make sure they get handled correctly. I can send
a true patch and write some test cases if you want.

setMethod("[", c(x="data.table", i="language", j="missing"),
          function(x, i, j, ...) data.table(x)[i=eval(i), ...])

setMethod("[", c(x="data.table", i="missing", j="language"),
          function(x, i, j, ...) data.table(x)[j=eval(j), ...])

setMethod("[", c(x="data.table", i="language", j="language"),
          function(x, i, j, ...) data.table(x)[i=eval(i), j=eval(j), ...])

Jeff

On Mon, Oct 1, 2012 at 2:26 PM, Steve Lianoglou <
mailinglist.honeypot at gmail.com> wrote:

> Hi Jeffrey,
>
> On Mon, Oct 1, 2012 at 1:39 PM, Jeffrey Arnold <jeffrey.arnold at gmail.com>
> wrote:
> > (Sorry, Steve; I realized that I originally replied to you instead of the
> > list)
> [snip]
> > I hadn't realized that I was doing something unintended when I started,
> or
> > maybe I wouldn't have :-)
>
> Actually, it wasn't so unintended after all -- I had written a trivial
> test (inst/tests/test-S4.R) to see that we could inherit (contains)
> from data.table, but I never kicked the tires with "[" and stuff, so
> ...
>
> >  Now R supports S4 classes inheriting from S3
> > classes pretty well, so it seemed like a good idea at the time.   The S4
> > class I am actually writing is for storing / manipulating MCMC samples.
> One
> > way to do that is to have a data.frame like object with specific columns,
> > e.g. "chain", "iteration", "parameter", ..., and then add functions that
> > take advantage of this known structure.  I want to inherit from the
> > data.frame directly so that it can make use of all the generic functions
> > defined for the data.frame.  It is more intuitive to use object[...]
> rather
> > than object at someSlotName[...].That all works great, except that these
> get
> > samples can get pretty big, so, of course, I want the performance of
> > data.table :-) if I can have it.
>
> I agree that it would be handy to do what you want this way ... I am
> unfortunately a bit short on time to help you dig into this at the
> moment.
>
> I think the "[" methods you are defining for data.table are on the
> right track -- perhaps it will be a good idea to include these into
> the data.table package and export them. Still, one package
> simultaneously (and fully) supporting s3 and s4 might be a bit ...
> something.
>
> -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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20121001/a0068bda/attachment-0001.html>


More information about the datatable-help mailing list