The future package provides a generic API for using futures in R. A future is a simple yet powerful mechanism to evaluate an R expression and retrieve its value at some point in time. Futures can be resolved in many different ways depending on which strategy is used. There are various types of synchronous and asynchronous futures to choose from in the future package.
This package, future.mirai, provides a type of futures that utilizes the mirai package.
For example,
> library(future.mirai)
> plan(mirai_multisession)
>
> x %<-% { Sys.sleep(5); 3.14 }
> y %<-% { Sys.sleep(5); 2.71 }
> x + y
[1] 5.85
This is obviously a toy example to illustrate what futures look like and how to work with them. For further examples on how to use futures, see the vignettes of the future package as well as those of future.apply and doFuture.
The future.mirai package implements a future backend wrapper for mirai.
Backend | Description | Alternative in future package |
---|---|---|
mirai_multisession |
parallel evaluation in a separate R process (on current machine) | plan(multisession) |
When using mirai
futures, each future is resolved in a fresh background R session which ends as soon as the value of the future has been collected. In contrast, multisession
futures are resolved in background R worker sessions that serve multiple futures over their life spans. The advantage with using a new R process for each future is that it is that the R environment is guaranteed not to be contaminated by previous futures, e.g. memory allocations, finalizers, modified options, and loaded and attached packages. The disadvantage, is an added overhead of launching a new R process.
The future package provides a demo using futures for calculating a set of Mandelbrot planes. The demo does not assume anything about what type of futures are used. The user has full control of how futures are evaluated. For instance, to use mirai_multisession
futures, run the demo as:
library(future.mirai)
plan(mirai_multisession)
demo("mandelbrot", package = "future", ask = FALSE)
R package future.mirai is only available via GitHub and can be installed in R as:
remotes::install_github("HenrikBengtsson/future.mirai", ref="main")
To install the pre-release version that is available in Git branch develop
on GitHub, use:
remotes::install_github("HenrikBengtsson/future.mirai", ref="develop")
This will install the package from source.