search_path.Rd
The set of environments searched to locate an object
search_path(parent = 1L, until = emptyenv())
The parent generation to query for the parent frame and the function.
An environment
to consider
the last parent environment. If until
is not one of the parent
environments, then emptyenv()
is the
last one. It is also possible to specify a list of alternative
environments.
A named list of environments. The first environment is the calling environment, and the last is the empty environment.
This is the same environment search path that is used by
exist(name, inherits = TRUE)
and get(name, inherits = TRUE)
.
a <- 1 ## <== f() only sees this 'a'
f <- function() {
b <- 2
## Get the environments scanned for 'a'. This shows that the current
## environment (= environment()) is first searched, then the parent
## environments of 'f' (= parent_envs(f)) are searched. This is the
## reason why 'a' (a == 1) in the top environment is used. Note that,
## if we call f() from g(), the environment from where f() is called,
## which holds another 'a' (a == 42), is *not* involved. Thus, we get
## the same result regardless from where f() is called.
envirs <- search_path(until = globalenv())
utils::str(envirs)
b * a
}
g <- function() {
a <- 42 ## <== this 'a' is never seen by f()
f()
}
f()
#> List of 6
#> $ 0x55f418972c60:<environment: 0x55f418972c60>
#> $ 0x55f418aac2c0:<environment: 0x55f418aac2c0>
#> $ 0x55f418ca09f8:<environment: 0x55f418ca09f8>
#> $ 0x55f418ceb5b0:<environment: 0x55f418ceb5b0>
#> $ 0x55f412202000:<environment: 0x55f412202000>
#> $ R_GlobalEnv :<environment: R_GlobalEnv>
#> [1] 2
g()
#> List of 6
#> $ 0x55f4188144f0:<environment: 0x55f4188144f0>
#> $ 0x55f418aac2c0:<environment: 0x55f418aac2c0>
#> $ 0x55f418ca09f8:<environment: 0x55f418ca09f8>
#> $ 0x55f418ceb5b0:<environment: 0x55f418ceb5b0>
#> $ 0x55f412202000:<environment: 0x55f412202000>
#> $ R_GlobalEnv :<environment: R_GlobalEnv>
#> [1] 2