# Saving and reloading DelayedArray objects

Aaron Lun*

*infinite.monkeys.with.keyboards@gmail.com

#### Revised: 2021-05-02

#### Package

chihaya 1.13.0

# Contents

# 1 Motivation

The chihaya package saves `DelayedArray` objects for efficient, portable and stable reproduction of delayed operations in a new R session or other programming frameworks.

- Portability. We provide a file specification in a standard format (HDF5) that enables other languages to easily interpret and reproduce the delayed operations.
- Stability. By converting DelayedArray operations into our specification, we provide a layer of protection against changes in the S4 class structure that would invalidate serialized RDS objects.
- Efficiency. We avoid any realization of the delayed operations, enabling quick saving and avoiding loss of data structure in the seed (e.g., sparsity).

Check out the specification for more details.

# 2 Quick start

Make a `DelayedArray` object with some operations:

    library(DelayedArray)
    x <- DelayedArray(matrix(runif(1000), ncol=10))
    x <- x[11:15,] / runif(5)
    x <- log2(x + 1)
    x

    ## <5 x 10> DelayedMatrix object of type "double":
    ##            [,1]       [,2]       [,3] ...        [,9]       [,10]
    ## [1,] 0.60018211 1.00640715 0.96924964   . 1.003599748 1.073355962
    ## [2,] 0.10298337 1.38283605 1.02227239   . 0.230452499 1.442346031
    ## [3,] 0.67989460 0.05573421 0.99845825   . 0.695209080 0.569981064
    ## [4,] 0.68253020 0.99856263 1.18302686   . 0.032468579 0.009043181
    ## [5,] 3.27576973 2.44491239 0.69858611   . 1.593386031 2.768161129

    showtree(x)

    ## 5x10 double: DelayedMatrix object
    ## └─ 5x10 double: Stack of 2 unary iso op(s)
    ##    └─ 5x10 double: Unary iso op with args
    ##       └─ 5x10 double: Subset
    ##          └─ 100x10 double: [seed] matrix object

Save it into a HDF5 file with `saveDelayed()`:

    library(chihaya)
    tmp <- tempfile(fileext=".h5")
    saveDelayed(x, tmp)
    rhdf5::h5ls(tmp)

    ##                            group    name       otype  dclass      dim
    ## 0                              / delayed   H5I_GROUP
    ## 1                       /delayed    base H5I_DATASET   FLOAT    ( 0 )
    ## 2                       /delayed  method H5I_DATASET  STRING    ( 0 )
    ## 3                       /delayed    seed   H5I_GROUP
    ## 4                  /delayed/seed  method H5I_DATASET  STRING    ( 0 )
    ## 5                  /delayed/seed    seed   H5I_GROUP
    ## 6             /delayed/seed/seed   along H5I_DATASET INTEGER    ( 0 )
    ## 7             /delayed/seed/seed  method H5I_DATASET  STRING    ( 0 )
    ## 8             /delayed/seed/seed    seed   H5I_GROUP
    ## 9        /delayed/seed/seed/seed   index   H5I_GROUP
    ## 10 /delayed/seed/seed/seed/index       0 H5I_DATASET INTEGER        5
    ## 11       /delayed/seed/seed/seed    seed   H5I_GROUP
    ## 12  /delayed/seed/seed/seed/seed    data H5I_DATASET   FLOAT 100 x 10
    ## 13  /delayed/seed/seed/seed/seed  native H5I_DATASET INTEGER    ( 0 )
    ## 14            /delayed/seed/seed    side H5I_DATASET  STRING    ( 0 )
    ## 15            /delayed/seed/seed   value H5I_DATASET   FLOAT        5
    ## 16                 /delayed/seed    side H5I_DATASET  STRING    ( 0 )
    ## 17                 /delayed/seed   value H5I_DATASET   FLOAT    ( 0 )

