Learn R Programming

mirai

ミライ

Minimalist Async Evaluation Framework for R

→ Event-driven core with microsecond messaging

→ Scale from laptop to HPC and cloud — add or remove compute on the fly

→ Built for production — bounded queues, cancellation, distributed tracing

Installation

install.packages("mirai")

Quick Start

library(mirai)

# Launch 6 background R processes (daemons) to run tasks on
daemons(6)

# mirai() starts an async task and returns immediately
m <- mirai({ Sys.sleep(1); mean(rnorm(1e6)) })

# Task still running, but the console is free
unresolved(m)
#> [1] TRUE

# Async map: [] collects results; options for progress bar and flatmap
mirai_map(1:9, \(x) { Sys.sleep(0.5); x^2 })[.progress, .flat]
#> [1]  1  4  9 16 25 36 49 64 81

# m[] waits for and returns the result
m[]
#> [1] 0.001157286

# Shut down all daemons
daemons(0)

Architecture

mirai() sends tasks to daemons — persistent R worker processes. The host listens at a URL; daemons dial in and pull work via an in-process dispatcher thread that handles scheduling, cancellation, and bounded queues. Add or remove daemons at any time, and direct tasks to different compute profiles (CPU pool, GPU pool, remote cluster) from the same session.

Round-trip latency stays in the microseconds:

daemons(1)
bench::mark(mirai("hello world")[])
#> # A tibble: 1 × 6
#>   expression                      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                 <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 "mirai(\"hello world\")[]"   73.6µs   91.9µs    10301.    9.68KB     4.02
daemons(0)

Deploy

WhereSetup
Local machinedaemons(n)
SSH (direct or tunnelled)ssh_config()
HPC scheduler — Slurm, SGE, Torque/PBS, LSFcluster_config()
HTTP API — Posit Workbench, customhttp_config()
Anywhere elseremote_config()
daemons(
  n = 6,
  url = host_url(tls = TRUE),
  remote = cluster_config(options = "#SBATCH --mem=10G")
)

See the reference vignette for the full deployment guide.

What’s inside

  • Async — mirai(), mirai_map(), everywhere(), race_mirai(), try_mirai()
  • Collection — m[], collect_mirai(), call_mirai(), .flat, .progress, .stop
  • Promises — as.promise() for mirai and mirai_map; event-driven Shiny ExtendedTask
  • Cancellation & timeouts — stop_mirai(), .timeout, .stop
  • Backpressure — daemons(memory = …) capacity, peak watermark via status()$memory, non-blocking try_mirai()
  • Serialization — serial_config() for torch, Arrow, polars, ADBC; mori::share() for local shared memory
  • Reproducibility — L’Ecuyer-CMRG streams; daemons(seed = …) for deterministic parallel RNG
  • Observability — info(), status(), OpenTelemetry spans via otel
  • Compute profiles — independent daemon pools, with_daemons(), local_daemons()
  • R parallel cluster — parallel::makeCluster(type = "MIRAI") (R ≥ 4.5)

Across the R stack

mirai has become the shared async layer for the R ecosystem. It’s the recommended async backend for Shiny and the only one for plumber2, the engine behind purrr::in_parallel() and targets pipelines through crew, and is the first official alternative communications backend for base R’s parallel package.

Acknowledgements

Will Landau for being instrumental in shaping development of the package, from initiating the original request for persistent daemons, through to orchestrating robustness testing for the high performance computing requirements of crew and targets.

Joe Cheng for integrating the ‘promises’ method to work seamlessly within Shiny, and prototyping event-driven promises.

Luke Tierney of R Core, for discussion on L’Ecuyer-CMRG streams to ensure statistical independence in parallel processing, and reviewing mirai’s implementation as the first ‘alternative communications backend for R’.

Travers Ching for a novel idea in extending the original custom serialization support in the package.

Hadley Wickham, Henrik Bengtsson, Daniel Falbel, and Kirill Müller for many deep insights and discussions.

Links

mirai | nanonext | CRAN HPC Task View

AI coding agents: the r-lib agent skill from the posit-dev-skills plugin provides mirai-specific guidance.

–

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Copy Link

Version

Install

install.packages('mirai')

Monthly Downloads

48,548

Version

2.7.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Charlie Gao

Last Published

September 24th, 2026

Functions in mirai (2.7.3)

cluster_config

Cluster Remote Launch Configuration
daemons

Daemons (Set Persistent Processes)
call_mirai

mirai (Call Value)
everywhere

Evaluate Everywhere
.flat

mirai Map Options
daemon

Daemon Instance
collect_mirai

mirai (Collect Value)
as.promise.mirai

Make mirai Promise
as.promise.mirai_map

Make mirai_map Promise
daemons_set

Query if Daemons are Set
info

Information Statistics
make_cluster

Make Mirai Cluster
http_config

HTTP Remote Launch Configuration
mirai-package

mirai: Minimalist Async Evaluation Framework for R
host_url

URL Constructors
is_mirai_error

Error Validators
launch_local

Launch Daemons
is_mirai

Is mirai / mirai_map
race_mirai

mirai (Race)
serial_config

Create Serialization Configuration
mirai_map

mirai Map
mirai

mirai (Evaluate Async)
register_serial

Register Serialization Configuration
ssh_config

SSH Remote Launch Configuration
on_daemon

On Daemon
nextstream

Next >> Developer Interface
remote_config

Generic Remote Launch Configuration
require_daemons

Require Daemons
with_daemons

With Daemons
status

Status Information
stop_mirai

mirai (Stop)
with.miraiDaemons

With Mirai Daemons
unresolved

Query if a mirai is Unresolved