# Manually load the libraries.
library(doParabar)
library(parabar)
library(foreach)
# Create an asynchronous `parabar` backend.
backend <- start_backend(cores = 2, cluster_type = "psock", backend_type = "async")
# Register the backend with the `foreach` package for the `%dopar%` operator.
registerDoParabar(backend)
# Get the parallel backend name.
getDoParName()
# Check that the parallel backend has been registered.
getDoParRegistered()
# Get the current version of backend registration.
getDoParVersion()
# Get the number of cores used by the backend.
getDoParWorkers()
# Define some variables strangers to the backend.
x <- 10
y <- 100
z <- "Not to be exported."
# Used the registered backend to run a task in parallel via `foreach`.
results <- foreach(i = 1:300, .export = c("x", "y"), .combine = c) %dopar% {
# Sleep a bit.
Sys.sleep(0.01)
# Compute and return.
i + x + y
}
# Show a few results.
head(results, n = 10)
tail(results, n = 10)
# Verify that the variable `z` was not exported.
try(evaluate(backend, z))
# To make packages available on the backend, see the `.packages` argument.
# Stop the backend.
stop_backend(backend)
Run the code above in your browser using DataLab