eval_code | R Documentation |
qenv
Evaluate code in qenv
eval_code(object, code)
## S3 method for class 'qenv'
within(data, expr, ...)
object |
( |
code |
( |
data |
( |
expr |
( |
... |
named argument value will substitute a symbol in the |
eval_code()
evaluates given code in the qenv
environment and appends it to the code
slot.
Thus, if the qenv
had been instantiated empty, contents of the environment are always a result of the stored code.
within()
is a convenience method that wraps eval_code
to provide a simplified way of passing expression.
within
accepts only inline expressions (both simple and compound) and allows to substitute expr
with ...
named argument values.
qenv
environment with code/expr
evaluated or qenv.error
if evaluation fails.
within
Passing language objects to expr
is generally not intended but can be achieved with do.call
.
Only single expression
s will work and substitution is not available. See examples.
# evaluate code in qenv
q <- qenv()
q <- eval_code(q, "a <- 1")
q <- eval_code(q, "b <- 2L # with comment")
q <- eval_code(q, quote(library(checkmate)))
q <- eval_code(q, expression(assert_number(a)))
# evaluate code using within
q <- qenv()
q <- within(q, {
i <- iris
})
q <- within(q, {
m <- mtcars
f <- faithful
})
q
get_code(q)
# inject values into code
q <- qenv()
q <- within(q, i <- iris)
within(q, print(dim(subset(i, Species == "virginica"))))
within(q, print(dim(subset(i, Species == species)))) # fails
within(q, print(dim(subset(i, Species == species))), species = "versicolor")
species_external <- "versicolor"
within(q, print(dim(subset(i, Species == species))), species = species_external)
# pass language objects
expr <- expression(i <- iris, m <- mtcars)
within(q, expr) # fails
do.call(within, list(q, expr))
exprlist <- list(expression(i <- iris), expression(m <- mtcars))
within(q, exprlist) # fails
do.call(within, list(q, do.call(c, exprlist)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.