https://github.com/mihaiconstantin/doparabar
An `R` package that provides a `foreach` parallel adaptor for `parabar` backends.
https://github.com/mihaiconstantin/doparabar
foreach parallel-computing r
Last synced: 3 months ago
JSON representation
An `R` package that provides a `foreach` parallel adaptor for `parabar` backends.
- Host: GitHub
- URL: https://github.com/mihaiconstantin/doparabar
- Owner: mihaiconstantin
- License: other
- Created: 2024-12-10T17:27:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-14T17:46:59.000Z (over 1 year ago)
- Last Synced: 2025-12-21T20:52:04.877Z (5 months ago)
- Topics: foreach, parallel-computing, r
- Language: R
- Homepage: https://parabar.mihaiconstantin.com
- Size: 386 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
README
Parallel Adaptor
foreach 🔌 parabar
The `doParabar` package acts as a
[`foreach`](https://CRAN.R-project.org/package=foreach) parallel adaptor for
[`parabar`](https://parabar.mihaiconstantin.com) backends. It provides a minimal
implementation for the `foreach::%dopar%` operator, enabling seamless
integration of the [`parabar`](https://parabar.mihaiconstantin.com) package with
the [`foreach`](https://CRAN.R-project.org/package=foreach) package.
## Installation
You can install `doParabar` directly from `CRAN` using the following command:
```r
# Install the package from `CRAN`.
install.packages("doParabar")
```
Alternatively, you can also install the latest development version from `GitHub`
via:
```r
# Install the package from `GitHub`.
remotes::install_github("mihaiconstantin/doParabar")
```
Then, load the package as usual using the `library` function:
```r
# Load the package.
library(doParabar)
```
**_Note._** By default, and for various reasons, the `doParabar` package does
not automatically load other packages. Instead, it is recommended to load the
[`foreach`](https://CRAN.R-project.org/package=foreach) and
[`parabar`](https://parabar.mihaiconstantin.com) packages explicitly in your
scripts (i.e., or add them to your `Imports` in the `DESCRIPTION` file when
developing an `R` package).
```r
# Load the `foreach` package.
library(foreach)
# Load the `parabar` package.
library(parabar)
```
**_Note._** Should you need to suppress the package startup messages (e.g., from
the [`parabar`](https://parabar.mihaiconstantin.com) package) you can use the
[`suppressPackageStartupMessages`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/message.html)
function (e.g., `suppressPackageStartupMessages(parabar)`).
## Usage
Below you can find a minimal example of how to use `doParabar` and
[`parabar`](https://parabar.mihaiconstantin.com) packages in your `R` scripts.
All examples below assume that you have already installed and loaded the
packages.
**_Tip._** For a more detailed discussion see the vignette "[Using `parabar`
with `foreach`](https://parabar.mihaiconstantin.com/articles/foreach)".
```r
# 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)
```
**_Note._** The `doParabar` package provides only a minimal implementation
for the `foreach::%dopar%` operator. If you need additional functionality,
please consider contributing to the package, or opening an issue on `GitHub`.
## Contributing
- Any contributions are welcome and greatly appreciated. Please open a [pull
request](https://github.com/mihaiconstantin/doParabar/pulls) on `GitHub`.
- To report bugs, or request new features, please open an
[issue](https://github.com/mihaiconstantin/doParabar/issues) on `GitHub`.
## License
- The package source code in this repository is licensed under the [MIT
license](https://opensource.org/license/mit).
-
The parabar and doParabar documentation, vignettes, and other website materials by Mihai Constantin are licensed under CC BY 4.0
.