[R] function showMethods and inheritance

John Chambers jmc at research.bell-labs.com
Tue Nov 5 22:18:28 CET 2002


Laurent Gautier wrote:
> 
> Hi,
> 
> The following question might come from my (deep) misunderstanding
> of the concepts in the package 'methods'.
> 
> I have a class 'A', and a class 'B' inheriting 'A' (so defined
> with 'setClass' and the parameter 'contains="A"'. The class
> A has a method 'mymethod'. A call to 'showMethods("mymethod")'
> return the signature of the function with A, but not with B no
> matter whether I specify 'inherited=TRUE' or not...
> 
> Any pointer ?
> 
> Thanks in advance,
> 
> Laurent

There isn't quite enough information in your question to be sure, but
the following is a likely explanation.

If you look at ?showMethods, under the argument "inherited" you'll see
the comment that "methods that have been found by inheritance, so far in
the session" will be shown.  The cost of searching for inherited methods
is `amortized', in that inherited methods are stored only after they
have been needed.  So until you actually call the relevant function with
an argument from class B, you won't see the method in showMethods.

Here's an example, roughly along your lines. (By the way, one point of
possible confusion:  _classes_ don't have methods, _functions_ have
methods corresponding to the classes of the arguments.  But let's call
the function myMethod to keep to the example.)

R> setClass("A", representation(a="character"))
[1] "A"
R> setClass("B", representation(b="numeric"), contains = "A")
[1] "B"
R> setGeneric("myMethod", function(x)standardGeneric("myMethod"))
[1] "myMethod"
R> setMethod("myMethod", "A", function(x)x at a)
[1] "myMethod"
R> b1 <- new("B", a="Testing", b = 1:10)
R> showMethods("myMethod", inherited = TRUE)

Function "myMethod":
x = "A"
R> myMethod(b1)
[1] "Testing"
R> showMethods("myMethod", inherited = TRUE)

Function "myMethod":
x = "A"
x = "B"
    (inherited from x = "A")

Another way to see what method would be used with class "B" (if that was
the original purpose) is to call selectMethod, which does the same
method selection as calling the function:

R> selectMethod("myMethod", "B")
Method Definition (Class "MethodDefinition"):

function(x)x at a

Signatures:
        x  
target  "B"
defined "A"

In the printed version of the method, "target" is the class signature
for which the method would be used, and "defined" is the signature with
which setMethod was called.
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list