Interaction between callNextMethod() and selectMethod()
FWIW, here is my sessionInfo:
> sessionInfo()
R Under development (unstable) (2012-07-25 r59963)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
BTW, it's funny that the VERSION file in the R source tree says:
hpages at latitude:~/src/R-2.16.r59963$ cat VERSION
2.16.0 Under development (unstable)
^^^^^^
H.
On 08/03/2012 04:18 PM, Herv? Pag?s wrote:
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