The alabaster.matrix package implements methods to save matrix-like objects to file artifacts and load them back into R. Check out the alabaster.base for more details on the motivation and the alabaster framework.
Given an array-like object, we can use saveObject()
to
save it inside a staging directory:
library(Matrix)
y <- rsparsematrix(1000, 100, density=0.05)
library(alabaster.matrix)
tmp <- tempfile()
saveObject(y, tmp)
list.files(tmp, recursive=TRUE)
## [1] "OBJECT" "matrix.h5"
We then load it back into our R session with
loadObject()
. This creates a HDF5-backed S4 array that can
be easily coerced into the desired format, e.g., a
dgCMatrix
.
## [1] "ReloadedMatrix"
## attr(,"package")
## [1] "alabaster.matrix"
This process is supported for all base arrays, Matrix objects and DelayedArray objects.
🚧🚧🚧 Oops. Yet to migrate the delayed operations to the new
saveObject
/readObject
world - stay tuned.
🚧🚧🚧
## R version 4.4.1 (2024-06-14)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] alabaster.matrix_1.5.10 alabaster.base_1.5.9 Matrix_1.7-0
## [4] BiocStyle_2.33.1
##
## loaded via a namespace (and not attached):
## [1] jsonlite_1.8.9 compiler_4.4.1 BiocManager_1.30.25
## [4] crayon_1.5.3 Rcpp_1.0.13 rhdf5filters_1.17.0
## [7] jquerylib_0.1.4 IRanges_2.39.2 yaml_2.3.10
## [10] fastmap_1.2.0 lattice_0.22-6 XVector_0.45.0
## [13] R6_2.5.1 S4Arrays_1.5.8 knitr_1.48
## [16] BiocGenerics_0.51.1 DelayedArray_0.31.11 MatrixGenerics_1.17.0
## [19] maketools_1.3.0 bslib_0.8.0 rlang_1.1.4
## [22] HDF5Array_1.33.6 cachem_1.1.0 xfun_0.47
## [25] sass_0.4.9 sys_3.4.2 SparseArray_1.5.39
## [28] cli_3.6.3 Rhdf5lib_1.27.0 zlibbioc_1.51.1
## [31] digest_0.6.37 grid_4.4.1 alabaster.schemas_1.5.0
## [34] rhdf5_2.49.0 lifecycle_1.0.4 S4Vectors_0.43.2
## [37] evaluate_1.0.0 buildtools_1.0.0 abind_1.4-8
## [40] stats4_4.4.1 rmarkdown_2.28 matrixStats_1.4.1
## [43] tools_4.4.1 htmltools_0.5.8.1