[R] Generic Functions

Elizabeth Purdom epurdom at stanford.edu
Thu Jan 5 21:44:15 CET 2006

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

More information about the R-help mailing list