as.matrix.HDF5Matrix: Convert HDF5Matrix to in-memory matrix

View source: R/S3_conversion.R

as.matrix.HDF5MatrixR Documentation

Convert HDF5Matrix to in-memory matrix

Description

Reads entire HDF5 dataset into memory as a standard R matrix. WARNING: This loads all data into RAM. For large datasets (>1GB), this may cause memory exhaustion.

Usage

## S3 method for class 'HDF5Matrix'
as.matrix(x, force = FALSE, max_size_mb = NULL, ...)

Arguments

x

An HDF5Matrix object

force

Logical. If TRUE, skip size warnings. Default FALSE.

max_size_mb

Numeric. Maximum size in MB to convert without warning. Default is NULL (auto-detect based on system RAM). Use Inf to disable.

...

Additional arguments (currently unused)

Details

Size thresholds and behavior:

Small datasets (< max_size_mb):

Convert silently

Medium datasets (max_size_mb to 2GB):

Show warning, require confirmation

Large datasets (> 2GB):

Show error, require force=TRUE

Huge datasets (> 8GB):

Block conversion even with force=TRUE

Memory estimation: The function estimates memory usage as: nrow * ncol * 8 bytes (for numeric)

Actual memory usage may be higher due to:

  • R's internal overhead

  • Temporary copies during conversion

  • Other objects in memory

Recommendations:

  • For large datasets, use subsetting instead: X[1:1000, ]

  • For analysis, use HDF5Matrix methods directly (they work on-disk)

  • Only convert to memory when absolutely necessary

Value

Standard R matrix with data from HDF5 file

See Also

[.HDF5Matrix for subsetting, as.data.frame.HDF5Matrix for data frame conversion

Examples


    fn <- tempfile(fileext = ".h5")
    X <- hdf5_create_matrix(fn, "data/X", data = matrix(rnorm(500), 100, 5))
    mat <- as.matrix(X)
    head(mat)

    # Subsetting is more efficient for large datasets
    subset <- X[1:10, 1:3]

    hdf5_close_all()
    unlink(fn)



BigDataStatMeth documentation built on May 15, 2026, 1:07 a.m.