eval
Evaluate an Expression

Description

Evaluates an expression. You can specify the frame in which the evaluation is to take place.

Usage

eval(expr, envir = parent.frame(),
   enclos = if (is.list(envir) || is.pairlist(envir)) parent.frame() 
   else baseenv())
evalq(expr, envir = parent.frame(),
   enclos = if (is.list(envir) || is.pairlist(envir)) parent.frame() 
   else baseenv())
eval.parent(expr, n = 1)
local(expr, envir = new.env())

Arguments

expr any expression or object to be evaluated.
envir the environment (frame) in which to do the evaluation; by default, the frame of the caller of eval, that is parent.frame(). envir can be an explicit list or pairlist, which creates an evaluation environment with bindings from the named elements of the list. envir can be envir = NULL, which is interpreted as an empty list, creating an evalulation environment with no bindings. envir can be a numeric value, specifying one of the frames of the evaluator, like the argument to sys.frame. A typical numeric choice of envir would be sys.parent(1), meaning the routine that called the routine calling eval.
enclos the enclosure frame used for evaluation; either NULL or an environment. If the evaluation environment is constructed from a list (envir is NULL, a list or a pairlist), enclos is used as the parent environment of this new environment. If enclos is NULL, the base environment is used.
n the number of stack frames to go back.

Details

Value
returns the value of expr.
References
Becker, R. A., Chambers, J. M., and Wilks, A. R. 1988. The New S Language. Pacific Grove, CA: Wadsworth & Brooks/Cole Advanced Books and Software. (eval only.)
See Also
call, sys.frame, parent.frame, environment, parse.
Examples
a <- 3; b <- 2
eval(quote(a*2+b)) # get a,b from global environment
evalq(a*2+b) # quote expression
evalq(a*2+b, list(a=100,b=5)) # get a,b from list
evalq(a*2+b, list(a=100)) # get a from list, b from global environment
eval(as.symbol("a")) # get a from global environment
eval(1:10) # constant values evaluate to themselves

a <- 3; b <- 2 env <- new.env() assign("b", 1000, envir = env)

# get a from list, b from enclos environment evalq(a*2+b, list(a=100), enclos=env)

a <- 3 env <- new.env() assign("a", 1000, envir = env) evalq(a <- 10, env) # '<-' can assign into evaluation environment get("a", envir = env) a

# create a function with its parent environment was created by local. # this function can access persistant state in variable saved.val ff <- local({ saved.val <- 100 function(val) { old.val <- saved.val if (!missing(val)) saved.val <<- val old.val } }) ff() # retrieve original value ff(5) # change internal variable ff() # retrieve new value

Package base version 6.1.4-13
Package Index