And then load it back in later:

    y <- loadDelayed(tmp)
    y

    ## <5 x 10> DelayedMatrix object of type "double":
    ##            [,1]       [,2]       [,3] ...        [,9]       [,10]
    ## [1,] 0.60018211 1.00640715 0.96924964   . 1.003599748 1.073355962
    ## [2,] 0.10298337 1.38283605 1.02227239   . 0.230452499 1.442346031
    ## [3,] 0.67989460 0.05573421 0.99845825   . 0.695209080 0.569981064
    ## [4,] 0.68253020 0.99856263 1.18302686   . 0.032468579 0.009043181
    ## [5,] 3.27576973 2.44491239 0.69858611   . 1.593386031 2.768161129

Of course, this is not a particularly interesting case as we end up saving the original array inside our HDF5 file anyway. The real fun begins when you have some more interesting seeds.

# 3 More interesting seeds

We can use the delayed nature of the operations to avoid breaking sparsity. For example:

    library(Matrix)
    x <- rsparsematrix(1000, 1000, density=0.01)
    x <- DelayedArray(x) + runif(1000)

    tmp <- tempfile(fileext=".h5")
    saveDelayed(x, tmp)
    rhdf5::h5ls(tmp)

    ##            group     name       otype  dclass   dim
    ## 0              /  delayed   H5I_GROUP
    ## 1       /delayed    along H5I_DATASET INTEGER ( 0 )
    ## 2       /delayed   method H5I_DATASET  STRING ( 0 )
    ## 3       /delayed     seed   H5I_GROUP
    ## 4  /delayed/seed     data H5I_DATASET   FLOAT 10000
    ## 5  /delayed/seed dimnames   H5I_GROUP
    ## 6  /delayed/seed  indices H5I_DATASET INTEGER 10000
    ## 7  /delayed/seed   indptr H5I_DATASET INTEGER  1001
    ## 8  /delayed/seed    shape H5I_DATASET INTEGER     2
    ## 9       /delayed     side H5I_DATASET  STRING ( 0 )
    ## 10      /delayed    value H5I_DATASET   FLOAT  1000

    file.info(tmp)[["size"]]

    ## [1] 101826

    # Compared to a dense array.
    tmp2 <- tempfile(fileext=".h5")
    out <- HDF5Array::writeHDF5Array(x, tmp2, "data")
    file.info(tmp2)[["size"]]

    ## [1] 280549

    # Loading it back in.
    y <- loadDelayed(tmp)
    showtree(y)

    ## 1000x1000 double: DelayedMatrix object
    ## └─ 1000x1000 double: Unary iso op with args
    ##    └─ 1000x1000 double, sparse: [seed] dgCMatrix object

