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

  • Asyncmirai(), mirai_map(), everywhere(), race_mirai(), try_mirai()
  • Collectionm[], collect_mirai(), call_mirai(), .flat, .progress, .stop
  • Promisesas.promise() for mirai and mirai_map; event-driven Shiny ExtendedTask
  • Cancellation & timeoutsstop_mirai(), .timeout, .stop
  • Backpressuredaemons(memory = …) capacity, peak watermark via status()$memory, non-blocking try_mirai()
  • Serializationserial_config() for torch, Arrow, polars, ADBC; mori::share() for local shared memory
  • Reproducibility — L’Ecuyer-CMRG streams; daemons(seed = …) for deterministic parallel RNG
  • Observabilityinfo(), status(), OpenTelemetry spans via otel
  • Compute profiles — independent daemon pools, with_daemons(), local_daemons()
  • R parallel clusterparallel::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

70,288

Version

2.7.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Charlie Gao

Last Published

July 20th, 2026

Functions in mirai (2.7.2)

serial_config

Create Serialization Configuration
http_config

HTTP Remote Launch Configuration
register_serial

Register Serialization Configuration
race_mirai

mirai (Race)
status

Status Information
with.miraiDaemons

With Mirai Daemons
stop_mirai

mirai (Stop)
unresolved

Query if a mirai is Unresolved
with_daemons

With Daemons
require_daemons

Require Daemons
remote_config

Generic Remote Launch Configuration
ssh_config

SSH Remote Launch Configuration
nextstream

Next >> Developer Interface
on_daemon

On Daemon
daemons

Daemons (Set Persistent Processes)
as.promise.mirai

Make mirai Promise
daemon

Daemon Instance
.flat

mirai Map Options
as.promise.mirai_map

Make mirai_map Promise
daemons_set

Query if Daemons are Set
cluster_config

Cluster Remote Launch Configuration
collect_mirai

mirai (Collect Value)
call_mirai

mirai (Call Value)
everywhere

Evaluate Everywhere
is_mirai_error

Error Validators
launch_local

Launch Daemons
host_url

URL Constructors
mirai

mirai (Evaluate Async)
mirai_map

mirai Map
is_mirai

Is mirai / mirai_map
info

Information Statistics
make_cluster

Make Mirai Cluster
mirai-package

mirai: Minimalist Async Evaluation Framework for R