| ProgressTrackingContext | R Documentation |
This class represents a progress tracking context for interacting with
Backend implementations via the BackendService
interface.
This class extends the base Context class and overrides the
sapply parent method to decorate the backend instance
with additional functionality. Specifically, this class creates a temporary
file to log the progress of backend tasks, and then creates a progress bar to
display the progress of the backend tasks.
The progress bar is updated after each backend task execution. The timeout
between subsequent checks of the temporary log file is controlled by the
Options class and defaults to 0.001. This value can be
adjusted via the Options instance present in the session
base::.Options list (i.e., see set_option()). For example, to
set the timeout to 0.1 we can run set_option("progress_timeout", 0.1).
This class is a good example of how to extend the base Context
class to decorate the backend instance with additional functionality.
parabar::BackendService -> parabar::Context -> ProgressTrackingContext
barThe Bar instance registered with the context.
set_backend()Set the backend instance to be used by the context.
ProgressTrackingContext$set_backend(backend)
backendAn object of class Backend that supports
progress tracking implements the BackendService
interface.
This method overrides the parent method to validate the backend
provided and guarantee it is an instance of the
AsyncBackend class.
set_bar()Set the Bar instance to be used by the context.
ProgressTrackingContext$set_bar(bar)
barAn object of class Bar.
configure_bar()Configure the Bar instance registered with the context.
ProgressTrackingContext$configure_bar(...)
...A list of named arguments passed to the create() method
of the Bar instance. See the documentation of the
specific concrete bar for details (e.g., ModernBar).
sapply()Run a task on the backend akin to parallel::parSapply(), but with
a progress bar.
ProgressTrackingContext$sapply(x, fun, ...)
xAn atomic vector or list to pass to the fun function.
funA function to apply to each element of x.
...Additional arguments to pass to the fun function.
This method returns void. The output of the task execution must be
stored in the private field .output on the Backend
abstract class, and is accessible via the get_output() method.
lapply()Run a task on the backend akin to parallel::parLapply(), but with
a progress bar.
ProgressTrackingContext$lapply(x, fun, ...)
xAn atomic vector or list to pass to the fun function.
funA function to apply to each element of x.
...Additional arguments to pass to the fun function.
This method returns void. The output of the task execution must be
stored in the private field .output on the Backend
abstract class, and is accessible via the get_output() method.
apply()Run a task on the backend akin to parallel::parApply().
ProgressTrackingContext$apply(x, margin, fun, ...)
xAn array to pass to the fun function.
marginA numeric vector indicating the dimensions of x the
fun function should be applied over. For example, for a matrix,
margin = 1 indicates applying fun rows-wise, margin = 2
indicates applying fun columns-wise, and margin = c(1, 2)
indicates applying fun element-wise. Named dimensions are also
possible depending on x. See parallel::parApply() and
base::apply() for more details.
funA function to apply to x according to the margin.
...Additional arguments to pass to the fun function.
This method returns void. The output of the task execution must be
stored in the private field .output on the Backend
abstract class, and is accessible via the get_output() method.
clone()The objects of this class are cloneable with this method.
ProgressTrackingContext$clone(deep = FALSE)
deepWhether to make a deep clone.
Context, BackendService, Backend, and
AsyncBackend.
# Define a task to run in parallel.
task <- function(x, y) {
# Sleep a bit.
Sys.sleep(0.15)
# Return the result of a computation.
return(x + y)
}
# Create a specification object.
specification <- Specification$new()
# Set the number of cores.
specification$set_cores(cores = 2)
# Set the cluster type.
specification$set_type(type = "psock")
# Create a backend factory.
backend_factory <- BackendFactory$new()
# Get a backend instance that does not support progress tracking.
backend <- backend_factory$get("sync")
# Create a progress tracking context object.
context <- ProgressTrackingContext$new()
# Attempt to set the incompatible backend instance.
try(context$set_backend(backend))
# Get a backend instance that does support progress tracking.
backend <- backend_factory$get("async")
# Register the backend with the context.
context$set_backend(backend)
# From now all, all backend operations are intercepted by the context.
# Start the backend.
context$start(specification)
# Create a bar factory.
bar_factory <- BarFactory$new()
# Get a modern bar instance.
bar <- bar_factory$get("modern")
# Register the bar with the context.
context$set_bar(bar)
# Configure the bar.
context$configure_bar(
show_after = 0,
format = " > completed :current out of :total tasks [:percent] [:elapsed]"
)
# Run a task in parallel (i.e., approx. 1.9 seconds).
context$sapply(x = 1:25, fun = task, y = 10)
# Get the task output.
backend$get_output(wait = TRUE)
# Change the bar type.
bar <- bar_factory$get("basic")
# Register the bar with the context.
context$set_bar(bar)
# Remove the previous bar configuration.
context$configure_bar()
# Run a task in parallel (i.e., approx. 1.9 seconds).
context$sapply(x = 1:25, fun = task, y = 10)
# Get the task output.
backend$get_output(wait = TRUE)
# Close the backend.
context$stop()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.