parfor.Rd
A flexible parallel/sequential loop implementation with progress tracking, proper argument passing, and comprehensive error handling.
parfor(
what,
args,
cl = NULL,
combine = c,
errorhandling = c("stop", "remove", "pass"),
verbose = FALSE,
show_progress = TRUE,
export = NULL,
packages = NULL,
cl_type = "PSOCK",
use_do_call = FALSE,
...
)
A function to apply to each element.
A vector or list of arguments to iterate over. For functions with multiple arguments, provide a list where each element contains the arguments for one call.
Number of cores to use. If `NULL` (default), runs sequentially. If -1, uses all available cores. Positive integers specify exact core count.
A function to combine results (default: `c`). Must be a function.
How to handle errors ("stop", "remove", or "pass").
Logical; if `TRUE`, prints debugging information.
Logical; if `TRUE`, shows progress bar.
Character vector of object names to export to workers (parallel only).
Character vector of package names to load on workers (parallel only).
Cluster type ("PSOCK", "FORK", etc). Default is "PSOCK" for cross-platform compatibility.
Logical; if `TRUE`, treats each element of args as a list of arguments to pass via do.call. If `FALSE`, passes each element as a single argument.
Additional arguments to pass to `what`.
Results combined according to `combine` function.
# Basic usage - single arguments
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 with 2 cores
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
# Multiple arguments using do.call style
args_list <- list(list(x=1, y=2), list(x=3, y=4), list(x=5, y=6))
parfor(function(x, y) x + y, args_list, use_do_call = TRUE)
#>
|
| | 0%
|
|======================= | 33%
|
|=============================================== | 67%
|
|======================================================================| 100%
#> [1] 3 7 11
# With additional fixed arguments
parfor(function(x, power) x^power, 1:5, power = 3)
#>
|
| | 0%
|
|============== | 20%
|
|============================ | 40%
|
|========================================== | 60%
|
|======================================================== | 80%
|
|======================================================================| 100%
#> [1] 1 8 27 64 125
# With error handling
parfor(function(x) if(x==3) stop("error") else x^2, 1:5,
errorhandling = "remove")
#>
|
| | 0%
|
|============== | 20%
|
|============================ | 40%
|
|========================================== | 60%
|
|======================================================== | 80%
|
|======================================================================| 100%
#> [1] 1 4 16 25