
DelayedArray(x) # constructor function
as.array
on it. However this realizes the
object in memory and could require too much memory. Big DelayedArray
objects are preferrably realized on disk e.g. by calling the
HDF5Dataset
constructor on it (other on-disk backends can be
supported). In that case, the full object is not realized at once in memory,
but split into small blocks first, and the blocks are realized and written
to disk one at a time.dim()
, length()
, and dimnames()
. Only dimnames()
is supported as a setter.drop
argument of the [
operator is ignored i.e.
subsetting a DelayedArray object always returns a DelayedArray
object with the same number of dimensions. You need to call
drop()
on the subsetted object to actually drop its
ineffective dimensions (i.e. the dimensions equal to 1). i
) is not supported.
[[
is supported but only the linear form of it. DelayedArray objects don't support subassignment ([<-
or [[<-
).
## ---------------------------------------------------------------------
## WITH AN ORDINARY array OBJECT
## ---------------------------------------------------------------------
a <- array(runif(1500000), c(10000, 30, 5))
A <- DelayedArray(a)
A
toto <- function(x) (5 * x[ , , 1] ^ 3 + 1L) * log(x[, , 2])
b <- toto(a)
head(b)
B <- toto(A) # very fast! (operations are delayed)
B # still 3 dimensions (subsetting a DelayedArray object
# never drops dimensions)
B <- drop(B)
B
cs <- colSums(b)
CS <- colSums(B)
stopifnot(identical(cs, CS))
## ---------------------------------------------------------------------
## WITH A HDF5Dataset OBJECT
## ---------------------------------------------------------------------
h5a <- HDF5Dataset(a) # create the dataset
h5a
A2 <- DelayedArray(h5a) # wrap the dataset in a DelayedArray object
A2
B2 <- toto(A2) # very fast! (operations are delayed)
B2 <- drop(B2)
CS2 <- colSums(B2)
stopifnot(identical(cs, CS2))
## ---------------------------------------------------------------------
## STORE THE RESULT IN A NEW HDF5Dataset OBJECT
## ---------------------------------------------------------------------
b2 <- HDF5Dataset(B2) # "realize" B2 on disk (as an HDF5 dataset)
## If this is just an intermediate result, you can either keep going
## with B2 or replace it with b2 wrapped in a DelayedArray object etc...
B2 <- DelayedArray(b2) # semantically equivalent to the previous B2
Run the code above in your browser using DataLab