### Set Realization Backend and Use Sink Source: https://context7.com/bioconductor/delayedarray/llms.txt Globally sets the realization backend and demonstrates creating a sink, filling it block by block, and coercing it to a DelayedArray. Resets to the in-memory backend afterwards. ```r library(DelayedArray) library(HDF5Array) # List registered backends registeredRealizationBackends() # Set HDF5Array as the active backend setAutoRealizationBackend("HDF5Array") # Manually create a sink and fill it block by block sink <- AutoRealizationSink(dim=c(500L, 500L), type="double") sinkApply(sink, FUN = function(sink, viewport) { block <- matrix(rnorm(prod(dim(viewport))), nrow=nrow(viewport)) write_block(sink, viewport, block) } ) # Coerce the filled sink to a DelayedArray result <- as(sink, "DelayedArray") close(sink) class(result) # "HDF5Matrix" # Reset to in-memory backend setAutoRealizationBackend(NULL) ``` -------------------------------- ### Realization Sinks and Backends Source: https://context7.com/bioconductor/delayedarray/llms.txt Manage realization backends globally or create custom sinks for block-by-block data writing. ```APIDOC ## `setAutoRealizationBackend()` / `AutoRealizationSink()` / `sinkApply()` — Realization sinks and backends `setAutoRealizationBackend()` globally selects the realization backend (`"RleArray"`, `"HDF5Array"`, `"TENxMatrix"`, `"ZarrArray"`, or `NULL` for in-memory). `AutoRealizationSink()` creates a sink object using the currently active backend. `sinkApply()` fills a sink block by block with a user-supplied callback, enabling custom write-to-disk workflows. ```r library(DelayedArray) library(HDF5Array) # List registered backends registeredRealizationBackends() # Set HDF5Array as the active backend setAutoRealizationBackend("HDF5Array") # Manually create a sink and fill it block by block sink <- AutoRealizationSink(dim=c(500L, 500L), type="double") sinkApply(sink, FUN = function(sink, viewport) { block <- matrix(rnorm(prod(dim(viewport))), nrow=nrow(viewport)) write_block(sink, viewport, block) } ) # Coerce the filled sink to a DelayedArray result <- as(sink, "DelayedArray") close(sink) class(result) # "HDF5Matrix" # Reset to in-memory backend setAutoRealizationBackend(NULL) ``` ``` -------------------------------- ### showtree(), seed(), nseed(), seedApply() Source: https://context7.com/bioconductor/delayedarray/llms.txt Functions to inspect the delayed operation tree of a DelayedArray object. `showtree()` visualizes the operation tree, `seed()` extracts the leaf seed, `nseed()` counts the number of seeds, and `seedApply()` applies a function to each seed. ```APIDOC ## showtree() / seed() / nseed() / seedApply() ### Description `showtree()` prints a human-readable tree of `DelayedOp` nodes. `seed()` extracts the leaf seed from a linear tree. `nseed()` counts the number of leaf seeds. `seedApply()` applies a function to every leaf seed. ### Usage ```r library(DelayedArray) a <- array(runif(1000), dim=c(10, 10, 10)) da <- DelayedArray(a) # Apply a subsetting + transposition chain da2 <- t(da[1:5, , 3]) # 2 delayed ops stacked showtree(da2) # 10x5 double: DelayedArray # └─ 10x5 double: [DelayedAperm] perm=c(2,1) # └─ 5x10x1 double: [DelayedSubset] ... # └─ 10x10x10 double: [seed] array object nseed(da2) # 1 — linear tree, single seed seed(da2) # returns the original array 'a' # seedApply: get path of every HDF5-backed seed library(HDF5Array) h <- as(da, "HDF5Array") h2 <- cbind(h, h) # tree with 2 seeds nseed(h2) # 2 seedApply(h2, path) # list of 2 file paths ``` ``` -------------------------------- ### Iterate over viewports with gridApply Source: https://context7.com/bioconductor/delayedarray/llms.txt gridApply provides lower-level iteration over ArrayViewport objects within an ArrayGrid. The callback function receives the viewport, not the data block itself, offering fine-grained control over grid traversal. ```r library(DelayedArray) m <- matrix(seq_len(100), 10, 10) dm <- DelayedArray(m) # Build a custom 5-row-by-5-column grid grid <- RegularArrayGrid(dim(dm), spacings=c(5L, 5L)) length(grid) # 4 blocks # gridApply: print the range of each viewport's coordinates gridApply(grid, function(vp) { cat("Block", currentBlockId(), "covers rows", start(ranges(vp))[1], "to", end(ranges(vp))[1], "\n") dim(vp) }) ``` ```r # gridReduce: count elements across all viewports total <- gridReduce( FUN = function(vp, acc) acc + prod(dim(vp)), grid = grid, init = 0L ) total # 100 ``` -------------------------------- ### Apply function block by block with blockApply Source: https://context7.com/bioconductor/delayedarray/llms.txt Use blockApply for lapply-like iteration over an array-like object in memory-bounded blocks. Results are collected as a list. Supports parallel execution via BPPARAM. ```r library(DelayedArray) # Large simulated matrix (imagine it lives on disk) m <- matrix(rnorm(1e6), nrow=1000, ncol=1000) dm <- DelayedArray(m) # blockApply: compute per-block column sums setAutoBlockSize(1e7) # 10 MB blocks block_csums <- blockApply(dm, colSums) total_csums <- Reduce("+", block_csums) # combine results ``` ```r # blockReduce: find first block that contains any NA dm_na <- dm; dm_na[5, 5] <- NA found_na <- blockReduce( FUN = function(block, acc) acc || anyNA(block), x = dm_na, init = FALSE, BREAKIF = identity # stop as soon as TRUE is returned ) found_na # TRUE ``` ```r # Parallel block processing with BiocParallel library(BiocParallel) setAutoBPPARAM(MulticoreParam(workers=4)) parallel_results <- blockApply(dm, function(block) sum(block^2)) setAutoBPPARAM(NULL) # reset to sequential ``` -------------------------------- ### Inspect Delayed Operation Tree Source: https://context7.com/bioconductor/delayedarray/llms.txt Use showtree() to visualize the pending operations and seed objects. nseed() counts leaf seeds, seed() extracts the primary seed, and seedApply() applies a function to all leaf seeds. ```r library(DelayedArray) a <- array(runif(1000), dim=c(10, 10, 10)) da <- DelayedArray(a) # Apply a subsetting + transposition chain da2 <- t(da[1:5, , 3]) # 2 delayed ops stacked showtree(da2) # 10x5 double: DelayedArray # └─ 10x5 double: [DelayedAperm] perm=c(2,1) # └─ 5x10x1 double: [DelayedSubset] ... # └─ 10x10x10 double: [seed] array object nseed(da2) # 1 — linear tree, single seed seed(da2) # returns the original array 'a' # seedApply: get path of every HDF5-backed seed library(HDF5Array) h <- as(da, "HDF5Array") h2 <- cbind(h, h) # tree with 2 seeds nseed(h2) # 2 seedApply(h2, path) # list of 2 file paths ``` -------------------------------- ### Realize DelayedArray to Memory or Disk Source: https://context7.com/bioconductor/delayedarray/llms.txt Executes pending operations to create a pristine in-memory array or an on-disk object. Use setAutoRealizationBackend() to specify HDF5Array or RleArray for disk-based realization. ```r library(DelayedArray) library(HDF5Array) # needed for the HDF5Array backend # Build a DelayedMatrix with several delayed operations m <- matrix(1:1e6, nrow=1000) dm <- DelayedArray(m) dm_processed <- log1p(dm[1:500, 1:800]) * 2 # Realize to memory (default) result_mem <- realize(dm_processed) class(result_mem) # "array" dim(result_mem) # 500 800 # Realize to HDF5 on disk (block-processed, memory-efficient) setAutoRealizationBackend("HDF5Array") result_hdf5 <- realize(dm_processed) class(result_hdf5) # "HDF5Matrix" path(result_hdf5) # path to the automatically created HDF5 file # Reset backend to default (in-memory) setAutoRealizationBackend(NULL) ``` -------------------------------- ### realize() Source: https://context7.com/bioconductor/delayedarray/llms.txt Executes all pending delayed operations and materializes the DelayedArray into an in-memory array or an on-disk object (e.g., HDF5). It allows for memory-efficient processing of large datasets. ```APIDOC ## realize() ### Description Executes all pending delayed operations and returns a *pristine* (no delayed ops) in-memory array (default) or on-disk object. When `BACKEND` is `NULL` (default), dense arrays are coerced to `array` and sparse arrays to `SVT_SparseArray`. When `BACKEND` is `"HDF5Array"` or `"RleArray"`, the result is written to the respective storage format using block processing. ### Usage ```r library(DelayedArray) library(HDF5Array) # needed for the HDF5Array backend # Build a DelayedMatrix with several delayed operations m <- matrix(1:1e6, nrow=1000) dm <- DelayedArray(m) dm_processed <- log1p(dm[1:500, 1:800]) * 2 # Realize to memory (default) result_mem <- realize(dm_processed) class(result_mem) # "array" dim(result_mem) # 500 800 # Realize to HDF5 on disk (block-processed, memory-efficient) setAutoRealizationBackend("HDF5Array") result_hdf5 <- realize(dm_processed) class(result_hdf5) # "HDF5Matrix" path(result_hdf5) # path to the automatically created HDF5 file # Reset backend to default (in-memory) setAutoRealizationBackend(NULL) ``` ``` -------------------------------- ### Row/column summary methods Source: https://context7.com/bioconductor/delayedarray/llms.txt Utilize block-processed and sparse-aware row/column summarization methods for DelayedMatrix objects. ```APIDOC ## Row/column summary methods — `rowSums`, `colMeans`, `rowVars`, `rowMins`, `rowMaxs`, `rowRanges`, etc. All standard row/column summarization methods for `DelayedMatrix` objects are block-processed and sparse-aware. They support `na.rm`, `useNames`, and accept an optional `center` argument (`rowVars`/`colVars`). The underlying `BLOCK_*` functions (e.g., `BLOCK_rowSums()`) can also be called directly with explicit `grid` and `BPPARAM` arguments for full control. ```r library(DelayedArray) set.seed(1) m <- matrix(rnorm(5000), nrow=100, ncol=50, dimnames=list(paste0("gene", 1:100), paste0("cell", 1:50))) dm <- DelayedArray(m) dm[1:3, 1:3] <- NA # introduce some NAs # Standard summary methods (block-processed transparently) rs <- rowSums(dm, na.rm=TRUE) # named numeric vector, length 100 cm <- colMeans(dm, na.rm=TRUE) # named numeric vector, length 50 rv <- rowVars(dm, na.rm=TRUE) # unbiased sample variances per row rmn <- rowMins(dm, na.rm=TRUE) # min per row rmx <- colMaxs(dm, na.rm=TRUE) # max per column rr <- rowRanges(dm, na.rm=TRUE) # 2-column matrix: [min, max] per row head(rs, 3) # gene1 gene2 gene3 # ... # Low-level: explicit parallelism and custom grid library(BiocParallel) setAutoBPPARAM(MulticoreParam(workers=2)) grid <- rowAutoGrid(dm, nrow=25) # 4 horizontal strips rv2 <- BLOCK_rowVars(dm, na.rm=TRUE, grid=grid) setAutoBPPARAM(NULL) ``` ``` -------------------------------- ### Block-Processed Row/Column Summary Methods Source: https://context7.com/bioconductor/delayedarray/llms.txt Applies standard summary methods like rowSums, colMeans, and rowVars to DelayedMatrix objects, which are block-processed and sparse-aware. Shows low-level control with explicit parallelism and custom grids. ```r library(DelayedArray) set.seed(1) m <- matrix(rnorm(5000), nrow=100, ncol=50, dimnames=list(paste0("gene", 1:100), paste0("cell", 1:50))) dm <- DelayedArray(m) dm[1:3, 1:3] <- NA # introduce some NAs # Standard summary methods (block-processed transparently) rs <- rowSums(dm, na.rm=TRUE) # named numeric vector, length 100 cm <- colMeans(dm, na.rm=TRUE) # named numeric vector, length 50 rv <- rowVars(dm, na.rm=TRUE) # unbiased sample variances per row rmn <- rowMins(dm, na.rm=TRUE) # min per row rmx <- colMaxs(dm, na.rm=TRUE) # max per column rr <- rowRanges(dm, na.rm=TRUE) # 2-column matrix: [min, max] per row head(rs, 3) # gene1 gene2 gene3 # ... # Low-level: explicit parallelism and custom grid library(BiocParallel) setAutoBPPARAM(MulticoreParam(workers=2)) grid <- rowAutoGrid(dm, nrow=25) # 4 horizontal strips rv2 <- BLOCK_rowVars(dm, na.rm=TRUE, grid=grid) setAutoBPPARAM(NULL) ``` -------------------------------- ### Create Run-Length Encoded Array Source: https://context7.com/bioconductor/delayedarray/llms.txt Constructs a memory-efficient RleArray from a single Rle vector or an RleList. Demonstrates checking class, dimensions, object size, and column sums. ```r library(DelayedArray) # From a single Rle vector + explicit dimensions library(S4Vectors) rle_vec <- Rle(c(1L, 2L, 3L), c(1000L, 500L, 500L)) ra <- RleArray(rle_vec, dim=c(100L, 20L)) class(ra) # "RleMatrix" dim(ra) # 100 20 object.size(ra) < object.size(as.array(ra)) # TRUE # From an RleList (one Rle per column — chunked layout) rl <- RleList( col1 = Rle(c(0L, 1L), c(900L, 100L)), col2 = Rle(c(0L, 5L), c(800L, 200L)) ) rm <- as(rl, "RleArray") dim(rm) # 1000 2 colSums(rm) # 100 1000 # Coerce back as(rm, "RleList")[[1]] # col1 as an Rle object ``` -------------------------------- ### Respecting Chunk Boundaries with defaultAutoGrid Source: https://context7.com/bioconductor/delayedarray/llms.txt Demonstrates how defaultAutoGrid respects chunk boundaries of a DelayedArray. Ensures that blocks are multiples of the chunk grid, which is useful when blocks are sufficiently large. ```R g <- defaultAutoGrid(hm) # blocks are multiples of the chunk grid all(dim(g[[1L]]) %% chunkdim(hm) == 0L) # TRUE (when blocks are large enough) ``` -------------------------------- ### Higher-Order Array Operations with apply, sweep, and scale Source: https://context7.com/bioconductor/delayedarray/llms.txt Utilize apply for slice-wise operations, sweep for broadcasting statistics, and scale for centering/scaling columns. Operations are delayed. ```R library(DelayedArray) m <- matrix(1:200, nrow=20, ncol=10, dimnames=list(paste0("r",1:20), paste0("c",1:10))) dm <- DelayedArray(m) # apply: row-wise range row_ranges <- apply(dm, MARGIN=1L, FUN=range) # 2 x 20 matrix # sweep: subtract per-row medians (delayed until realized) row_meds <- apply(dm, 1L, median) centered <- sweep(dm, MARGIN=1L, STATS=row_meds, FUN="-") class(centered) # "DelayedMatrix" — still delayed # scale: center=TRUE, scale=TRUE (uses block-processed colMeans + rowSds) dm_num <- DelayedArray(matrix(rnorm(200), 20, 10)) scaled <- scale(dm_num) attr(scaled, "scaled:center")[1:3] # per-column centers attr(scaled, "scaled:scale")[1:3] # per-column SDs # Realize the centered matrix as.matrix(centered)[1:3, 1:4] ``` -------------------------------- ### Create processing grids with defaultAutoGrid Source: https://context7.com/bioconductor/delayedarray/llms.txt Use defaultAutoGrid to create an ArrayGrid optimized for block processing, with blocks as large as possible and aligned to physical chunk boundaries. rowAutoGrid and colAutoGrid create grids of complete row or column strips. ```r library(DelayedArray) m <- matrix(1:1e6, nrow=1000) dm <- DelayedArray(m) # Default grid: hypercube-shaped blocks bounded by 100 MB setAutoBlockSize(1e8) # 100 MB g <- defaultAutoGrid(dm) length(g) # number of blocks dim(g[[1L]]) # dimensions of the first block ``` ```r # Row grid: one block per group of rows rg <- rowAutoGrid(dm, nrow=200) nrow(rg) # 5 (1000 rows / 200 per block) col(rg) # 1 (full-width blocks) ``` ```r # Column grid: one block per group of columns cg <- colAutoGrid(dm, ncol=250) nrow(cg) # 1 col(cg) # 4 ``` ```r # Custom block shape: column-oriented (last dim grows first) g2 <- defaultAutoGrid(dm, block.shape="last-dim-grows-first") dim(g2[[1L]]) # narrow and tall blocks ``` -------------------------------- ### Control global block processing settings Source: https://context7.com/bioconductor/delayedarray/llms.txt Use setAutoBlockSize and setAutoBlockShape to control the maximum bytes per block and the geometric layout of blocks, respectively. These global settings affect block-processing functions that do not explicitly receive a grid argument. ```r library(DelayedArray) # Check current settings getAutoBlockSize() # 1e8 (100 MB) getAutoBlockShape() # "hypercube" # Reduce block size to 10 MB for memory-constrained environments setAutoBlockSize(1e7) # Switch to column-oriented blocks (good for column-access patterns) setAutoBlockShape("last-dim-grows-first") # Restore defaults setAutoBlockSize(1e8) setAutoBlockShape("hypercube") ``` ```r # getAutoBlockLength: max number of elements for a given type getAutoBlockLength("double") # 1e8 / 8 = 12500000 getAutoBlockLength("integer") # 1e8 / 4 = 25000000 getAutoBlockLength("logical") # 1e8 / 4 = 25000000 ``` -------------------------------- ### Querying Chunk Geometry with chunkdim and chunkGrid Source: https://context7.com/bioconductor/delayedarray/llms.txt Inspect the physical chunk dimensions (chunkdim) and grid (chunkGrid) of an array-like object, particularly useful for on-disk backends like HDF5Array. ```R library(DelayedArray) library(HDF5Array) # Write a matrix to HDF5 with explicit chunk dimensions m <- matrix(rnorm(1e6), nrow=1000) h5f <- tempfile(fileext=".h5") writeHDF5Array(m, filepath=h5f, name="data", chunkdim=c(100L, 100L)) hm <- HDF5Array(h5f, name="data") # Inspect chunk layout chunkdim(hm) # 100 100 cg <- chunkGrid(hm) length(cg) # 100 (10 row-chunks × 10 col-chunks) dim(cg[[1L]]) # 100 100 ``` -------------------------------- ### RleArray() Source: https://context7.com/bioconductor/delayedarray/llms.txt Construct memory-efficient RleArray objects for in-memory arrays with long runs of identical values. ```APIDOC ## `RleArray()` — Run-length encoded in-memory arrays Creates a `DelayedArray` subclass whose seed stores data as Rle (run-length encoding) objects, making it highly memory-efficient for arrays with long runs of identical values (e.g., genomic coverage tracks). Supports construction from a single `Rle` object or from a list/`RleList` of `Rle` objects (one per column). ```r library(DelayedArray) # From a single Rle vector + explicit dimensions library(S4Vectors) rle_vec <- Rle(c(1L, 2L, 3L), c(1000L, 500L, 500L)) ra <- RleArray(rle_vec, dim=c(100L, 20L)) class(ra) # "RleMatrix" dim(ra) # 100 20 object.size(ra) < object.size(as.array(ra)) # TRUE # From an RleList (one Rle per column — chunked layout) rl <- RleList( col1 = Rle(c(0L, 1L), c(900L, 100L)), col2 = Rle(c(0L, 5L), c(800L, 200L)) ) rm <- as(rl, "RleArray") dim(rm) # 1000 2 colSums(rm) # 100 1000 # Coerce back as(rm, "RleList")[[1]] # col1 as an Rle object ``` ``` -------------------------------- ### DelayedArray() Source: https://context7.com/bioconductor/delayedarray/llms.txt Constructs a DelayedArray object from any array-like object that satisfies the seed contract. It can wrap ordinary arrays, sparse matrices, and other DelayedArray objects. ```APIDOC ## DelayedArray() ### Description Constructs a `DelayedArray` (or `DelayedMatrix` for 2-D seeds) from any seed object that satisfies the *seed contract*: it must have `dim()`, `dimnames()`, and an `extract_array()` method. Passing an existing `DelayedArray` is a no-op; passing a `DelayedOp` node wraps it after optional incremental simplification. ### Usage ```r library(DelayedArray) # From an ordinary in-memory array m <- matrix(1:120, nrow=10, ncol=12) dm <- DelayedArray(m) dm # <10 x 12> DelayedMatrix object # From a sparse dgCMatrix (Matrix package) library(Matrix) sp <- sparseMatrix(i=c(1,3,5), j=c(2,4,6), x=c(10,20,30), dims=c(6,8)) dsp <- DelayedArray(sp) is_sparse(dsp) # TRUE — sparse-aware block processing will be used # Chained delayed operations — nothing is computed yet result <- log2(dsp + 1) # a new DelayedMatrix with two pending operations showtree(result) # 6x8 double: DelayedArray # └─ 6x8 double: [DelayedUnaryIsoOpStack] # └─ 6x8 double: [DelayedNaryIsoOp] # ├─ 6x8 double: [seed] dgCMatrix object # └─ 1x1 double: [seed] array object ``` ``` -------------------------------- ### Create Memory-Efficient Constant Array Source: https://context7.com/bioconductor/delayedarray/llms.txt Creates a large constant-value DelayedArray that uses minimal memory by storing only dimensions and a scalar value. Zero-valued arrays are recognized as sparse. ```r library(DelayedArray) # A large constant matrix — barely uses any memory ca <- ConstantArray(dim=c(1e6L, 1e6L), value=0L) object.size(ca) # tiny! only stores dim + scalar dim(ca) # 1000000 1000000 is_sparse(ca) # TRUE (zero is the sparse "background") # Use in arithmetic (result is a DelayedMatrix) ones <- ConstantArray(dim=c(100L, 200L), value=1L) result <- ones * 42L as.array(result)[1:3, 1:3] ``` -------------------------------- ### Wrap array-like objects in DelayedArray Source: https://context7.com/bioconductor/delayedarray/llms.txt Constructs a DelayedArray from various seed objects like in-memory arrays or sparse matrices. Sparse-aware block processing is automatically enabled for sparse seeds. ```r library(DelayedArray) # From an ordinary in-memory array m <- matrix(1:120, nrow=10, ncol=12) dm <- DelayedArray(m) dm # <10 x 12> DelayedMatrix object # From a sparse dgCMatrix (Matrix package) library(Matrix) sp <- sparseMatrix(i=c(1,3,5), j=c(2,4,6), x=c(10,20,30), dims=c(6,8)) dsp <- DelayedArray(sp) is_sparse(dsp) # TRUE — sparse-aware block processing will be used # Chained delayed operations — nothing is computed yet result <- log2(dsp + 1) # a new DelayedMatrix with two pending operations showtree(result) # 6x8 double: DelayedArray # └─ 6x8 double: [DelayedUnaryIsoOpStack] # └─ 6x8 double: [DelayedNaryIsoOp] # ├─ 6x8 double: [seed] dgCMatrix object # └─ 1x1 double: [seed] array object ``` -------------------------------- ### Arithmetic and unary isometric operations Source: https://context7.com/bioconductor/delayedarray/llms.txt Perform delayed arithmetic, comparison, and mathematical operations on DelayedArray objects. ```APIDOC ## Arithmetic and unary isometric operations — Delayed `Ops`, `Math`, `log`, `round`, `grepl`, `sub` All standard arithmetic, comparison, and mathematical operations on `DelayedArray` objects are implemented as delayed operations. They return a new `DelayedArray` that carries an extra node in the operation tree; no computation occurs until realization or block processing is triggered. ```r library(DelayedArray) m1 <- DelayedArray(matrix(1:100, 10, 10)) m2 <- DelayedArray(matrix(101:200, 10, 10)) ``` -------------------------------- ### Delayed Arithmetic and Unary Operations Source: https://context7.com/bioconductor/delayedarray/llms.txt Implements standard arithmetic, comparison, and mathematical operations on DelayedArray objects as delayed operations. Computation is deferred until realization or block processing. ```r library(DelayedArray) m1 <- DelayedArray(matrix(1:100, 10, 10)) m2 <- DelayedArray(matrix(101:200, 10, 10)) ``` -------------------------------- ### ConstantArray() Source: https://context7.com/bioconductor/delayedarray/llms.txt Create memory-efficient DelayedArray objects that store only dimensions and a single scalar value. ```APIDOC ## `ConstantArray()` — Memory-efficient constant-value arrays Creates a `DelayedArray` subclass whose seed, `ConstantArraySeed`, stores only the dimensions and a single scalar value. No memory is allocated for the elements themselves. Zero-valued `ConstantArray` objects are recognized as sparse, enabling sparse-aware block processing. ```r library(DelayedArray) # A large constant matrix — barely uses any memory ca <- ConstantArray(dim=c(1e6L, 1e6L), value=0L) object.size(ca) # tiny! only stores dim + scalar dim(ca) # 1000000 1000000 is_sparse(ca) # TRUE (zero is the sparse "background") # Use in arithmetic (result is a DelayedMatrix) ones <- ConstantArray(dim=c(100L, 200L), value=1L) result <- ones * 42L as.array(result)[1:3, 1:3] # [,1] [,2] [,3] # [1,] 42 42 42 # [2,] 42 42 42 # [3,] 42 42 42 ``` ``` -------------------------------- ### Delayed Arithmetic and String Operations Source: https://context7.com/bioconductor/delayedarray/llms.txt Perform arithmetic and string operations on DelayedArray objects. Operations are delayed until the array is realized. ```R diff_m <- m2 - m1 # DelayedMatrix with DelayedNaryIsoOp node scaled <- m1 * 2L # scalar broadcast log_m <- log2(m1 + 1) # chained unary + binary bool_m <- m1 > 50L # logical DelayedMatrix showtree(log_m) ``` ```R char_arr <- DelayedArray(array(c("foo", "bar", "baz", "qux"), dim=c(2,2))) up <- toupper(char_arr) hit <- grepl("^b", char_arr) rep1 <- sub("o+", "0", char_arr) as.array(up) ``` ```R int_m <- m1; type(int_m) <- "integer" type(int_m) # "integer" ``` -------------------------------- ### Delayed Binding with cbind and rbind Source: https://context7.com/bioconductor/delayedarray/llms.txt Perform column-wise (cbind) or row-wise (rbind) binding of DelayedArray objects. This operation is delayed and creates a DelayedAbind node. ```R library(DelayedArray) a <- DelayedArray(matrix(1:30, nrow=5, ncol=6)) b <- DelayedArray(matrix(31:60, nrow=5, ncol=6)) c <- DelayedArray(matrix(61:90, nrow=5, ncol=6)) # Column-bind (delayed) abc_col <- cbind(a, b, c) dim(abc_col) # 5 x 18 nseed(abc_col) # 3 showtree(abc_col) # Row-bind (delayed) abc_row <- rbind(a, b, c) dim(abc_row) # 15 x 6 # Realize and verify all(as.matrix(abc_col) == cbind(as.matrix(a), as.matrix(b), as.matrix(c))) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.