place_in_env: Place variables into an environment

View source: R/place_in_env.R

place_in_envR Documentation

Place variables into an environment

Description

Place the specified variables into the environment supplied.

Usage

place_in_env(..., envir = NULL, var_names = NULL)

Arguments

...

The variables to place into the environment (they can be named or unnamed).

envir

The environment into which to place the variables. The default is the environment of the function itself (which would not be very useful in practice but this is by design to avoid any unintended additions to an environment). So envir should usually be explicitly specified.

var_names

An optional character vector of names for the variables within the environment. See 'Details' for information about default naming.

Details

If envir is NULL, a new environment will be created with new.env and an empty parent, i.e., new.env(parent = emptyenv()).

Names for the environment variables are assigned as follows:

  • If names are specified in var_names, these are used;

  • If var_names is NULL and the ellipsis arguments are named, these names are used;

  • if var_names is NULL and the ellipsis arguments are unnamed, names are set to sequential letters of the alphabet (i.e. the first list item would be named "a", the second "b" and so forth).

Value

The environment supplied, updated to include variables specified in the ellipsis argument (invisibly).

See Also

new.env, emptyenv

Examples

an_env <- new.env(parent = emptyenv())
place_in_env(x = 1, y = 2, envir = an_env)
# Now it is possible to access these by, e.g. an_env$y etc.

# The following would all be equivalent in terms of naming, i.e. they would
# all result in the names "a" and "b" being assigned:
place_in_env("one", "two")
place_in_env(a = "one", b = "two")
place_in_env("one", "two", var_names = c("a", "b"))

# Run from within a function, the following would have the effect of placing
# the variables v and w into the function's local environment:
place_in_env(v = "one", w = "two", envir = environment())

# Run from within the global environment, the following would have the effect
# of placing the variables v and w into the global environment:
## Not run: 
place_in_env(v = "one", w = "two", envir = environment())

## End(Not run)


toniprice/jute documentation built on Jan. 11, 2023, 8:23 a.m.