Sequential or parallel for loop.

parfor(
  what,
  args,
  cl = NULL,
  combine = c,
  errorhandling = c("stop", "remove", "pass"),
  verbose = FALSE,
  show_progress = TRUE,
  export = NULL,
  ...
)

Arguments

what

A function.

args

A list of arguments.

cl

Number of cores to use. If NULL, the loop will be sequential. It -1, the number of cores will be detected automatically.

combine

A function to combine the results.

errorhandling

A character string specifying how to handle errors. Possible values are "stop", "remove", and "pass".

verbose

A logical indicating whether to print progress.

show_progress

A logical indicating whether to show a progress bar.

export

A list of objects to export to the workers.

...

Additional arguments to pass to what for foreach::foreach (excludind .combine, .errorhandling, .options.snow, .verbose, and .export).

Value

A list of results.

Examples


# Sequential
print(misc::parfor(function(x) x^2, 1:10))
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |======================================================================| 100%
#>  [1]   1   4   9  16  25  36  49  64  81 100

# Parallel
print(misc::parfor(function(x) x^2, 1:10, cl = 2))
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |======================================================================| 100%
#>  [1]   1   4   9  16  25  36  49  64  81 100