Create an new injector, set and get the current injector, and get the default injector.

new_injector()

set_injector(injector = new_injector())

set_default_injector()

get_injector()

get_default_injector()

Arguments

injector

An injector returned by new_injector.

Value

Every function returns an injector (R environment).

Details

An injector essentially carries an internal modulr state. Technically, it is is an R environment containing every piece of information needed by modulr to reflect the module definitions, the dependencies between them, the configurations, and all the associated metadata. As a PORO (Plain Old R Object), an injector can be stored to disk with the session data, or shared between Alice and Bob, for instance.

When the modulr package is loaded, a default injector is created. This injector is returned by the get_default_injector function.

Warning

Setting an injector from within a module is not allowed and results in an error.

See also

.SharedEnv, define, list_modules, make, and reset.

Examples

#> [2017-06-21T21:58:22 UTC] Resetting modulr state ... OK
define("foo", NULL, function() NULL)
#> [2017-06-21T21:58:22 UTC] Defining 'foo' ... OK
injector <- new_injector() previous_injector <- set_injector(injector) define("bar", NULL, function() NULL)
#> [2017-06-21T21:58:22 UTC] Defining 'bar' ... OK
#> name version storage along type weight calls dependencies uses size #> 1 bar <NA> in-memory <NA> <NA> <NA> 0 0 0 888 bytes #> lines modified #> 1 2 2017-06-21T21:58:22 UTC
set_injector(previous_injector)
#> <environment: 0x9edb7d0>
#> name version storage along type weight calls dependencies uses size #> 1 foo <NA> in-memory <NA> <NA> <NA> 0 0 0 888 bytes #> lines modified #> 1 2 2017-06-21T21:58:22 UTC
not_run({ .Last <- function() { # Bind the current injector (internal modulr state) to the environment. injector <- get_injector() } quit(save = "yes") }) reset()
#> [2017-06-21T21:58:22 UTC] Resetting modulr state ... OK
define("foo", NULL, function() "Hi Bob!")
#> [2017-06-21T21:58:22 UTC] Defining 'foo' ... OK
## Alice saves its injector and sends it to ... saveRDS(get_injector(), file = "injector.R") ## ... Bob who restores it. injector <- readRDS("injector.R") set_injector(injector)
#> <environment: 0x7d97570>
make("foo")
#> [2017-06-21T21:58:23 UTC] Making 'foo' ... #> [2017-06-21T21:58:23 UTC] * Visiting and defining dependencies ... #> [2017-06-21T21:58:23 UTC] * Constructing dependency graph ... OK #> [2017-06-21T21:58:23 UTC] DONE ('foo' in 0.011 secs)
#> [1] "Hi Bob!"