Skip to content

Allow user-provided NULL value in bplapply #269

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: BiocParallel
Type: Package
Title: Bioconductor facilities for parallel evaluation
Version: 1.41.2
Version: 1.41.3
Authors@R: c(
person("Martin", "Morgan",
email = "mtmorgan.bioc@gmail.com",
Expand Down
26 changes: 26 additions & 0 deletions R/bploop.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### utils

## a reserved value to represent a NULL value in X in bplapply
.bp_null <- function(){
structure(list(), class = "bp_null")
}

.is_bp_null <- function(x){
inherits(x, "bp_null")
}


### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Manager loop used by SOCK, MPI and FORK

Expand Down Expand Up @@ -81,6 +94,12 @@
.bploop_lapply_iter <-
function(X, redo_index, elements_per_task)
{
## replace the user-provided NULL with a reserved NULL
null_indices <- which(vapply(X, is.null, logical(1)))
for (null_index in null_indices) {
X[[null_index]] <- .bp_null()
}

redo_n <- length(redo_index)
redo_i <- 1L
x_n <- length(X)
Expand Down Expand Up @@ -270,6 +289,13 @@
warning("first invocation of 'ITER()' returned NULL")
break
}

## Replace the reserved NULL value with a real NULL
null_indices <- which(vapply(value, .is_bp_null, logical(1)))
for (null_index in null_indices) {
value[null_index] <- list(NULL)
}

args <- ARGFUN(value, seed)
task <- .EXEC(
total + 1L, .workerLapply,
Expand Down
9 changes: 9 additions & 0 deletions inst/unitTests/test_bplapply.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,12 @@ test_bplapply_auto_export <- function(){
bpexportvariables(p) <- FALSE
checkException(bplapply(1:2, fun2, BPPARAM = p), silent = TRUE)
}


test_bplapply_null_value_in_input <- function(){
p <- SerialParam()
FUN <- function(x) x
X <- list(a = 1, b = 2, c = NULL, d = 4)
result <- bptry(bplapply(X, FUN, BPPARAM = p))
checkIdentical(X, result)
}