get_fn_envir: Get value from function call or specific environment

View source: R/splicejam-shiny-data.R

get_fn_envirR Documentation

Get value from function call or specific environment

Description

Get value from function call or specific environment in that order

Usage

get_fn_envir(x, envir = NULL, verbose = FALSE, ...)

Arguments

x

character string indicating the name of an R object.

envir

environment or list of environment objects.

verbose

logical indicating whether to print verbose output.

...

additional arguments are ignored.

assign_to_envir

logical indicating whether to assign values to environment envir, if envir is not NULL. This option is helpful to combine function arguments with environment values.

Details

This function is a helper function intended to return a variable value, if it exists and is not NULL, by searching these locations in order:

  1. The calling function, which is the environment of the function that called get_fn_envir().

  2. The environment or environments provided in envir.

  3. It returns NULL if the previous steps do not find the object named by x.

Value

object represented by variable name given in x from either the calling function, or the environment envir, or NULL if not defined in either case.

Examples

x <- 10;
get_fn_envir("x")

test_x <- function(x=NULL, envir=NULL, verbose=FALSE, ...) {
   get_fn_envir("x", envir, verbose=verbose)
}

test_x()

test_x(envir=globalenv())

test_x(x=5)

test_x(x=5, envir=globalenv())
test_x(x=NULL, envir=globalenv())

test_x(envir=globalenv())

# create new environment
testenv <- new.env();
testenv$x <- 100;

test_x(envir=testenv, verbose=TRUE)
test_x(x=1000, envir=testenv, verbose=TRUE)

# search testenv then globalenv()
test_x(x=12, envir=c(testenv, globalenv()), verbose=TRUE)
test_x(envir=c(testenv, globalenv()), verbose=TRUE)

testenv$x <- NULL;
test_x(envir=c(testenv, globalenv()), verbose=TRUE)

rm("x", envir=testenv);
test_x(envir=c(testenv, globalenv()), verbose=TRUE)


jmw86069/splicejam documentation built on April 21, 2024, 4:57 p.m.