[R] Why S4 method is not visible from another package?

Gábor Csárdi csardi.gabor at gmail.com
Fri Sep 18 16:01:19 CEST 2009


On Thu, Sep 17, 2009 at 5:59 PM, Martin Morgan <mtmorgan at fhcrc.org> wrote:
> Gábor Csárdi wrote:
>> Dear All,
>>
>> maybe this is something obvious, I seem to be incapable of
>> understanding how S4 works.
>>
>> So, in package 'A' I defined a "summary" method for my class:
>>
>> setMethod("summary", signature(object="ListHyperGResult"),
>>           function(object, pvalue=pvalueCutoff(object), categorySize=NULL) {
>>              "whatever"
>>           })
>>
>> "ListHyperGResult" has a subclass, "GOListHyperGResult":
>>
>> setClass("GOListHyperGResult",
>>          representation=representation(conditional="logical"),
>>          contains="ListHyperGResult",
>>          prototype=prototype(testname="GO"))
>>
>> The summary method is exported in the NAMESPACE:
>>
>> exportMethods("summary")
>>
>> Package 'B' depends on package 'A', this is stated in the
>> 'DESCRIPTION' file. If I call 'summary' on a 'GOListHyperGResult' in
>
> Hi Gabor
>
> It is not S4 alone, but S4 + name spaces that are giving you problems.
>
> You probably want to Import: A rather than depends, and importFrom(A,
> summary).
>
> As it stands, inside the B name space, you find base::summary, whereas
> you've defined a method on summary that has been promoted to a generic
> in one of the packages that A imports (probably AnnotationDbi).
>
> This is a little bit of a guess; at some level it might seem more
> appropriate to Import: AnnotationDbi and importFrom(AnnotationDbi,
> summary) (or wherever the generic for summary that you are trying to use
> is created).

Martin, thanks, this solved the problem.

But isn't this a bit weird? Suppose I am the author of package 'B' and
want to use the classes defined in package 'A'. I don't care about
exact details of the implementation of these classes, I don't want to
know that they are based on something in package 'C' (AnnotationDbi,
really). But I still have to import specific functions from 'C'.

Moreover, suppose the author of 'C' changes 'summary', e.g. puts it
into another package, 'D' and makes 'C' importing it from 'D'. This
will break my package, 'B' as well.

Anyway, thanks a lot for your help, Best Regards,
Gabor

> Martin
>


-- 
Gabor Csardi <Gabor.Csardi at unil.ch>     UNIL DGM




More information about the R-help mailing list