block_apply: Apply a function to streamed matrix blocks

View source: R/delarr-eval.R

block_applyR Documentation

Apply a function to streamed matrix blocks

Description

Evaluates a delarr slice-by-slice, materialising manageable chunks for further processing without realising the full matrix.

Usage

block_apply(
  x,
  margin = c("cols", "rows"),
  size = 16384L,
  fn,
  parallel = FALSE,
  workers = NULL
)

Arguments

x

A delarr object.

margin

Dimension along which to chunk ("cols" or "rows").

size

Approximate chunk size.

fn

Function applied to each materialised chunk.

parallel

Logical; process chunks in parallel when possible.

workers

Number of worker processes for parallel execution.

Value

A list of results returned by fn.

Examples

mat <- matrix(1:20, nrow = 4, ncol = 5)
darr <- delarr(mat)

# Apply function to column chunks
col_maxes <- block_apply(darr, margin = "cols", size = 2L, fn = function(block) {
  apply(block, 2, max)
})
unlist(col_maxes)

# Apply function to row chunks
row_means <- block_apply(darr, margin = "rows", size = 2L, fn = function(block) {
  rowMeans(block)
})
unlist(row_means)


delarr documentation built on July 1, 2026, 1:06 a.m.