From janko.thyson.rstuff at googlemail.com  Fri Jan 21 13:08:48 2011
From: janko.thyson.rstuff at googlemail.com (Janko Thyson)
Date: Fri, 21 Jan 2011 13:08:48 +0100
Subject: [Rd] setGeneric for residuals, etc
In-Reply-To: <u1ok4hzfvi5.fsf@lx-chrateresearch01.citadelgroup.com>
References: <u1ok4hzfvi5.fsf@lx-chrateresearch01.citadelgroup.com>
Message-ID: <4d39774c.825bdf0a.5ca4.ffffa40a@mx.google.com>

> -----Urspr?ngliche Nachricht-----
> Von: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-
> project.org] Im Auftrag von Johann Hibschman
> Gesendet: Donnerstag, 20. Januar 2011 16:25
> An: r-devel at stat.math.ethz.ch
> Betreff: [Rd] setGeneric for residuals, etc
> 
> I'm experimenting with a few model-fitting classes of my own.  I'm
> leaning towards using S4 for my classes, but the R functions I'd want
> to
> override (residuals, predict, etc.) are all S3 methods.
> 
> As I understand it, I could do setGeneric("residuals"), then add S4
> specializations to it.  However, I don't understand the full
> consequences of doing this.  Are there any drawbacks?  Is there some
> code that will not run properly if I do this?  It feels like I'm
> reaching in and modifying a core R function, which makes me nervous.
> 
> To put it another way, if it's completely transparent and causes no
> problems, why isn't it done by default?
> 
> (If it matters, I'm still on 2.10.1, but I can take it up with global
> architecture if there are compelling changes in later versions.)
> 
> Thanks,
> Johann
> 

Well, one "drawback" with S4 is that you need to write more explicit code
than in a pure S3 world, but it makes your code also a lot more robust
(method dispatch, validation etc.). As far as I understand it, working with
customized S4 objects would imply that you need to specify methods for all
those S3 functions already out there that you want to work with your
objects. For example, inside your method for 'residuals()', you will
probably just get some data out of a slot of your object and run the S3
function 'residuals(your.slot.data)'. So there's nothing that should make
you nervous in that respect, you're not overwriting anything with your
method. Setting a generic for an existing function is just a necessary step
in order to specify S4 methods for it. 

However, you could also write a couple methods that coerce your custom
object into those S3 objects that the already implemented functions expect
(e.g. 'residuals()'). Another drawback might be efficiency depending on how
complex your S4 objects are (e.g. how many nested function calls are
required to get to actual data).

HTH,
Janko

> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] setGeneric for residuals, etc
Janko Thyson