## ---------------------------------------------------------------------
## 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