Skip to contents

Submit code to an active mirai daemon setup while preserving key session context from the current R session.

Usage

mirai_launch(
  code,
  import_objects = c(),
  import_path = NULL,
  project_continue = g6tr_is_project(),
  task_id = mirai_task_name(),
  tags = NULL,
  status_interval = 10,
  cleanup = FALSE
)

Arguments

code

Code to execute, enclosed in {}.

import_objects

A c() expression (e.g. c(i, j)) listing environment objects to import into worker evaluation environment.

import_path

Optional .RData file path to load before evaluation. Must end with .RData.

project_continue

Whether to run g6tr::g6tr_project_continue() before evaluating code. Defaults to g6tr_is_project().

task_id

Optional task identifier. Defaults to mirai_task_name().

tags

Optional character vector of tags to store with the task.

status_interval

Status snapshot interval in seconds. Set to NULL or 0 to disable periodic snapshots.

cleanup

Logical. If TRUE, remove the local task folder after a successful run. This is useful for side-effect tasks that write to the datalake or Google Drive and do not need local artifacts afterward.

Value

A task id (character). The underlying mirai object is stored as an attribute named "mirai".

Details

Use mirai::info() and mirai::status() to inspect active daemons while submitted tasks are running. To shut down all daemons in the current session, run mirai::daemons(0). mirai_launch() returns a task id; use mirai_status(), mirai_output(), and mirai_log() to inspect task progress, results, and logs.

This function can:

  • load objects from an .RData file via import_path

  • inject selected objects from the current environment via import_objects

  • set the worker process environment variables to the current values

  • switch worker working directory to the current getwd()

  • run g6tr::g6tr_project_continue() when project_continue = TRUE

  • save the result to output.rds in the task folder

  • optionally remove the local task folder after successful completion

Examples

if (FALSE) { # \dontrun{
mirai_ls_partitions()
mirai_start_daemons(n = 2)
mirai_ls_daemons()

x <- 10
task_id <- mirai_launch({
cli::cli_alert_info("Task started!")
out <- x + 2
cli::cli_alert_info("Task done! Returning output")
out
}, import_objects = c(x), tags = c("example", "demo"))
mirai_ls() # List all tasks
mirai_status(task_id) # Get task status tibble
mirai_log(task_id) # Get console log
mirai_output(task_id) # Get returned output

# a longer-running task (5 minutes) with CPU + memory spike
task_id_long <- mirai_launch({
x <- rnorm(5e7)
for (i in 1:10) {
cli::cli_alert_info("Starting {i}")
x <- sqrt(x^2 + 1)
Sys.sleep(10)
}
cli::cli_alert_success("Done! Waiting 55 seconds before return")
Sys.sleep(55)
mean(x)
}, cleanup = TRUE)
mirai_ls()
mirai_status(task_id_long) # Every 10 seconds, new entry should be added
mirai_log(task_id_long)
mirai_output(task_id_long)

mirai_launch({Sys.sleep(60); 3.14}, task_id = "pi")
mirai_output("pi")
mirai_status("pi")

mirai_rm()
mirai_stop_daemons()
} # }