View source: R/parallelize_fun.R
| parallelize_fun | R Documentation |
Parallelize a function
parallelize_fun(
x,
fun,
cores = 1,
export_fun = NULL,
clean_result = FALSE,
throw_error = TRUE,
progress_bar_width = 10L,
timestamp_format = paste0("[", format(Sys.time(), "%Y-%m-%d %H:%M:%S"), "] "),
verbose = TRUE
)
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 |
export_fun |
The functions to export the function to workers. |
clean_result |
Whether to remove failed results from output.
If |
throw_error |
Whether to print detailed error information for failed results.
Default is |
progress_bar_width |
Width of the verbose progress bar in characters.
Default is |
timestamp_format |
Format string for timestamp display.
Default is |
verbose |
Whether to print the message.
Default is |
A list of computed results.
If clean_result = FALSE, failed results are included as error objects.
If clean_result = TRUE, only successful results are returned.
parallelize_fun(1:3, function(x) {
Sys.sleep(0.2)
x^2
})
parallelize_fun(list(1, 2, 3), function(x) {
Sys.sleep(0.2)
x^2
}, cores = 2)
# Examples with error handling
parallelize_fun(1:5, function(x) {
if (x == 3) stop("Error on element 3")
x^2
}, clean_result = FALSE)
parallelize_fun(1:5, function(x) {
if (x == 3) stop("Error on element 3")
x^2
}, clean_result = TRUE)
# Control error printing
parallelize_fun(1:5, function(x) {
if (x == 2) stop("Error on element 3")
if (x == 4) stop("Error on element 4")
x^2
})
parallelize_fun(1:5, function(x) {
if (x == 3) stop("Error on element 3")
x^2
}, throw_error = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.