Learn R Programming

dipsaus (version 0.0.5)

lapply_async2: Apply, but in parallel

Description

Apply, but in parallel

Usage

lapply_async2(x, FUN, FUN.args = list(), callback = NULL, plan = TRUE, ...)

Arguments

x

vector, list

FUN

function to apply on each element of x

FUN.args

more arguments to feed into FUN

callback

function to run after each iteration

plan

logical, or character or future plan; see Details.

...

passed to plan

Value

same as with(FUN.args, lapply(x, function(el){eval(body(FUN))}))

Details

When plan is logical, FALSE means use current plan. If plan=TRUE, then it equals to plan='multicore'. For characters, plan can be 'multicore', 'callr', 'sequential', 'multisession', 'multiprocess', etc. Alternatively, you could pass future plan objects.

Examples

Run this code
# NOT RUN {
library(future)
plan(sequential)

# Use sequential plan
# 1. Change `plan` to 'multicore', 'multisession', or TRUE to enable
# multi-core, but still with progress information
# 2. Change plan=FALSE will use current future plan
res <- lapply_async2(100:200, function(x){
  return(x+1)
}, callback = function(e){
  sprintf('Input=%d', e)
}, plan = 'sequential')

# Disable callback message, then the function reduce to
# `future.apply::future_lapply`
res <- lapply_async2(100:200, function(x){
  return(x+1)
}, callback = NULL, plan = FALSE)


# }

Run the code above in your browser using DataLab