View source: R/delayed_api-futureAssign.R
futureAssign | R Documentation |
x %<-% value
(also known as a "future assignment") and
futureAssign("x", value)
create a Future that evaluates the expression
(value
) and binds it to variable x
(as a
promise). The expression is evaluated in parallel
in the background. Later on, when x
is first queried, the value of future
is automatically retrieved as it were a regular variable and x
is
materialized as a regular value.
futureAssign(
x,
value,
envir = parent.frame(),
substitute = TRUE,
lazy = FALSE,
seed = FALSE,
globals = TRUE,
packages = NULL,
stdout = TRUE,
conditions = "condition",
earlySignal = FALSE,
label = NULL,
gc = FALSE,
...,
assign.env = envir
)
x %<-% value
fassignment %globals% globals
fassignment %packages% packages
fassignment %seed% seed
fassignment %stdout% capture
fassignment %conditions% capture
fassignment %lazy% lazy
fassignment %label% label
fassignment %plan% strategy
fassignment %tweak% tweaks
x |
the name of a future variable, which will hold the value of the future expression (as a promise). |
value |
An R expression. |
envir |
The environment from where global objects should be identified. |
substitute |
If TRUE, argument |
lazy |
If FALSE (default), the future is resolved eagerly (starting immediately), otherwise not. |
seed |
(optional) If TRUE, the random seed, that is, the state of the
random number generator (RNG) will be set such that statistically sound
random numbers are produced (also during parallelization).
If FALSE (default), it is assumed that the future expression does neither
need nor use random numbers generation.
To use a fixed random seed, specify a L'Ecuyer-CMRG seed (seven integer)
or a regular RNG seed (a single integer). If the latter, then a
L'Ecuyer-CMRG seed will be automatically created based on the given seed.
Furthermore, if FALSE, then the future will be monitored to make sure it
does not use random numbers. If it does and depending on the value of
option future.rng.onMisuse, the check is
ignored, an informative warning, or error will be produced.
If |
globals |
(optional) a logical, a character vector, or a named list
to control how globals are handled.
For details, see section 'Globals used by future expressions'
in the help for |
packages |
(optional) a character vector specifying packages to be attached in the R environment evaluating the future. |
stdout |
If TRUE (default), then the standard output is captured,
and re-outputted when |
conditions |
A character string of conditions classes to be captured
and relayed. The default is to relay all conditions, including messages
and warnings. To drop all conditions, use |
earlySignal |
Specified whether conditions should be signaled as soon as possible or not. |
label |
A character string label attached to the future. |
gc |
If TRUE, the garbage collector run (in the process that
evaluated the future) only after the value of the future is collected.
Exactly when the values are collected may depend on various factors such
as number of free workers and whether |
assign.env |
The environment to which the variable should be assigned. |
fassignment |
The future assignment, e.g.
|
capture |
If TRUE, the standard output will be captured, otherwise not. |
strategy |
The mechanism for how the future should be
resolved. See |
tweaks |
A named list (or vector) with arguments that should be changed relative to the current strategy. |
... |
Additional arguments passed to |
For a future created via a future assignment, x %<-% value
or
futureAssign("x", value)
, the value is bound to a promise, which when
queried will internally call value()
on the future and which will then
be resolved into a regular variable bound to that value. For example, with
future assignment x %<-% value
, the first time variable x
is queried
the call blocks if, and only if, the future is not yet resolved. As soon
as it is resolved, and any succeeding queries, querying x
will
immediately give the value.
The future assignment construct x %<-% value
is not a formal assignment
per se, but a binary infix operator on objects x
and expression value
.
However, by using non-standard evaluation, this constructs can emulate an
assignment operator similar to x <- value
. Due to R's precedence rules
of operators, future expressions often need to be explicitly bracketed,
e.g. x %<-% { a + b }
.
futureAssign()
and x %<-% expr
returns the Future invisibly,
e.g. f <- futureAssign("x", expr)
and f <- (x %<-% expr)
.
future()
and futureAssign()
take several arguments that can be used
to explicitly specify what global variables and packages the future should
use. They can also be used to override default behaviors of the future,
e.g. whether output should be relayed or not. When using a future
assignment, these arguments can be specified via corresponding
assignment expression. For example, x %<-% { rnorm(10) } %seed% TRUE
corresponds to futureAssign("x", { rnorm(10) }, seed = TRUE)
. Here are
a several examples.
To explicitly specify variables and functions that a future assignment
should use, use %globals%
. To explicitly specify which packages need
to be attached for the evaluate to success, use %packages%
. For
example,
> x <- rnorm(1000) > y %<-% { median(x) } %globals% list(x = x) %packages% "stats" > y [1] -0.03956372
The median()
function is part of the 'stats' package.
To declare that you will generate random numbers, use %seed%
, e.g.
> x %<-% { rnorm(3) } %seed% TRUE > x [1] -0.2590562 -1.2262495 0.8858702
To disable relaying of standard output (e.g. print()
, cat()
, and
str()
), while keeping relaying of conditions (e.g. message()
and
> x %<-% { cat("Hello\n"); message("Hi there"); 42 } %stdout% FALSE > y <- 13 > z <- x + y Hi there > z [1] 55
To disable relaying of conditions, use %conditions%
, e.g.
> x %<-% { cat("Hello\n"); message("Hi there"); 42 } %conditions% character(0) > y <- 13 > z <- x + y Hello > z [1] 55
> x %<-% { print(1:10); message("Hello"); 42 } %stdout% FALSE > y <- 13 > z <- x + y Hello > z [1] 55
To create a future without launching in such that it will only be
processed if the value is really needed, use %lazy%
, e.g.
> x %<-% { Sys.sleep(5); 42 } %lazy% TRUE > y <- sum(1:10) > system.time(z <- x + y) user system elapsed 0.004 0.000 5.008 > z [1] 97
Because future assignments are promises, errors produced by the the future expression will not be signaled until the value of the future is requested. For example, if you create a future assignment that produce an error, you will not be affected by the error until you "touch" the future-assignment variable. For example,
> x %<-% { stop("boom") } > y <- sum(1:10) > z <- x + y Error in eval(quote({ : boom
Futures are evaluated on the future backend that the user has specified
by plan()
. With regular futures, we can temporarily use another future
backend by wrapping our code in with(plan(...), { ... }]
, or temporarily
inside a function using with(plan(...), local = TRUE)
. To achieve the
same for a specific future assignment, use %plan%
, e.g.
> plan(multisession) > x %<-% { 42 } > y %<-% { 13 } %plan% sequential > z <- x + y > z [1] 55
Here x
is resolved in the background via the multisession backend,
whereas y
is resolved sequentially in the main R session.
The underlying Future of a future variable x
can be retrieved without
blocking using f <- futureOf(x)
, e.g.
> x %<-% { stop("boom") } > f_x <- futureOf(x) > resolved(f_x) [1] TRUE > x Error in eval(quote({ : boom > value(f_x) Error in eval(quote({ : boom
Technically, both the future and the variable (promise) are assigned at
the same time to environment assign.env
where the name of the future is
.future_<name>
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.