[R] 2 setGeneric's, same name, different method signatures

Romain Fenouil fenouil at ciml.univ-mrs.fr
Wed Feb 20 11:19:05 CET 2013

Dear members,
please excuse me for eventual mistake in posting my first message on this

I am actually glad to read these very interesting questions and answers
because I have been facing a related situation for the last 2 days. I am
writing a R package that is using some of the interesting functions offered
by Dr. Morgan's 'ShortRead' library.

I have a C++/Java background and only used R as a scripting language until
I am now writing my first class with R and everything seems fine until I
tried to define a function (accessor) named 'chromosome' for it.
Indeed this function name is already used in ShortRead library as an
accessor to the corresponding slot.

My others accessors are usually defined like this :

# Getter
setGeneric(name="pairedEnds",	def=function(object, ...)
setMethod(	f="pairedEnds",
		definition=function(object, ...) {return(slot(object, "pairedEnds"))});

# Setter
setGeneric(name="pairedEnds<-",	def=function(object, value)
setReplaceMethod(	f="pairedEnds",
		definition=function(object, value) {object at pairedEnds<-value;
validObject(object); return(object);});

However, I realized that when I tried to do the same with the 'chromosome'
function, calling setGeneric overrides the previous 'definition' (the
'ShortRead' one) and if I understand correctly, masked it. The consequence
is that I cannot use chromosome anymore on a 'ShortRead' object

I think I understood that we can only call setGeneric once per function
name. By the way, if I ommit my setGeneric and only use the setMethod in my
package, it seems to work correctly, probably basing it on the setGeneric
call from the ShortRead library.

My questions are :

1. Is my last statement correct ?

2. How to define a function with the same name as one previously existing in
another package ? How to avoid conflicts in case we load these two packages
at the same time ?

3. In order to avoid several calls to setGeneric, I have been thinking about
a conditional use of setGeneric 'if(!is.generic(myFunc)) setGeneric(myFunc)'
but it seems tricky and I have never seen such use, I guess this would be

4. Is this phenomenon happening because I'm doing this in the global
environment (Testing) ? Would it be transparent when in the package
Namespace ?

This method dispatching is very disappointing to me... I believe that these
questions come from my general misunderstanding of how R OOP is designed and
I would be glad to find a reference document for S4.

Thank you very much for your help.

Romain Fenouil.

View this message in context: http://r.789695.n4.nabble.com/2-setGeneric-s-same-name-different-method-signatures-tp4658570p4659139.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list