We can also store references to external files, thus avoiding data duplication:

    library(HDF5Array)
    test <- HDF5Array(tmp2, "data")
    stuff <- log2(test + 1)
    stuff

    ## <1000 x 1000> DelayedMatrix object of type "double":
    ##               [,1]       [,2]       [,3] ...     [,999]    [,1000]
    ##    [1,]  0.3003196  0.3003196  0.3003196   .  0.3003196  0.3003196
    ##    [2,]  0.8974169  0.8974169  0.8974169   .  0.8974169  0.8974169
    ##    [3,]  0.4315282  0.4315282 -4.3610784   .  0.4315282  0.4315282
    ##    [4,]  0.2221985  0.2221985  0.2221985   .  0.2221985  0.2221985
    ##    [5,]  0.7942106  0.7942106  0.7942106   .  0.7942106  0.7942106
    ##     ...          .          .          .   .          .          .
    ##  [996,] 0.14024181 0.14024181 0.14024181   . 0.14024181 0.14024181
    ##  [997,] 0.63317790 0.63317790 0.63317790   . 0.63317790 0.63317790
    ##  [998,] 0.01926272 0.01926272 0.01926272   . 0.01926272 0.01926272
    ##  [999,] 0.93050826 0.93050826 0.93050826   . 0.93050826 0.93050826
    ## [1000,] 0.11214274 0.11214274 0.11214274   . 0.11214274 0.11214274

    tmp <- tempfile(fileext=".h5")
    saveDelayed(stuff, tmp)
    rhdf5::h5ls(tmp)

    ##                 group       name       otype  dclass   dim
    ## 0                   /    delayed   H5I_GROUP
    ## 1            /delayed       base H5I_DATASET   FLOAT ( 0 )
    ## 2            /delayed     method H5I_DATASET  STRING ( 0 )
    ## 3            /delayed       seed   H5I_GROUP
    ## 4       /delayed/seed     method H5I_DATASET  STRING ( 0 )
    ## 5       /delayed/seed       seed   H5I_GROUP
    ## 6  /delayed/seed/seed dimensions H5I_DATASET INTEGER     2
    ## 7  /delayed/seed/seed       file H5I_DATASET  STRING ( 0 )
    ## 8  /delayed/seed/seed       name H5I_DATASET  STRING ( 0 )
    ## 9  /delayed/seed/seed     sparse H5I_DATASET INTEGER ( 0 )
    ## 10 /delayed/seed/seed       type H5I_DATASET  STRING ( 0 )
    ## 11      /delayed/seed       side H5I_DATASET  STRING ( 0 )
    ## 12      /delayed/seed      value H5I_DATASET   FLOAT ( 0 )

    file.info(tmp)[["size"]] # size of the delayed operations + pointer to the actual file

    ## [1] 49642

    y <- loadDelayed(tmp)
    y

    ## <1000 x 1000> DelayedMatrix object of type "double":
    ##               [,1]       [,2]       [,3] ...     [,999]    [,1000]
    ##    [1,]  0.3003196  0.3003196  0.3003196   .  0.3003196  0.3003196
    ##    [2,]  0.8974169  0.8974169  0.8974169   .  0.8974169  0.8974169
    ##    [3,]  0.4315282  0.4315282 -4.3610784   .  0.4315282  0.4315282
    ##    [4,]  0.2221985  0.2221985  0.2221985   .  0.2221985  0.2221985
    ##    [5,]  0.7942106  0.7942106  0.7942106   .  0.7942106  0.7942106
    ##     ...          .          .          .   .          .          .
    ##  [996,] 0.14024181 0.14024181 0.14024181   . 0.14024181 0.14024181
    ##  [997,] 0.63317790 0.63317790 0.63317790   . 0.63317790 0.63317790
    ##  [998,] 0.01926272 0.01926272 0.01926272   . 0.01926272 0.01926272
    ##  [999,] 0.93050826 0.93050826 0.93050826   . 0.93050826 0.93050826
    ## [1000,] 0.11214274 0.11214274 0.11214274   . 0.11214274 0.11214274

# Session information

    sessionInfo()

    ## R version 4.6.0 Patched (2026-05-01 r89994)
    ## Platform: aarch64-apple-darwin23
    ## Running under: macOS Tahoe 26.3.1
    ##
    ## Matrix products: default
    ## BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib
    ## LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
    ##
    ## locale:
    ## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] HDF5Array_1.41.0      h5mread_1.5.0         rhdf5_2.57.0
    ##  [4] chihaya_1.13.0        DelayedArray_0.39.2   SparseArray_1.13.2
    ##  [7] S4Arrays_1.13.0       abind_1.4-8           IRanges_2.47.1
    ## [10] S4Vectors_0.51.2      MatrixGenerics_1.25.0 matrixStats_1.5.0
    ## [13] BiocGenerics_0.59.2   generics_0.1.4        Matrix_1.7-5
    ## [16] BiocStyle_2.41.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] jsonlite_2.0.0      compiler_4.6.0      BiocManager_1.30.27
    ##  [4] Rcpp_1.1.1-1.1      rhdf5filters_1.25.0 jquerylib_0.1.4
    ##  [7] yaml_2.3.12         fastmap_1.2.0       lattice_0.22-9
    ## [10] R6_2.6.1            XVector_0.53.0      knitr_1.51
    ## [13] bookdown_0.46       bslib_0.11.0        rlang_1.2.0
    ## [16] cachem_1.1.0        xfun_0.57           sass_0.4.10
    ## [19] otel_0.2.0          cli_3.6.6           Rhdf5lib_2.1.0
    ## [22] digest_0.6.39       grid_4.6.0          lifecycle_1.0.5
    ## [25] evaluate_1.0.5      rmarkdown_2.31      tools_4.6.0
    ## [28] htmltools_0.5.9