Skip to content

Commit

Permalink
Add temporary patch for 'flexFitR'
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Feb 9, 2025
1 parent 2fbe7a4 commit 0e71e16
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: doFuture
Version: 1.0.1-9008
Version: 1.0.1-9009
Title: Use Foreach to Parallelize via the Future Framework
Depends:
foreach (>= 1.5.0),
Expand Down
14 changes: 12 additions & 2 deletions R/dofuture_OP.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ doFuture2 <- function(obj, expr, envir, data) { #nolint
mprint(expr)
}

## Patch expression for flexFitR?
expr <- flexFitR_patch(expr)

## WORKAROUND: foreach::times() passes an empty string in 'argnames'
argnames <- it$argnames
argnames <- argnames[nzchar(argnames)]
Expand Down Expand Up @@ -426,6 +429,13 @@ doFuture2 <- function(obj, expr, envir, data) { #nolint
assign("...future.x_ii", 42, envir = globals_envir, inherits = FALSE)
add <- c(add, "...future.x_ii")

if (isTRUE(attr(expr, "patched"))) {
if (debug) mdebug(" - flexFitR patch: rename global 'fn' to 'fn2'")
value <- globalsByName("fn", envir = envir)$fn
assign("fn2", value, envir = globals_envir, inherits = FALSE)
add <- c(add, "fn2")
}

ignore <- attr(globals, "ignore", exact = TRUE)
ignore <- c(ignore, argnames)

Expand Down Expand Up @@ -473,7 +483,7 @@ doFuture2 <- function(obj, expr, envir, data) { #nolint
}

globals <- globals_mapreduce

if (debug) {
mdebugf(" - globals: [%d] %s", length(globals),
paste(sQuote(names(globals)), collapse = ", "))
Expand Down Expand Up @@ -514,7 +524,7 @@ doFuture2 <- function(obj, expr, envir, data) { #nolint
args_list_ii <- args_list[chunk]
globals_ii[["...future.x_ii"]] <- args_list_ii

if (debug) mdebugf(" - Finding globals in 'args_list' chunk #%d ...", ii)
if (debug) mdebugf(" - Finding globals in 'args_list' for chunk #%d ...", ii)
## Search for globals in 'args_list_ii':
gp <- getGlobalsAndPackages(args_list_ii, envir = envir, globals = TRUE)
globals_X <- gp$globals
Expand Down
52 changes: 51 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ patch_expressions <- function() {
if ("WARDEN" %in% loadedNamespaces()) {
patches <- c(patches, "WARDEN")
}
## Package 'flexFitR'
if ("flexFitR" %in% loadedNamespaces()) {
patches <- c(patches, "flexFitR")
}
}
options(doFuture.patches = patches)
}
Expand All @@ -36,6 +40,40 @@ patch_expressions <- function() {
}


flexFitR_tweak_modeler_expr <- function(expr) {
if (!is.call(expr)) return(expr)
expr <- unclass(expr)
op <- expr[[1]]
if (!is.symbol(op)) return(expr)
if (length(expr) != 3L) return(expr)
e <- expr[[3]]
if (length(e) != 10L) return(expr)
op <- e[[1]]
if (!is.symbol(op)) return(expr)
if (as.character(op) != ".fitter_curve") return(expr)
op <- e[[4]]
if (!is.symbol(op)) return(expr)
if (as.character(op) != "fn") return(expr)
e[[4]] <- as.symbol("fn2")
expr[[3]] <- e
attr(expr, "patched") <- TRUE
expr
}

flexFitR_patch <- local({
patch <- NULL
function(expr) {
if (is.null(patch)) {
patch_expressions()
patches <- getOption("doFuture.patches")
patch <<- ("flexFitR" %in% patches)
}
if (!patch) return(expr)
expr <- flexFitR_tweak_modeler_expr(expr)
expr
}
})


## covr: skip=all
.onLoad <- function(libname, pkgname) {
Expand All @@ -51,7 +89,7 @@ patch_expressions <- function() {
value <- getOption("doFuture.globals.scanVanillaExpression")
if (is.null(value)) {
value <- Sys.getenv("R_DOFUTURE_GLOBALS_SCANVANILLAEXPRESSION", NA_character_)
if (is.na(value)) {
if (is.na(value) || !nzchar(value)) {
value <- future_has_evalFuture()
} else {
value <- trim(value)
Expand All @@ -60,4 +98,16 @@ patch_expressions <- function() {
}
options(doFuture.globals.scanVanillaExpression = value)
}

## doFuture 1.1.0
value <- getOption("doFuture.patches")
if (is.null(value)) {
value <- Sys.getenv("R_DOFUTURE_PATCHES", NA_character_)
if (is.na(value) || !nzchar(value)) {
value <- NULL
} else {
value <- strsplit(value, split = ",", fixed = TRUE)[[1]]
}
options(doFuture.patches = value)
}
}

0 comments on commit 0e71e16

Please sign in to comment.