Skip to content

Commit

Permalink
Add early sanity check on some delayed operations
Browse files Browse the repository at this point in the history
This sanity check will trigger an error in situations like:
  M <- DelayedArray(matrix(character(12), ncol=3))
  M2 <- log(M)  # error!
Before this change, the above code was working and the error was delayed
until the user would display or realize M2. The check is cheap so better
fail early than late!
  • Loading branch information
hpages committed Aug 16, 2018
1 parent ecddbd7 commit 70fd89a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Description: Wrapping an array-like object (typically an on-disk object) in
also works on in-memory array-like objects like DataFrame objects
(typically with Rle columns), Matrix objects, and ordinary arrays and
data frames.
Version: 0.7.29
Version: 0.7.30
Encoding: UTF-8
Author: Hervé Pagès <hpages@fredhutch.org>, with contributions from
Peter Hickey <peter.hickey@gmail.com>
Expand Down
3 changes: 2 additions & 1 deletion R/DelayedArray-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ stash_DelayedUnaryIsoOp <- function(x, OP, Largs=list(), Rargs=list(),
{
stopifnot(is(x, "DelayedArray"))
op <- new_DelayedUnaryIsoOp(x@seed, OP=OP, Largs=Largs, Rargs=Rargs,
Lalong=Lalong, Ralong=Ralong)
Lalong=Lalong, Ralong=Ralong,
check.op=TRUE)
DelayedArray(op)
}

Expand Down
20 changes: 17 additions & 3 deletions R/DelayedOp-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ setClass("DelayedUnaryIsoOp",

new_DelayedUnaryIsoOp <- function(seed=new("array"),
OP=identity, Largs=list(), Rargs=list(),
Lalong=NA, Ralong=NA)
Lalong=NA, Ralong=NA,
check.op=FALSE)
{
seed_dim <- dim(seed)
if (length(seed_dim) == 0L)
Expand All @@ -417,8 +418,21 @@ new_DelayedUnaryIsoOp <- function(seed=new("array"),

OP <- match.fun(OP)

new2("DelayedUnaryIsoOp", seed=seed, OP=OP, Largs=Largs, Rargs=Rargs,
Lalong=Lalong, Ralong=Ralong)
ans <- new2("DelayedUnaryIsoOp", seed=seed,
OP=OP, Largs=Largs, Rargs=Rargs,
Lalong=Lalong, Ralong=Ralong)
if (check.op) {
## We quickly test the validity of the operation by calling type()
## on the returned object. This will fail if the operation cannot
## be applied e.g. if the user does something like:
## M <- DelayedArray(matrix(character(12), ncol=3))
## M2 <- log(M)
## The test is cheap and type() will be called anyway by show()
## later when the user tries to display M2. Better fail early than
## late!
type(ans) # we ignore the returned value
}
ans
}

### S3/S4 combo for summary.DelayedUnaryIsoOp
Expand Down

0 comments on commit 70fd89a

Please sign in to comment.