Evaluate an expression asynchronously in a new background R process. This function will return immediately with a 'mirai', which will resolve to the evaluated result once complete.

eval_mirai(.expr, ..., .timeout)

mirai(.expr, ..., .timeout)

Arguments

.expr

an expression to evaluate in a new R process. This may be of arbitrary length, wrapped in {} if necessary.

...

named arguments specifying the variables contained in '.expr'.

.timeout

(optional) integer value in milliseconds. A 'mirai' will resolve to an 'errorValue' 5 (timed out) if evaluation exceeds this limit.

Value

A 'mirai' object.

Details

This function will return a 'mirai' object immediately.

The value of a 'mirai' may be accessed at any time at $data, and if yet to resolve, an 'unresolved' logical NA will be returned instead.

unresolved may also be used on a 'mirai', which returns TRUE only if a 'mirai' has yet to resolve and FALSE otherwise. This is suitable for use in control flow statements such as while or if.

Alternatively, to call (and wait for) the result, use call_mirai on the returned 'mirai' object. This will block until the result is returned.

The expression '.expr' will be evaluated in a new R process in a clean environment consisting of the named objects passed as '...' only.

mirai is an alias for eval_mirai.

Examples

m <- mirai(x + y + 1, x = 2, y = 3)
m
#> < mirai >
#>  - $data for evaluated result
m$data
#> 'unresolved' logi NA
Sys.sleep(0.2)
m$data
#> [1] 6

m <- mirai(as.matrix(df), df = data.frame(), .timeout = 1000)
call_mirai(m)$data
#> <0 x 0 matrix>

m <- mirai({
  res <- rnorm(n)
  res / rev(res)
}, n = 1e6)
while (unresolved(m)) {
  cat("unresolved\n")
  Sys.sleep(0.1)
}
#> unresolved
#> unresolved
#> unresolved
#> unresolved
str(m$data)
#>  num [1:1000000] 0.0164 -0.96 0.2568 2.7376 2.48 ...