eval(expression, local=T, parent=<<see below>>) eval.parent(expression, n=1)
eval.
If
local is
FALSE,
evaluation is done in the top-level frame (frame 1).
Otherwise,
local can be either a numeric value, interpreted as one of the
frames of the evaluator, or it can be an explicit list, with the named elements
of the list defining the bindings for the names occurring in the expression.
A typical numeric choice of
local would be
sys.parent(1), meaning
the routine that called the routine calling
eval.
browser).
The
parent argument is ignored if
local is not a list.
eval.parent is a convenience function that does
eval(expression, local=sys.parent(n)).
expression.
If the expression you evaluate involves a function call, the evaluator
typically creates a new frame in which to evaluate the function; if
you want to access or affect the frame designated by
local from the
function your expression called, your function can use
sys.parent in
conjunction with
assign or
eval (see the examples section).
# a function that can simulate the S-PLUS program
# from inside a function: see section 7.4.3 of
# Becker, Chambers and Wilks
try.S <- function()
print(eval(parse(), F))
# this test evaluates a function call, and makes assignments
# into the caller's frame from the function called
eval.test <-
function()
{
x <- pi
y <- runif(10)
z <- 12
myfunc <- function()
{
# this assignment has no effect on parent frame's x
x <- 1
assign("y", 2, frame = sys.parent(1))
eval(expression(z <- 3), local = sys.parent(1))
}
old.values <- list(x = x, y = y, z = z)
# creates new frame for myfunc
eval(expression(myfunc()), local = T)
new.values <- list(x = x, y = y, z = z)
list(old.values = old.values, new.values = new.values)
}