| par_sapply {parabar} | R Documentation |
Run a Task in Parallel
Description
This function can be used to run a task in parallel. The task is executed in
parallel on the specified backend, similar to parallel::parSapply(). If
backend = NULL, the task is executed sequentially using base::sapply().
See the Details section for more information on how this function works.
Usage
par_sapply(backend = NULL, x, fun, ...)
Arguments
backend |
An object of class |
x |
An atomic vector or list to pass to the |
fun |
A function to apply to each element of |
... |
Additional arguments to pass to the |
Details
This function uses the UserApiConsumer class that acts like an
interface for the developer API of the parabar package.
Value
A vector of the same length as x containing the results of the fun. The
output format resembles that of base::sapply().
See Also
start_backend(), peek(), export(),
evaluate(), clear(), configure_bar(),
par_lapply(), par_apply(), stop_backend(),
set_option(), get_option(), Options,
UserApiConsumer, and Service.
Examples
# Define a simple task.
task <- function(x) {
# Perform computations.
Sys.sleep(0.01)
# Return the result.
return(x + 1)
}
# Start an asynchronous backend.
backend <- start_backend(cores = 2, cluster_type = "psock", backend_type = "async")
# Run a task in parallel.
results <- par_sapply(backend, x = 1:300, fun = task)
# Disable progress tracking.
set_option("progress_track", FALSE)
# Run a task in parallel.
results <- par_sapply(backend, x = 1:300, fun = task)
# Enable progress tracking.
set_option("progress_track", TRUE)
# Change the progress bar options.
configure_bar(type = "modern", format = "[:bar] :percent")
# Run a task in parallel.
results <- par_sapply(backend, x = 1:300, fun = task)
# Stop the backend.
stop_backend(backend)
# Start a synchronous backend.
backend <- start_backend(cores = 2, cluster_type = "psock", backend_type = "sync")
# Run a task in parallel.
results <- par_sapply(backend, x = 1:300, fun = task)
# Disable progress tracking to remove the warning that progress is not supported.
set_option("progress_track", FALSE)
# Run a task in parallel.
results <- par_sapply(backend, x = 1:300, fun = task)
# Stop the backend.
stop_backend(backend)
# Run the task using the `base::sapply` (i.e., non-parallel).
results <- par_sapply(NULL, x = 1:300, fun = task)