chunk_apply {matter} | R Documentation

## Apply Functions Over Chunks of a List, Vector, or Matrix

### Description

Perform equivalents of `apply`, `lapply`, and `mapply`, but over parallelized chunks of the data. This is most useful if accessing the data is potentially time-consuming, such as for file-based `matter` objects. Operating on chunks reduces the number of I/O operations.

### Usage

    chunk_apply(X, FUN, MARGIN, ..., simplify = FALSE,
        chunks = NA, view = c("element", "chunk"),
        attr = list(), alist = list(), pattern = NULL,
        outfile = NULL, verbose = FALSE,
        BPREDO = list(), BPPARAM = bpparam())

    chunk_mapply(FUN, ..., MoreArgs = NULL, simplify = FALSE,
        chunks = NA, view = c("element", "chunk"),
        attr = list(), alist = list(), pattern = NULL,
        outfile = NULL, verbose = FALSE,
        BPREDO = list(), BPPARAM = bpparam())

### Arguments

`X` | A list, vector, or matrix for `chunk_apply()`. These may be any class that implements suitable methods for `[`, `[[`, `dim`, and `length()`. Only lists are supported for `chunk_mapply()`.
`FUN` | The function to be applied.
`MARGIN` | If the object is matrix-like, which dimension to iterate over. Must be 1 or 2, where 1 indicates rows and 2 indicates columns. The dimension names can also be used if `X` has `dimnames` set.
`MoreArgs` | A list of other arguments to `FUN`.
`...` | Additional arguments to be passed to `FUN`.
`simplify` | Should the result be simplified into a vector, matrix, or higher dimensional array?
`chunks` | The number of chunks to use. If `NA` (the default), this is inferred from `chunksize(X)` for `matter` objects, or from `getOption("matter.default.chunksize")` for non-`matter` classes. For IO-bound operations, using fewer chunks will often be faster, but use more memory.
`view` | What should be passed as the argment to `FUN`: "element" means the vector element, row, or column are passed (same as the behavior of `lapply` and `apply`), and "chunk" means to pass the entire chunk.
`attr` | A named list of attributes that will be attached to the argument passed to `FUN` as-is.
`alist` | A named list of vector-like attributes that will be attached to the argument passed to `FUN`, subsetted to the current elements. Typically, each attribute should be as long as `X`, unless `pattern` is specified, in which case each attribute should be as long as `pattern`.
`pattern` | A list of indices giving a pattern over which to apply `FUN` to `X`. Each element of `pattern` should give a vector of indices which can be used subscript `X`. For time and space efficiency, no attempt is made to verify these indices are valid.
`outfile` | If non-NULL, a file path where the results should be written as they are processed. If specified, `FUN` must return a 'raw', 'logical', 'integer', or 'numeric' vector. The result will be returned as a `matter` object.
`verbose` | Should user messages be printed with the current chunk being processed?
`BPREDO` | See documentation for `bplapply`.
`BPPARAM` | An optional instance of `BiocParallelParam`. See documentation for `bplapply`.

### Value

Typically, a list if `simplify=FALSE`. Otherwise, the results may be coerced to a vector or array.

### Author(s)

Kylie A. Bemis

### See Also

`apply`, `lapply`, `mapply`,

### Examples

    register(SerialParam())

    set.seed(1)
    x <- matrix(rnorm(1000^2), nrow=1000, ncol=1000)

    out <- chunk_apply(x, mean, 1, chunks=20, verbose=TRUE)

[Package matter version 1.13.8 Index]