h5Write {hdf5r.Extra}R Documentation

Write an R object to HDF5 file

Description

Methods to write an R object to an HDF5 file.

Usage

h5Write(x, file, name, ...)

## Default S3 method:
h5Write(x, file, name, overwrite = FALSE, gzip_level = 6, ...)

## S3 method for class 'array'
h5Write(
  x,
  file,
  name,
  overwrite = FALSE,
  transpose = FALSE,
  block_size = 5000L,
  gzip_level = 6,
  ...
)

## S3 method for class 'factor'
h5Write(x, file, name, overwrite = FALSE, ordered = TRUE, gzip_level = 6, ...)

## S3 method for class 'data.frame'
h5Write(x, file, name, overwrite = FALSE, gzip_level = 6, ...)

## S3 method for class 'dgCMatrix'
h5Write(
  x,
  file,
  name,
  overwrite = FALSE,
  transpose = FALSE,
  add.shape = FALSE,
  dimnames = list(),
  gzip_level = 6,
  ...
)

## S3 method for class 'dgRMatrix'
h5Write(
  x,
  file,
  name,
  overwrite = FALSE,
  transpose = FALSE,
  add.shape = FALSE,
  dimnames = list(),
  gzip_level = 6,
  ...
)

## S3 method for class 'list'
h5Write(x, file, name, overwrite = FALSE, gzip_level = 6, ...)

Arguments

x

An R object to be written

file

An existing HDF5 file

name

Name of the HDF5 link to be written into

...

Arguments passed to other methods.

overwrite

Whether or not to overwrite the existing HDF5 link.

gzip_level

Enable zipping at the level given here.

transpose

Whether or not to transpose the input matrix. Only works for a 2-dimension array-like object.

block_size

Default size for number of columns when transpose is TRUE.

ordered

When writing a factor, whether or not the categories are ordered.

add.shape

When writing a CSC- or CSR-matrix, whether or not to also write the number of dimensions into an HDF5 dataset.

dimnames

When writing a CSC- or CSR-matrix, whether or not to also write the dimension names.

Details

By default, h5Write will try to transform any S4 object x into combination of base R objects using h5Prep before writting it.

Value

This is an operation function and no return. Any failure should raise an error.

References

https://anndata.readthedocs.io/en/latest/fileformat-prose.html

Examples


file <- system.file("extdata", "pbmc_small.h5ad", package = "hdf5r.Extra")
tmp.file <- tempfile(fileext = ".h5")
h5CreateFile(tmp.file)

# vector -----------------------
x <- h5Read(file, "/raw/X/data")
h5Write(x, tmp.file, "raw/X/data")
x2 <- h5Read(tmp.file, "raw/X/data")
stopifnot(identical(x, x2))



# matrix -----------------------
x <- h5Read(file, "X")
h5Write(x, tmp.file, "X")
x2 <- h5Read(tmp.file, "X")
stopifnot(identical(x, x2))

h5Write(x, tmp.file, "X2", transpose = TRUE)
x2 <- h5Read(tmp.file, "X2")
stopifnot(identical(t(x), x2))



# data.frame -----------------------
x <- h5Read(file, "obs")
h5Write(x, tmp.file, "obs")
x2 <- h5Read(tmp.file, "obs")
stopifnot(identical(x, x2))

x <- h5Read(file, "raw/var") # data.frame with empty column
h5Write(x, tmp.file, "raw/var")
x2 <- h5Read(tmp.file, "raw/var")
stopifnot(identical(x, x2))



# dgCMatrix -----------------------
x <- h5Read(file, "raw/X")
h5Write(x, tmp.file, "raw/X", overwrite = TRUE)
x2 <- h5Read(tmp.file, "raw/X")
stopifnot(identical(x, x2))



# list -----------------------
x <- h5Read(file)
h5Write(x, tmp.file, name = NULL, overwrite = TRUE)
x2 <- h5Read(tmp.file)
stopifnot(identical(x, x2))



[Package hdf5r.Extra version 0.0.6 Index]