Most apps start out with setup code that is non-reactive, such as
library()
calls, loading of static data into local
variables, or source
-ing of supplemental R scripts.
metaAction
provides a convenient way to run such code for its side effects
(including declaring new variables) while making it easy to export that code
using expandChain()
. Note that metaAction
executes code directly in the
env
environment (which defaults to the caller's environment), so any local
variables that are declared in the expr
will be available outside of
metaAction
as well.
metaAction(expr, env = parent.frame(), quoted = FALSE)
A code expression that will immediately be executed (before the
call to metaAction
returns), and also stored for later retrieval (i.e.
meta mode).
An environment.
Is the expression quoted? This is useful when you want to use an expression
that is stored in a variable; to do so, it must be quoted with quote()
.
A function that, when called in meta mode (i.e. inside
expandChain()
), will return the code in quoted form. If this function is
ever called outside of meta mode, it throws an error, as it is definitely
being called incorrectly.
setup <- metaAction({
library(stats)
"# Set the seed to ensure repeatable randomness"
set.seed(100)
x <- 1
y <- 2
})
# The action has executed
print(x)
#> [1] 1
print(y)
#> [1] 2
# And also you can emit the code
expandChain(
setup()
)
#> library(stats)
#> # Set the seed to ensure repeatable randomness
#> set.seed(100)
#> x <- 1
#> y <- 2