[Rd] operator :: and symbols not in the namespace of a package with a namespace (PR#9279)

thomas.friedrichsmeier at rub.de thomas.friedrichsmeier at rub.de
Fri Oct 6 15:05:05 CEST 2006


Full_Name: Thomas Friedrichsmeier
Version: 2.4.0
OS: GNU/Linux
Submission from: (NULL) (84.61.116.51)


Since R 2.4.0, operator "::" also returns objects in the package environment, if
the package does not have a namespace. This is a very welcome addition.

Additional wish:
If a package has a namespace, but does not place all symbols in that namespace,
lookup will still fail. For example in package boot (version 1.2-26):

library (boot)
exists ("motor", envir=as.environment ("package:boot"))   # TRUE
getAnywhere ("motor")                                     # found in
package:boot
boot::motor                                               # error not in
namespace

This is as documented, but I think it would be convenient, if boot::motor would
also return the object in this case.

This might be achieved by adding an additional tryCatch in "::": If the package
has a namespace, try getExportedValue (), if that fails, try to get from package
environment instead. E.g.:

"::" <- function (pkg, name)
{
    pkg <- as.character(substitute(pkg))
    name <- as.character(substitute(name))
    ns <- tryCatch(asNamespace(pkg), hasNoNamespaceError = function(e) NULL)
    if (is.null(ns)) {
        pos <- match(paste("package", pkg, sep = ":"), search(), 
            0)
        if (pos == 0) 
            stop(gettextf(paste("package '%s' has no name space and", 
                "is not on the search path"), pkg), domain = NA)
        get(name, pos = pos, inherits = FALSE)
    }
    else tryCatch(getExportedValue(pkg, name), error=function (e) {
                       pos <- match(paste("package", pkg, sep=":"), search(),
0)
                       get(name, pos, inherits=FALSE)
                  })
}




More information about the R-devel mailing list