Skills

R Mirai

Help users write correct R code for async, parallel, and distributed computing using...

Install

npx skills add posit-dev/skills --skill r-mirai -g -a claude-code -y
Repository
posit-dev/skills551
Category
Writing
Updated
7d ago
License
MIT
Context cost
Mediumabout 2,914 tokens
Runs scripts
No, instructions only

What it does

Help users write correct R code for async, parallel, and distributed computing using mirai. Use when users need to run R code asynchronously or in parallel, write mirai code with correct dependency passing, set up parallel workers, convert from future or parallel, use mirai_map, integrate with Shiny or promises, or configure cluster/HPC computing.

mirai is a minimalist R framework for async, parallel, and distributed evaluation, built on nanonext.

Core Principle: Explicit Dependency Passing

mirai evaluates expressions in a clean environment on a daemon process. Nothing from the calling environment is available unless passed explicitly, this is the #1 source of mistakes.

# WRONG: my_data and my_func are not available on the daemon
m <- mirai(my_func(my_data))

There are two ways to pass objects, and the names used must match the names referenced in the expression.

.args (recommended)

Objects in .args populate the expression's local evaluation environment, available directly by name inside the expression.

m <- mirai(my_func(my_data), .args = list(my_func = my_func, my_data = my_data))

... (dot-dot-dot)

Objects passed via ... are assigned to the daemon's global environment. Use this when objects need to be found by R's standard scoping rules (e.g., helper functions called by other functions).

m <- mirai(my_func(my_data), my_func = my_func, my_data = my_data)

Shortcut: pass the whole calling environment

# .args form, populates local eval env
process <- function(x, y) mirai(x + y, .args = environment())

# ... form, single unnamed environment, populates daemon global env
df_matrix <- function(x, y) mirai(as.matrix(rbind(x, y)), environment())

When to use which

ScenarioUse
Data and simple functions.args
Helper functions called by other functions that need lexical scoping...
Pass entire local scope to local eval env.args = environment()
Pass entire local scope to daemon global envmirai(expr, environment())
Large objects shared across many taskseverywhere() first, then reference by name

Common Mistakes

Unqualified package functions

Daemons start with no user packages loaded. Same applies inside mirai_map() callbacks.

Read the whole SKILL.md on GitHub

More in Writing