[Rd] Interaction between callNextMethod() and selectMethod()

Hervé Pagès hpages at fhcrc.org
Sat Aug 4 01:18:55 CEST 2012


Hi,

Strange things happen. Here is a simple example:

   > setClass("A", contains="integer")
   > setMethod("as.matrix", "A", function(x, ...) t(callNextMethod()))
   Creating a generic function for ‘as.matrix’ from package ‘base’ in 
the global environment
   [1] "as.matrix"
   > a <- new("A", 1:3)
   > as.matrix(a)
        [,1] [,2] [,3]
   [1,]    1    2    3

Everything fine so far. But:

   > selectMethod("as.matrix", "A")(a)
   Error in callNextMethod() :
     c("a call to callNextMethod() appears in a call to 
\"selectMethod\", but the call does not seem to come from either a 
generic function or another 'callNextMethod'", "a call to 
callNextMethod() appears in a call to \"as.matrix\", but the call does 
not seem to come from either a generic function or another 
'callNextMethod'", "a call to callNextMethod() appears in a call to 
\"A\", but the call does not seem to come from either a generic function 
or another 'callNextMethod'")
   In addition: Warning message:
   In if (is.na(i)) { :
     the condition has length > 1 and only the first element will be used

2 things about this:

(1) Sure callNextMethod() needs some context in order to know what to
     do but it seems that the MethodDefinition object returned by
     selectMethod() carries that context i.e. it seems to contain
     all the information that callNextMethod() would need (name of the
     generic and defined signature) to actually work:

       > mymethod <- selectMethod("as.matrix", "A")
       > class(mymethod)
       [1] "MethodDefinition"
       attr(,"package")
       [1] "methods"
       > mymethod at generic
       [1] "as.matrix"
       attr(,"package")
       [1] "base"
       > mymethod at defined
       An object of class "signature"
         x
       "A"

(2) If for whatever reason callNextMethod() really needs to fail, could
     the error handling be fixed so it produces the same output as:

     > mymethod(a)
     Error in callNextMethod() :
       a call to callNextMethod() appears in a call to "mymethod", but 
the call does not seem to come from either a generic function or another 
'callNextMethod'

Thanks!
H.

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-devel mailing list