Parallelize a function
Arguments
- x
A vector or list to apply over.
- fun
The function to be applied to each element.
- cores
The number of cores to use for parallelization with foreach::foreach. Default is
1
.- export_fun
The functions to export the function to workers.
- verbose
Logical value, default is
TRUE
. Whether to print progress messages.
Examples
parallelize_fun(1:3, function(x) {
Sys.sleep(0.2)
x^2
})
#> ℹ [2025-07-06 07:15:17] Using "1" core
#> ⠙ [2025-07-06 07:15:17] Running [1/3] ETA: 0s
#> ✔ [2025-07-06 07:15:17] Completed 3 tasks in 619ms
#>
#> ℹ [2025-07-06 07:15:18] Building results
#> $`1`
#> [1] 1
#>
#> $`2`
#> [1] 4
#>
#> $`3`
#> [1] 9
#>
parallelize_fun(list(1, 2, 3), function(x) {
Sys.sleep(0.2)
x^2
}, cores = 2)
#> ℹ [2025-07-06 07:15:18] Using "2" cores
#> ℹ [2025-07-06 07:15:18] Building results
#> $`1`
#> [1] 1
#>
#> $`2`
#> [1] 9
#>
#> $`3`
#> [1] 4
#>