[R] 2 setGeneric's, same name, different method signatures
Martin Morgan
mtmorgan at fhcrc.org
Wed Feb 20 21:58:08 CET 2013
On 02/20/2013 02:19 AM, Romain Fenouil wrote:
> Dear members,
> please excuse me for eventual mistake in posting my first message on this
> list.
>
> 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
> now.
> 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, ...)
> {standardGeneric("pairedEnds")});
> setMethod( f="pairedEnds",
> signature="myClass",
> definition=function(object, ...) {return(slot(object, "pairedEnds"))});
>
> # Setter
> setGeneric(name="pairedEnds<-", def=function(object, value)
> {standardGeneric("pairedEnds<-")});
> setReplaceMethod( f="pairedEnds",
> signature="AlignedData",
> 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
> ('alignedRead').
actually, the original generic and its methods are still available with
ShortRead::chromosome
>
> 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 ?
maybe; you should have in your DESCRIPTION file
Imports: ShortRead
and in your NAMESPACE file
importFrom(ShortRead, chromosome)
> 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 ?
inside your package, you're free to do what you like. If you don't want to
re-use the ShortRead::chromosome generic, then don't importFrom(ShortRead,
chromosome)
> 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
> problematic...
this used to be an idiom when there was no NAMESPACE, but inside your package
you know what generics are defined (you've imported them explicitly) so no need
to test whether they exist or not.
>
> 4. Is this phenomenon happening because I'm doing this in the global
> environment (Testing) ? Would it be transparent when in the package
> Namespace ?
>
Yes to some extent there are fewer conflicts -- inside your package -- when
using a 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.
I would guess that what you want to do is reuse the ShortRead generic, not
create a new one, and add your method to it
DESCRIPTION:
Imports: ShortRead
NAMESPACE:
importFrom(ShortRead, chromosome)
exportMethods(chromosome)
R/somewhere.R
setMethod(chromosome, "MyClass", function(object, ...) {
## implementation
})
Hope that helps, and good luck with your package.
Martin
>
> 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.
>
> ______________________________________________
> 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.
>
--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109
Location: Arnold Building M1 B861
Phone: (206) 667-2793
More information about the R-help
mailing list