[Rd] Overloading S4 methods

Iago Mosqueira iago.mosqueira at gmail.com
Mon Jun 6 13:11:54 CEST 2011


On Wed, Jun 1, 2011 at 6:04 PM, Martin Morgan <mtmorgan at fhcrc.org> wrote:
> On 06/01/2011 04:39 AM, Iago Mosqueira wrote:
>>
>> Dear all,
>>
>> I am experiencing some problems with S4 method overloading. I have
>> defined a generic for graphics:plot, using
>>
>> setGeneric("plot", useAsDefault = plot)
>>
>> and with
>>
>> importFrom('graphics', 'plot') and
>>
>> exportMethods('plot') in the NAMESPACE file of pkg A.
>
> I'd guess you were creating two generics (explicitly in pkgA, implicitly in
> pkgB). Maybe
>
>  export(plot)
>
> in NAMESPACE of pkg A,
>
>  importFrom('pkgA', plot)
>  exportMethods(plot)
>
> in pkg B. Feel free to post to the list if that's helpful.
>
> Martin
>
>>
>> I then proceed to define a method for signature c('myS4class',
>> 'missing'). This works as expected: selectMethod('plot',
>> c('myS4class', 'missing')) returns the newly defined method, and the
>> method gets called when invoked.
>>
>> Another pkg, B, wishes to overload this and redefines the method for
>> the same signature. A method is defined for c('myS4class', 'missing'),
>> and exported on the NAMESPACE. The new method is shown by
>> selectMethod() after pkg B has been loaded, but a call to
>>
>> plot(anobjectofmyS4class)
>>
>> comes up with the result of running the first method, from pkg A. I
>> have tried importing 'plot' in B's NAMESPACE from both graphics or A,
>> but the end result is the same.
>>
>> Package B does the same thing for a method created by pkg A, myMethod,
>> and that works fine.
>>
>> Any pointers or where this might be going wrong? How is it that a
>> different method than the one shown by selectMethod() is being run?
>> Something to do with the 'missing' part of the signature?
>>
>> Many thanks,
>>
>>
>>
>> Iago Mosqueira

Dear all,

I have tried Martin's suggestion, but the problem persists. It seems
to be related to having 'missing' in the signature, as doing the same
kind of overloading for c('myclass', 'ANY') work as expected.

I am attaching 2 simple packages where I attempt this repeated
overloading of plot for the same class. Script below, also found in
Bpkg/tests.test.R, shows what I have encountered so far:
plot('myclass', 'ANY') can be re-overloaded, but plot('myclass',
'missing') cannot in the same way. If I run

trace("plot", browser, exit=browser, signature = c("mynum", "missing"))

the  new method is actually called.

Any hint on what I am doing wrong or where to look for an explanation
will be much appreciated.

Best regards,


Iago Mosqueira
-------------- next part --------------
A non-text attachment was scrubbed...
Name: plot_pkgs.tar.gz
Type: application/x-gzip
Size: 1178 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20110606/b35855a1/attachment.gz>


More information about the R-devel mailing list