Skip to contents

A DelayedArray subclass to efficiently mimic an array containing a constant value, without actually creating said array in memory.

Usage

## Constructor function:
ConstantArray(dim, value=NA)

Arguments

dim

The dimensions (specified as an integer vector) of the ConstantArray object to create.

value

Vector (atomic or list) of length 1, containing the value to fill the matrix.

Value

A ConstantArray (or ConstantMatrix) object. (Note that ConstantMatrix extends ConstantArray.)

Details

This class allows us to efficiently create arrays containing a single value. For example, we can create matrices full of NA values, to serve as placeholders for missing assays when combining SummarizedExperiment objects.

Author

Aaron Lun

See also

Examples

## This would ordinarily take up 8 TB of memory:
CM <- ConstantArray(c(1e6, 1e6), value=NA_real_)
CM
#> <1000000 x 1000000> ConstantMatrix object of type "double":
#>                  [,1]       [,2]       [,3] ...  [,999999] [,1000000]
#>       [1,]         NA         NA         NA   .         NA         NA
#>       [2,]         NA         NA         NA   .         NA         NA
#>       [3,]         NA         NA         NA   .         NA         NA
#>       [4,]         NA         NA         NA   .         NA         NA
#>       [5,]         NA         NA         NA   .         NA         NA
#>        ...          .          .          .   .          .          .
#>  [999996,]         NA         NA         NA   .         NA         NA
#>  [999997,]         NA         NA         NA   .         NA         NA
#>  [999998,]         NA         NA         NA   .         NA         NA
#>  [999999,]         NA         NA         NA   .         NA         NA
#> [1000000,]         NA         NA         NA   .         NA         NA

CM2 <-ConstantArray(c(4, 1e6), value=55)
rbind(CM, CM2)
#> <1000004 x 1000000> DelayedMatrix object of type "double":
#>                  [,1]       [,2]       [,3] ...  [,999999] [,1000000]
#>       [1,]         NA         NA         NA   .         NA         NA
#>       [2,]         NA         NA         NA   .         NA         NA
#>       [3,]         NA         NA         NA   .         NA         NA
#>       [4,]         NA         NA         NA   .         NA         NA
#>       [5,]         NA         NA         NA   .         NA         NA
#>        ...          .          .          .   .          .          .
#> [1000000,]         NA         NA         NA   .         NA         NA
#> [1000001,]         55         55         55   .         55         55
#> [1000002,]         55         55         55   .         55         55
#> [1000003,]         55         55         55   .         55         55
#> [1000004,]         55         55         55   .         55         55