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()
injector | An injector returned by |
---|
Every function returns an injector (R environment).
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.
Setting an injector from within a module is not allowed and results in an error.
.SharedEnv
, define
,
list_modules
, make
, and reset
.
reset()#> [2018-12-02T17:14:00 CET] Resetting modulr state ... OK#> [2018-12-02T17:14:00 CET] Defining 'foo' ... OKinjector <- new_injector() previous_injector <- set_injector(injector) define("bar", NULL, function() NULL)#> [2018-12-02T17:14:00 CET] Defining 'bar' ... OKlsmod()#> 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 2018-12-02T17:14:00 CETset_injector(previous_injector)#> <environment: 0x9f6cc60>lsmod()#> 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 2018-12-02T17:14:00 CETnot_run({ .Last <- function() { # Bind the current injector (internal modulr state) to the environment. injector <- get_injector() } quit(save = "yes") }) reset()#> [2018-12-02T17:14:00 CET] Resetting modulr state ... OK#> [2018-12-02T17:14:00 CET] 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: 0xa7a8f98>make("foo")#> [2018-12-02T17:14:01 CET] Making 'foo' ... #> [2018-12-02T17:14:01 CET] * Visiting and defining dependencies ... #> [2018-12-02T17:14:01 CET] * Constructing dependency graph ... OK #> [2018-12-02T17:14:01 CET] DONE ('foo' in 0.028 secs)#> [1] "Hi Bob!"