Skip to content
Prev 366958 / 398506 Next

Function that works within a package and not when copied in global environment. Why?

When searching for the object referred to by a name, R looks first in
the current environment, then in the environment's parent environment,
then in that environment's parent environment, etc.  It stops looking
either when the name is found or when it hits .EmptyEnv, the ultimate
ancestor of all environments.

When a function,f,  is being evaluated, the current environment is a
child of environment(f), which is typically the environment in which
the function was created.  When a function is in a package, the
function's environment is generally the package's namespace. When a
function is made on the command line, its namespace is .GlobalEnv.
.GlobalEnv is not a descendant of any package namespace so when the
function is evaluated it will not look in any package namespace when
looking up names.

You can change the environment of a function with
   environment(f) <- someEnvironment
as in
    environment(revisedLmFunc) <- environment(lm)
or
    environment(revisedLmFunc) <- getNamespace(lm)
I do this when debugging functions in environments, but I don't
recommend it for general use (e.g., for code other people will be
using).

Here is some code that displays the ancestral environments of an
environment to help you look at various situations.

myFormatEnvironment <- function(envir) {
    if (is.null(n <- attr(envir, "name"))) {
        n <- format(envir)
    }
    n
}
ancestralEnvironments <- function (envir) {
    if (identical(envir, emptyenv())) {
        list(envir)
    }
    else {
        c(list(envir), ancestralEnvironments(parent.env(envir)))
    }
}

Here are some examples showing the ancestral environments of a (a)
function in a pacakge, (b) a function in .GlobalEnv, and (c) a
function created by a function in a package.
[1] "<environment: namespace:stats>" "imports:stats"
 [3] "<environment: namespace:base>"  "<environment: R_GlobalEnv>"
 [5] "package:stats"                  "package:graphics"
 [7] "package:grDevices"              "package:utils"
 [9] "package:datasets"               "package:methods"
[11] "Autoloads"                      "<environment: base>"
[13] "<environment: R_EmptyEnv>"
[1] "<environment: R_GlobalEnv>" "package:stats"
 [3] "package:graphics"           "package:grDevices"
 [5] "package:utils"              "package:datasets"
 [7] "package:methods"            "Autoloads"
 [9] "<environment: base>"        "<environment: R_EmptyEnv>"
[1] "<environment: 0x0000000015d33f28>"
 [2] "<environment: namespace:stats>"
 [3] "imports:stats"
 [4] "<environment: namespace:base>"
 [5] "<environment: R_GlobalEnv>"
 [6] "package:stats"
 [7] "package:graphics"
 [8] "package:grDevices"
 [9] "package:utils"
[10] "package:datasets"
[11] "package:methods"
[12] "Autoloads"
[13] "<environment: base>"
[14] "<environment: R_EmptyEnv>"
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Thu, Feb 2, 2017 at 8:34 AM, Marc Girondot via R-help
<r-help at r-project.org> wrote: