A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://insightsengineering.github.io/teal.code/latest-tag/reference/eval_code.html below:

Evaluate code in qenv — eval_code • teal.code

Evaluate code in qenv

Usage
eval_code(object, code)

# S3 method for class 'qenv'
within(data, expr, ...)
Arguments
object

(qenv)

code

(character, language or expression) code to evaluate. It is possible to preserve original formatting of the code by providing a character or an expression being a result of parse(keep.source = TRUE).

data

(qenv)

expr

(expression) to evaluate. Must be inline code, see Using language objects...

...

named argument value will substitute a symbol in the expr matched by the name. For practical usage see Examples section below.

Value

qenv environment with code/expr evaluated or qenv.error if evaluation fails.

Details

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.

Using language objects with within

Passing language objects to expr is generally not intended but can be achieved with do.call. Only single expressions will work and substitution is not available. See examples.

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
#> <environment: 0x5586d8824090> 🔒 
#> Parent: <environment: package:checkmate> 
#> Bindings:
#> - f: [data.frame]
#> - i: [data.frame]
#> - m: [data.frame]
get_code(q)
#> [1] "i <- iris\nm <- mtcars\nf <- faithful"

# inject values into code
q <- qenv()
q <- within(q, i <- iris)
within(q, print(dim(subset(i, Species == "virginica"))))
#> [1] 50  5
#> <environment: 0x5586d905e678> 🔒 
#> Parent: <environment: package:checkmate> 
#> Bindings:
#> - i: [data.frame]
within(q, print(dim(subset(i, Species == species)))) # fails
#> <qenv.error: object 'species' not found 
#>  when evaluating qenv code:
#> print(dim(subset(i, Species == species)))>
within(q, print(dim(subset(i, Species == species))), species = "versicolor")
#> [1] 50  5
#> <environment: 0x5586d1393b20> 🔒 
#> Parent: <environment: package:checkmate> 
#> Bindings:
#> - i: [data.frame]
species_external <- "versicolor"
within(q, print(dim(subset(i, Species == species))), species = species_external)
#> [1] 50  5
#> <environment: 0x5586d75e8a98> 🔒 
#> Parent: <environment: package:checkmate> 
#> Bindings:
#> - i: [data.frame]

# pass language objects
expr <- expression(i <- iris, m <- mtcars)
within(q, expr) # fails
#> <qenv.error: object 'expr' not found 
#>  when evaluating qenv code:
#> expr>
do.call(within, list(q, expr))
#> <environment: 0x5586d83cb438> 🔒 
#> Parent: <environment: package:checkmate> 
#> Bindings:
#> - i: [data.frame]
#> - m: [data.frame]

exprlist <- list(expression(i <- iris), expression(m <- mtcars))
within(q, exprlist) # fails
#> <qenv.error: object 'exprlist' not found 
#>  when evaluating qenv code:
#> exprlist>
do.call(within, list(q, do.call(c, exprlist)))
#> <environment: 0x5586d8c22170> 🔒 
#> Parent: <environment: package:checkmate> 
#> Bindings:
#> - i: [data.frame]
#> - m: [data.frame]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4