search_path: The set of environments searched to locate an object

View source: R/search_path.R

search_pathR Documentation

The set of environments searched to locate an object

Description

The set of environments searched to locate an object

Usage

search_path(parent = 1L, until = emptyenv())

Arguments

parent

The parent generation to query for the parent frame and the function.

until

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.

Details

This is the same environment search path that is used by exist(name, inherits = TRUE) and get(name, inherits = TRUE).

Value

A named list of environments. The first environment is the calling environment, and the last is the empty environment.

Examples

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()

g()

HenrikBengtsson/environments documentation built on Jan. 15, 2025, 12:58 a.m.