[R] Generic Functions

Berton Gunter gunter.berton at gene.com
Thu Jan 5 22:28:15 CET 2006

Briefly, S4 classes and methods are entirely different -- and often do not
comfortably coexist with -- the older S3 class/method system (which really
isn't, since the classes of the objects aren't really guaranteed as one
would expect). 

Probably the best place to learn about S4 is The Green Book (Chambers:
PROGRAMMING WITH DATA). Also you can load the "methods" package and type
?Methods for a shorter overview. See also ?setClass.

S4 provides a lot better control and encapsulation than S3, but it also
makes considerably greater demands of the programmer. You can decide whether
you think the tradeoff is justified for your work.

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> Elizabeth Purdom
> Sent: Thursday, January 05, 2006 12:44 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Generic Functions
> Hi,
> I've been using the package "graph" in the BioConductor 
> assortment and 
> writing some functions based on its classes. My question is 
> not specific to 
> this package or BioConductor (I think), but it serves as a 
> useful example. 
> I recently wanted to look at the code for the function 
> "edgeMatrix" for the 
> class "graphNEL".
> Usually I would type
> 	> func.foo
> and the code for the function func for class foo would appear 
> (where func 
> and foo are edgeMatrix and graphNEL respectively). Similarly 
> I would type
> 	> methods(func)
> to see for what classes the function func is defined.
> However, these do not work for these functions (they are not 
> S3 functions I 
> am told, though I don't know what that means). After a great deal of 
> guessing and help.search requests, I finally found functions 
> that seem to work:
> 	> getMethod(func,"foo")
> 	> showMethods(func)
> I get the corresponding code and possible methods available.
> What is this about and is there a section in the R language 
> definition that 
> explains the difference?
> Similarly I'm use to the object oriented program described in the R 
> language online based on the command UseMethod:
> 	> func <- function(x, ...) UseMethod("func", x)
> Under this system, I could just create a function "func.foo" 
> and it would 
> work for my class "foo" -- most notably I would create a 
> print command 
> "print.foo" and it would just seamlessly work. However my function 
> "print.graphNEL", for example, never worked and I'm just now 
> guessing from 
> the pieces of documentation from R, that I have to set it up 
> differently 
> but it is not clear to me how. How can I add a method to an existing 
> function under this setup?
> On another note: even if these different functions are 
> internally quite 
> different, can't the functions everyone is already accustomed 
> to be made to 
> access the properties, rather than creating new, similar 
> functions? (what 
> is the need for a different function "showMethods" when a function 
> "methods" already exists? I have the same issue for slots 
> where I have to 
> use a function "slotNames" rather than the more commonly 
> known function 
> "names"). It becomes such a learning curve, that I shy away 
> from packages 
> that use new techniques in coding and stick with packages and 
> functions I 
> can comfortably dissect and personalize.
> Thank you for any assistance,
> Elizabeth Purdom
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html

More information about the R-help mailing list