Skip to content

Commit 4aac09c

Browse files
Add registerDoFuture(flavor = "%dofuture%")
1 parent 179005d commit 4aac09c

File tree

6 files changed

+89
-14
lines changed

6 files changed

+89
-14
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: doFuture
2-
Version: 1.0.2-9001
2+
Version: 1.0.2-9002
33
Title: Use Foreach to Parallelize via the Future Framework
44
Depends:
55
foreach (>= 1.5.0),

NEWS.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
# Version 1.0.2-9000 (2025-03-15)
1+
# Version 1.0.2-9001 (2025-04-30)
22

3-
* ...
3+
## New Features
4+
5+
* Add `registerDoFuture(flavor = "%dofuture%")`, which makes the
6+
`%dopar%` infix operator behave as if `%dofuture%` would have been
7+
used. This makes it possible for you to use `%dofuture%`, even if
8+
you do not have the option to update the code that uses
9+
`%dopar%`. For instance, if you use a package that uses
10+
`foreach(...) %dopar% { ... }` internally, this flavor allows you to
11+
effectively make that the same as `foreach(...) %dofuture% { ... }`.
12+
This is particularly useful if you suspect that the code does not
13+
account for random number generation (RNG), where it ideally should
14+
use `%dorng%` of the **doRNG** package instead of `%dopar%`. Using
15+
`%dofuture%` resolves such issues, because it will use a proper
16+
parallel RNG set by the futureverse ecosystem.
417

518

619
# Version 1.0.2 (2025-03-15)

R/registerDoFuture.R

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#' Use the Foreach `%dopar%` Adapter with Futures
22
#'
3+
#' @param flavor Control how the adapter should behave.
4+
#' If `"%dopar%"`, it behaves as a classical foreach adapter.
5+
#' If `"%dofuture%"`, it behaves as if `%dofuture%` would have
6+
#' been used instead of `%dopar%`.
7+
#'
38
#' The `registerDoFuture()` function makes the
49
#' \code{\link[foreach:\%dopar\%]{\%dopar\%}} operator of the
510
#' \pkg{foreach} package to process foreach iterations via any of
@@ -189,10 +194,22 @@
189194
#' @importFrom utils packageVersion
190195
#' @export
191196
#' @keywords utilities
192-
registerDoFuture <- function() { #nolint
197+
registerDoFuture <- function(flavor = c("%dopar%", "%dofuture%")) { #nolint
198+
flavor <- match.arg(flavor, several.ok = FALSE)
199+
200+
if (flavor == "%dopar%") {
201+
name <- "doFuture"
202+
doFcn <- doFuture
203+
} else if (flavor == "%dofuture%") {
204+
name <- "doFuture2"
205+
doFcn <- function(obj, expr, envir, data) {
206+
doFuture2(obj, expr = expr, envir = envir, data = NULL)
207+
}
208+
}
209+
193210
info <- function(data, item) {
194211
switch(item,
195-
name = "doFuture",
212+
name = name,
196213
version = packageVersion("doFuture"),
197214
workers = nbrOfWorkers(),
198215
)
@@ -217,7 +234,7 @@ registerDoFuture <- function() { #nolint
217234
## is supported. /HB 2020-12-28
218235
oldDoPar <- .getDoPar()
219236

220-
setDoPar(doFuture, info = info)
237+
setDoPar(doFcn, info = info)
221238

222239
invisible(oldDoPar)
223240
}

man/doFuture.options.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/registerDoFuture.Rd

Lines changed: 15 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
source("incl/start.R")
2+
3+
strategies <- future:::supportedStrategies()
4+
strategies <- setdiff(strategies, "multiprocess")
5+
6+
message("*** registerDoFuture(flavor = \"%dofuture%\") - reproducibility ...")
7+
8+
registerDoFuture(flavor = "%dofuture%")
9+
10+
res0 <- NULL
11+
12+
for (strategy in strategies) {
13+
message(sprintf("- plan('%s') ...", strategy))
14+
plan(strategy)
15+
16+
mu <- 1.0
17+
sigma <- 2.0
18+
res <- foreach(i = 1:3, .options.future = list(packages = "stats")) %dopar% {
19+
dnorm(i, mean = mu, sd = sigma)
20+
}
21+
print(res)
22+
23+
if (is.null(res0)) {
24+
res0 <- res
25+
} else {
26+
stopifnot(all.equal(res, res0))
27+
}
28+
29+
# Shutdown current plan
30+
plan(sequential)
31+
32+
message(sprintf("- plan('%s') ... DONE", strategy))
33+
} ## for (strategy ...)
34+
35+
message("*** registerDoFuture(flavor = \"%dofuture%\") - reproducibility ... DONE")
36+
37+
source("incl/end.R")

0 commit comments

Comments
 (0)