assign_in_ns: Assign a variable into a namespace

View source: R/utils_environments_namespaces.R

assign_in_nsR Documentation

Assign a variable into a namespace

Description

Unlocks and relocks namespaces and bindings as needed

Usage

assign_in_ns(
  x,
  nm = rlang::expr_deparse(rlang::enexpr(x)),
  ns_env = rlang::ns_env(pkg_name())
)

Arguments

x

object/chr either the object itself or the name of the object to assign

nm

chr name for object in the namespace. IF x is not supplied, this object will be retrieved by name from the calling environment via get0

ns_env

env of the namespace

Value

x the object

See Also

Other namespaces: assign_global(), get_from_ns(), get_global(), pkg_ns(), unload_namespaces()

Examples

(function(x = mtcars) {
y <- dplyr::mutate(x, cylinders = cyl)
  assign_in_ns(nm = "y", ns_env = "UU")
})()
uu <- rlang::ns_env("UU")
exists("y", uu)
assign_in_ns(mtcars, ns_env = "UU")
exists("mtcars", uu)
assign_in_ns(mtcars, "cars_copy", ns_env = "UU")
exists("cars_copy", uu)
rlang::env_unlock(uu)
rm(list = c("y", "mtcars", "cars_copy"), envir = uu)
rlang::env_lock(uu)

yogat3ch/UU documentation built on May 31, 2024, 10:14 p.m.