Skip to content

Commit 2e839fb

Browse files
authored
remove support for foreach (#237)
* remove support for foreach * raise a warning when only foreach is used
1 parent 43ee07e commit 2e839fb

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Depends:
2020
Imports:
2121
butcher (>= 0.1.3),
2222
cli,
23-
doFuture,
2423
dplyr (>= 1.1.0),
2524
foreach,
25+
furrr,
2626
future,
2727
generics,
2828
ggplot2,

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* Added missing commas and addressed formatting issues throughout the vignettes and articles. Backticks for package names were removed and missing parentheses for functions were added (@Joscelinrocha, #218).
77

88
* Increased the minimum R version to R 4.1.
9+
10+
* Transitioned support for parallel processing fully to the
11+
[future](https://www.futureverse.org/) framework. Parallelism backends
12+
registered with foreach will be ignored with a warning (#234).
913

1014
# stacks 1.0.5
1115

R/fit_members.R

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,32 +129,20 @@ fit_members <- function(model_stack, ...) {
129129
)
130130
}
131131

132-
if (foreach::getDoParWorkers() > 1 || future::nbrOfWorkers() > 1) {
133-
`%do_op%` <- switch(
134-
# note some backends can return +Inf
135-
min(future::nbrOfWorkers(), 2),
136-
foreach::`%dopar%`,
137-
doFuture::`%dofuture%`
138-
)
139-
} else {
140-
`%do_op%` <- foreach::`%do%`
132+
if (uses_foreach_only()) {
133+
warn_foreach_deprecation()
141134
}
142135

143136
# fit each of them
144137
member_fits <-
145-
foreach::foreach(
146-
mem = member_names,
147-
.inorder = FALSE,
148-
.options.future = list(seed = TRUE)
149-
) %do_op%
150-
{
151-
asNamespace("stacks")$fit_member(
152-
name = mem,
153-
wflows = model_stack[["model_defs"]],
154-
members_map = members_map,
155-
train_dat = dat
156-
)
157-
}
138+
furrr::future_map(
139+
member_names,
140+
.f = fit_member,
141+
wflows = model_stack[["model_defs"]],
142+
members_map = members_map,
143+
train_dat = dat,
144+
.options = furrr::furrr_options(seed = TRUE)
145+
)
158146

159147
model_stack[["member_fits"]] <-
160148
setNames(member_fits, member_names)
@@ -269,7 +257,8 @@ check_for_required_packages <- function(x) {
269257

270258
purrr::map(
271259
pkgs,
272-
function(.x) suppressPackageStartupMessages(requireNamespace(.x, quietly = TRUE))
260+
function(.x)
261+
suppressPackageStartupMessages(requireNamespace(.x, quietly = TRUE))
273262
)
274263

275264
invisible(TRUE)
@@ -290,3 +279,17 @@ error_needs_install <- function(pkgs, installed, call) {
290279
is_installed_ <- function(pkg) {
291280
rlang::is_installed(pkg)
292281
}
282+
283+
uses_foreach_only <- function() {
284+
future::nbrOfWorkers() == 1 && foreach::getDoParWorkers() > 1
285+
}
286+
287+
warn_foreach_deprecation <- function() {
288+
cli::cli_warn(c(
289+
"!" = "{.pkg stacks} detected a parallel backend registered with \\
290+
foreach but no backend registered with future.",
291+
"i" = "Support for parallel processing with foreach was \\
292+
deprecated in {.pkg stacks} 1.0.6.",
293+
"i" = "See {.help tune::parallelism} to learn more."
294+
))
295+
}

R/utils.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ utils::globalVariables(c(
2121
".metric",
2222
".pred",
2323
".pred_class",
24+
".sum",
2425
"across",
2526
"any_of",
2627
"as.formula",

0 commit comments

Comments
 (0)