[Rd] NAMESPACE & methods guidance, please

Martin Morgan mtmorgan at fhcrc.org
Fri May 30 22:02:05 CEST 2008


My conception of how NAMESPACE and methods in R-2.7.0 resolved a
generic 'func' to a func-method was to search as follows:

In 2.7.0:

func -->
    NAMESPACE, including Imports: (and other details) -->
    .GlobalEnv, and eventually Depends: since these are on search()

In R-devel it seems like

func -->
    NAMESPACE, including Imports: (and other details) -->
    ?? but not pkgs in Depends:

Is this correct or as intended, or is there a better conception to
have?

This model is from the following:

In 2.7.0:

> library(KEGG.db) # Imports, Depends AnnotationDbi; KEGG.db is data-only
> head(ls(KEGGPATHID2EXTID))
[1] "hsa00232" "hsa00230" "hsa04514" "hsa04010" "hsa04012" "hsa04150"

vs. in a package tmpA with a NAMESPACE with Depend: and Imports:
KEGG.db and in a new session of R

> library(tmpA)
> dokegg
function () 
{
    require("KEGG.db")
    head(ls(KEGGPATHID2EXTID))
}
<environment: namespace:tmpA>
> dokegg()
[1] "hsa00232" "hsa00230" "hsa04514" "hsa04010" "hsa04012" "hsa04150"

In R-devel

> dokegg()
Error in as.environment(pos) : invalid object for 'as.environment'
Error in head(ls(KEGGPATHID2EXTID)) : 
  error in evaluating the argument 'x' in selecting a method for function 'head'

In some ways this seems good (tmpA author has to specify use of
AnnotationDbi::ls, and tmpA is not confused by things the user does to
their environment) but in other ways it seems to undo any benefit of
method dispatch and requires the tmpA author to fully discover (and
monitor -- what if AnnotationDbi splits 'ls' into a AnnotationBase
pkg?) the class hierarchy.

The specific example of KEGG.db and AnnotationDbi is maybe more
interesting than others, because there is really no functionality to
be imported from KEGG.db.

Martin
-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-devel mailing list