Skip to contents

Reads an attribute associated with an HDF5 object (dataset or group).

Usage

h5_read_attr(file, name, attribute)

Arguments

file

Path to the HDF5 file.

name

Name of the object (dataset or group) the attribute is attached to.

attribute

Name of the attribute to read.

Value

A numeric, character, complex, factor, or raw vector/array. Returns NULL if the attribute has a null dataspace.

Details

  • Numeric attributes are read as numeric (double).

  • String attributes are read as character.

  • Complex attributes are read as complex.

  • enum attributes are read as factor.

  • 1-byte opaque attributes are read as raw.

Examples

file <- tempfile(fileext = ".h5")

# Create a dataset to attach attributes to
h5_write(file, "dset", 1)

# Write attributes of different types
h5_write_attr(file, "dset", "a_string", "some metadata")
h5_write_attr(file, "dset", "a_vector", c(1.1, 2.2))

# Read them back
str_attr <- h5_read_attr(file, "dset", "a_string")
vec_attr <- h5_read_attr(file, "dset", "a_vector")

print(str_attr)
#> [1] "some metadata"
print(vec_attr)
#> [1] 1.1 2.2
unlink(file)