[Rd] NAMESPACE & methods guidance, please

Seth Falcon seth at userprimary.net
Sun Jun 1 20:15:56 CEST 2008


* On 2008-06-01 at 11:30 -0400 John Chambers wrote:
> My impression (but just as a user, not an implementer) is that the 
> NAMESPACE mechanism is intended to search for anything, not just for 
> methods, as follows:
> 
>  - look in the namespace itself;
>  - look in the imports, which are in the parent.env of the namespace;
>  - look in the base package's namespace.

As described in the R News article [1], the above describes the static
component of the search mechanism, but there is a dynamic component
which adds:

    - look in .GlobalEnv
    - look in each package on the search path
    - look (again) in base

[1] http://cran.r-project.org/doc/Rnews/Rnews_2003-1.pdf

> Period.  This provides a definition of the behavior of functions in the 
> package that is independent of the dynamically changing contents of the 
> search list.

I think the dynamic lookup is important.  Consider class Foo and some
methods, like show, for working with Foo instances defined in pkgA.
Further, suppose pkgB imports pkgA and contains a function that
returns a Foo instance.

If a user class library("pkgB") at the prompt, both the developer and
the user would like for methods for dealing with Foo instances to be
available.

This has been achieved by adding pkgA to the Depends field of pkgB.
In this case, library("pkgB") has the side-effect of attaching pkgA to
the search path and Foo instances behave as desired.  This, I believe,
describes the first part of Martin's example:

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

John Chambers:
> "Depends" may cause the relevant packages to be put on the search list. 
> But a subsequent attach or detach could change what objects were found.  
> So unless this is not the intended interpretation of namespaces, looking 
> in the search list seems a bad idea in principle.

I agree that using the dynamic lookup when the static lookup is
available is bad programming practice.  However, given the flexibility
of the current tools, it seems not unreasonable to expect that
picking up a method via the search path would work in a package just
as it does (should?) interactively.


+ seth

-- 
Seth Falcon | http://userprimary.net/user/



More information about the R-devel mailing list