if (interactive()) {
# Only run examples in interactive R sessions
daemons(4, dispatcher = FALSE)
# map with constant args specified via '.args'
mirai_map(1:3, rnorm, .args = list(mean = 20, sd = 2))[]
# flatmap with function definition passed via '...'
mirai_map(1:3, function(x) func(1L, x, x + 1L), func = stats::runif)[.flat]
# sum slices of a list of vectors
(listvec <- list(1:3, c(4, 3, 2)))
mirai_map(listvec, sum)[.flat]
# sum rows of a matrix
(mat <- matrix(1:4, nrow = 2L))
mirai_map(mat, sum)[.flat]
# map over rows of a dataframe
df <- data.frame(a = c("Aa", "Bb"), b = c(1L, 4L))
mirai_map(df, function(...) sprintf("%s: %d", ...))[.flat]
# indexed map over a vector
v <- c("egg", "got", "ten", "nap", "pie")
mirai_map(list(1:length(v), v), sprintf, .args = list(fmt = "%d_%s"))[.flat]
# return a 'mirai_map' object, check for resolution, collect later
mp <- mirai_map(
c(a = 2, b = 3, c = 4),
function(x, y) do(x, as.logical(x %% y)),
do = nanonext::random,
.args = list(y = 2)
)
unresolved(mp)
mp
mp[]
unresolved(mp)
# progress indicator counts up from 0 to 4 seconds
res <- mirai_map(1:4, Sys.sleep)[.progress]
daemons(0)
# generates warning as daemons not set
# stops early when second element returns an error
tryCatch(
mirai_map(list(list(1, "a", 3), 3:1), sum)[.stop],
error = identity
)
# promises example that outputs the results, including errors, to the console
if (requireNamespace("promises", quietly = TRUE)) {
daemons(1, dispatcher = FALSE)
ml <- mirai_map(
1:30,
function(x) {Sys.sleep(0.1); if (x == 30) stop(x) else x},
.promise = list(
function(x) cat(paste(x, "")),
function(x) { cat(conditionMessage(x), "\n"); daemons(0) }
)
)
}
}
Run the code above in your browser using DataLab