In this post, a way to overcome one of R’s limitations (lack of multi-threading) is discussed by job queuing using the jobqueue package - a generic asynchronous job queue implementation for R. See the package description below.
The jobqueue package is meant to provide an easy-to-use interface that allows to queue computations for background evaluation while the calling R session remains responsive. It is based on a 1-node socket cluster from the parallel package. The package provides a way to do basic threading in R. The main focus of the package is on an intuitive and easy-to-use interface for the job queue programming construct. … Typical applications include: background computation of lengthy tasks (such as data sourcing, model fitting, bootstrapping), simple/interactive parallelization (if you have 5 different jobs, move them to up to 5 different job queues), and concurrent task scheduling in more complicated R programs. …
Added to the typical applications indicated above, this package can be quite beneficial with a Shiny application especially when long-running process has to be served.
The package is not on CRAN and it can be installed as following.
As can be seen in the description, it is highly related to the parallel package and thus it wouldn’t be hard to understand how it works if you know how to do parallel processing using that package - if not, have a look at this post.
Here is a quick example of job queue. In the following function, execution is suspended for 1 second at each iteration and the processed is blocking until it is executed in base R.
Using the package, however, the function can be executed asynchronously as shown below.
Another example of applying job queue is fitting a bootstrap-based algorithm. In this example, each of 500 trees are grown and they are combined at the end - note that, in practice, it’d be better to save outputs and combine them later.