Movatterモバイル変換


[0]ホーム

URL:


rdrr.io

delayedAssign: Delay Evaluation

delayedAssignR Documentation

Delay Evaluation

Description

delayedAssign creates apromise to evaluate the givenexpression if its value is requested. This provides direct accessto thelazy evaluation mechanism used byR for the evaluationof (interpreted) functions.

Usage

delayedAssign(x, value, eval.env = parent.frame(1),              assign.env = parent.frame(1))

Arguments

x

a variable name (given as a quoted string in the function call)

value

an expression to be assigned tox

eval.env

an environment in which to evaluatevalue

assign.env

an environment in which to assignx

Details

Botheval.env andassign.env default to the currently activeenvironment.

The expression assigned to a promise bydelayedAssign willnot be evaluated until it is eventually ‘forced’. This happens whenthe variable is first accessed.

When the promise is eventually forced, it is evaluated within theenvironment specified byeval.env (whose contents may have changed inthe meantime). After that, the value is fixed and the expression willnot be evaluated again.

Value

This function is invoked for its side effect, which is assigninga promise to evaluatevalue to the variablex.

See Also

substitute, to see the expression associated with apromise, ifassign.env is not the.GlobalEnv.

Examples

msg <- "old"delayedAssign("x", msg)substitute(x) # shows only 'x', as it is in the global env.msg <- "new!"x # new!delayedAssign("x", {    for(i in 1:3)        cat("yippee!\n")    10})x^2 #- yippeex^2 #- simple numberne <- new.env()delayedAssign("x", pi + 2, assign.env = ne)## See the promise {without "forcing" (i.e. evaluating) it}:substitute(x, ne) #  'pi + 2'### Promises in an environment [for advanced users]:  ---------------------e <- (function(x, y = 1, z) environment())(cos, "y", {cat(" HO!\n"); pi+2})## How can we look at all promises in an env (w/o forcing them)?gete <- function(e_)   lapply(lapply(ls(e_), as.name),          function(n) eval(substitute(substitute(X, e_), list(X=n))))(exps <- gete(e))sapply(exps, typeof)(le <- as.list(e)) # evaluates ("force"s) the promisesstopifnot(identical(unname(le), lapply(exps, eval))) # and another "Ho!"

What can we improve?

R Package Documentation

Browse R Packages

We want your feedback!

Note that we can't provide technical support on individual packages. You should contact the package authors for that.

 
Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, readEmbedding Snippets.

Close

[8]ページ先頭

©2009-2026 Movatter.jp