lapply_async2: Apply, but in parallel

Description Usage Arguments Details Value See Also Examples

View source: R/parallels-future.R

Description

Apply, but in parallel

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
lapply_async2(
  x,
  FUN,
  FUN.args = list(),
  callback = NULL,
  plan = TRUE,
  future.chunk.size = NULL,
  future.seed = sample.int(1, n = 1e+05 - 1),
  ...
)

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.

future.chunk.size, future.seed

see also future_lapply. If you want the callbacks to be called immediately after each loop, then set it to 1, which is not optimal but the only way right now.

...

passed to plan

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.

Value

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

See Also

make_forked_clusters

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
# normal `future.apply::future_lapply`
res <- lapply_async2(100:200, function(x){
  return(x+1)
}, callback = NULL, plan = FALSE)

if(interactive()) {

  # PID are different, meaning executing in different sessions
  lapply_async2(1:4, function(x){
    Sys.getpid()
  })
}

dipsaus documentation built on Sept. 6, 2021, 5:08 p.m.