Last chance! 50% off unlimited learning
Sale ends in
delayedAssign
creates a promise to evaluate the given
expression if its value is requested. This provides direct access
to the lazy evaluation mechanism used by R for the evaluation
of (interpreted) functions.
delayedAssign(x, value, eval.env = parent.frame(1), assign.env = parent.frame(1))
x
value
x
value
to the variable x
.
eval.env
and assign.env
default to the currently active
environment. The expression assigned to a promise by delayedAssign
will
not be evaluated until it is eventually ‘forced’. This happens when
the variable is first accessed.
When the promise is eventually forced, it is evaluated within the
environment specified by eval.env
(whose contents may have changed in
the meantime). After that, the value is fixed and the expression will
not be evaluated again.
substitute
, to see the expression associated with a promise.
msg <- "old"
delayedAssign("x", msg)
msg <- "new!"
x #- new!
substitute(x) #- x (was 'msg' ?)
delayedAssign("x", {
for(i in 1:3)
cat("yippee!\n")
10
})
x^2 #- yippee
x^2 #- simple number
e <- (function(x, y = 1, z) environment())(1+2, "y", {cat(" HO! "); pi+2})
(le <- as.list(e)) # evaluates the promises
Run the code above in your browser using DataLab