This function can be used to export objects to a
backend created by start_backend().
Arguments
- backend
An object of class
Backendas returned by thestart_backend()function.- variables
A character vector of variable names to export to the backend.
- environment
An environment from which to export the variables. If no environment is provided, the
.GlobalEnvenvironment is used.
Value
The function returns void. It throws an error if the value provided for the
backend argument is not an instance of class Backend.
Details
This function is a convenience wrapper around the lower-lever API of
parabar aimed at developers. More specifically, this function
calls the export method on the provided
backend instance.
Examples
# Create an asynchronous backend.
backend <- start_backend(cores = 2, cluster_type = "psock", backend_type = "async")
# Check that the backend is active.
backend$active
#> [1] TRUE
# Check if there is anything on the backend.
peek(backend)
#> [[1]]
#> character(0)
#>
#> [[2]]
#> character(0)
#>
# Create a dummy variable.
name <- "parabar"
# Export the `name` variable in the current environment to the backend.
export(backend, "name", environment())
# Remove the dummy variable from the current environment.
rm(name)
# Check the backend to see that the variable has been exported.
peek(backend)
#> [[1]]
#> [1] "name"
#>
#> [[2]]
#> [1] "name"
#>
# Run an expression on the backend.
# Note that the symbols in the expression are resolved on the backend.
evaluate(backend, {
# Print the name.
print(paste0("Hello, ", name, "!"))
})
#> [[1]]
#> [1] "Hello, parabar!"
#>
#> [[2]]
#> [1] "Hello, parabar!"
#>
# Clear the backend.
clear(backend)
# Check that there is nothing on the backend.
peek(backend)
#> [[1]]
#> character(0)
#>
#> [[2]]
#> character(0)
#>
# Use a basic progress bar (i.e., see `parabar::Bar`).
configure_bar(type = "basic", style = 3)
# Run a task in parallel (i.e., approx. 1.25 seconds).
output <- par_sapply(backend, x = 1:10, fun = function(x) {
# Sleep a bit.
Sys.sleep(0.25)
# Compute and return.
return(x + 1)
})
# Print the output.
print(output)
#> [1] 2 3 4 5 6 7 8 9 10 11
# Stop the backend.
stop_backend(backend)
# Check that the backend is not active.
backend$active
#> [1] FALSE
