Learn R Programming

future (version 0.12.0)

futureAssign: Create a future and assign its value to a variable as a promise

Description

Method and infix operators for creating futures and assigning their values as variables using promises. Trying to access such a "future variable" will correspond to requesting the value of the underlying future. If the the future is already resolved at this time, then the value will be available instantaneously and the future variable will appear as any other variable. If the future is unresolved, then the current process will block until the future is resolved and the value is available.

Usage

futureAssign(name, value, envir = parent.frame(), assign.env = envir,
  substitute = TRUE)

Arguments

name
the name of the variable (and the future) to assign.
value
the expression to be evaluated in the future and whose value will be assigned to the variable.
envir
The environment from which global variables used by the expression should be search for.
assign.env
The environment to which the variable should be assigned.
substitute
Controls whether expr should be substitute():d or not.

Value

Details

This function creates a future and a corresponding promise, which hold the future's value. Both the future and the promise are assigned to environment assign.env. The name of the promise is given by name and the name of the future is .future_. The future is also returned invisibly.

See Also

The futureOf() function can be used to get the Future object of a future variable.

Examples

Run this code
## Future assignment via "assign" function
futureAssign("A", {
  x <- 3
  x^2
})
cat("Value 'A': ", A, sep="")


## Equivalent via infix "assign" operator
A %<=% {
  x <- 3
  x^2
}
cat("Value 'A': ", A, sep="")


## A global variable
a <- 1

## Three future evaluations
A %<=% { 0.1 }
B %<=% { 0.2 }
C %<=% { z <- a+0.3 }

## Sleep until 'C' is available
cat("Value 'C': ", C, sep="")

## Sleep until 'A' is available
cat("Value 'A': ", A, sep="")

## Sleep until 'C' is available
cat("Value 'B': ", B, sep="")

Run the code above in your browser using DataLab