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

From: Hervé Pagès <hpages_at_fhcrc.org>
Date: Fri, 03 Aug 2012 16:24:50 -0700

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Fri 03 Aug 2012 - 23:28:15 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 04 Aug 2012 - 20:20:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive