[Rd] Possible problem with S4 dispatch

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 1 14:09:19 CEST 2007

On Mon, 30 Apr 2007, Seth Falcon wrote:

> First a bit of disclaimer...  I haven't isolated this problem into an
> easy to reproduce case, and I won't be surprised if the root cause is
> a fault in my code's use of name spaces or some such.

Note that you called

selectMethod("mget", signature(x="character", envir=class(LLe)))

by name rather than calling the visible function mget() (which you could 
have supplied as fdef).  I've never really got to the bottom of the 
complicated searches that getGeneric() uses, but the fact that it does not 
just look for a visible function of that name tells you it is doing 
something different.

What I would check from your browser is what parent.env() shows, 
successively until you get to the imports and then the base namespace. If 
mget is not in the imports, something would seem to be up with your 
importing of namespaces.  find() is not relevant here as namespace 
scoping is in play: only if the mget generic is imported will it take 
precedence over base:::mget.  (It is not clear to me what is being browsed 
here, and hence what namespaces are in play.)

Perhaps we need lexical versions of find() and search() to make it easier 
to explore search paths from a function body.

Hope it helps: without a reproducible example I can only guess blindly.

> The error I'm seeing is one in which the desired method is not found.
> What worries me in terms of my expectations of how to debug the
> problem is that showMethods and selectMethod both find the method.
> Here is an example browser session:
>    ## Input 1, a character vector
>    Browse[1]> gN[1:3]
>    [1] "1005_at"   "1007_s_at" "1008_f_at"
>    ## Input 2, an object of class AtomicAnnMap (subclass of AnnMap)
>    Browse[1]> class(LLe)
>    [1] "AtomicAnnMap"
>    attr(,"package")
>    [1] "AnnotationDbi"
>    ## See what selectMethod has to say (it finds the method I'm expecting)
>    Browse[1]> selectMethod("mget", signature(x="character", envir=class(LLe)))
>    Method Definition:
>    function (x, envir, mode = "any", ifnotfound = list(function(x) stop(paste("value for '",
>        x, "' not found", sep = ""), call. = FALSE)), inherits = FALSE)
>    {
>        .checkNamesAreStrings(x)
>        .checkNamesExist(x, names(envir))
>        as.list(envir, names = x)
>    }
>    <environment: namespace:AnnotationDbi>
>    Signatures:
>            x           envir
>    target  "character" "AtomicAnnMap"
>    defined "ANY"       "AnnMap"
>    ## Call it (we get base::mget, not the generic and hence an error)
>    Browse[1]> mget(gN[1:3], LLe)
>    Error in mget(x, envir, mode, ifnotfound, inherits) :
>    	second argument must be an environment
>    Browse[1]> find("mget")
>    [1] "package:AnnotationDbi" "package:base"
> The package code I'm working with imports the package that defines the
> mget method (and this package does exportMethods(mget)), yet the
> problem seems to be that the mget generic is not found -- but I find
> it confusing that selectMethod works here.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-devel mailing list