[R] questions on generic functions

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Jan 3 16:17:14 CET 2010



Wayne (Yanwei) Zhang wrote:
> Hi all,
> 
> I'm trying to build a package, but run into problems with generic functions. 
> 
> I have two classes "aa" and "bb", and defined the function "summary.bb". Now I 
> want to make "summary.bb" the default method for both classes "aa" and "bb". 

If you are using S3 approach, you can either define

summary.aa <- summary.bb

or make use of inheritance, i.e. you need to create object of class aa with

class(object.aa) <- c("aa", "bb")


> That is, if I have two objects  "object.aa" and "object.bb" from class "aa" and "bb" 
> respectively, the function call "summary(object.aa)" and "summary(object.bb)" will 
> apply the function "summary.bb" at default.  How can I do that? Should I use 
> "setMethod" ?  Thanks a lot. 


 From the naming scheme above I feel you are going the S3 way. If you 
want to use S4 (what setMethod is intended for), you can either use 
setMethod() for the declaration or also use inheritance (already in the 
class definition of class aa in setClass()).

Uwe Ligges


> Regards,
> Wayne
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list