parent.nenv: Access environments further in the definition chain

Description Usage Arguments Value Examples

View source: R/Misc.R

Description

Get the parent, grandparent, grandgrandparent of an environment

Usage

1
parent.nenv(env, n)

Arguments

env

an environment

n

how many steps to go up the chain, >= 0

Value

An enclosing environment

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
myfun <- function(x) {
  myfun2 <- function(x2) {
    x+x2
  }
  x <- x+1
  return(myfun2)
}
parent.env(environment(myfun(1))) # That is myfuns environment
parent.env(parent.env(environment(myfun(1))))
  # This is the environment in which myfun was defined, normally .GlobalEnv
parent.nenv(environment(myfun(1)), 2) # The same call, but clearer

EmilBode/EmilMisc documentation built on Feb. 24, 2020, 4:11 p.m.