Learn R Programming

h5 (version 0.9.9)

H5File: HDF5 File Objects

Description

*H5File* objects are are the main entry point to access HDF5 data from binary files. The *H5File* S4 class directly maps https://www.hdfgroup.org/HDF5/doc/cpplus_RM/class_h5_1_1_h5_file.html objects from the C++ API to R. Through the implemented class hierarchy it shares common functionality with *H5Group*.

Usage

h5file(name, mode = "a")

H5File(name, mode = "a")

h5flush(.Object)

# S4 method for H5File h5flush(.Object)

# S4 method for H5File h5close(.Object)

is.h5file(name)

Arguments

name

character; File path pointing to H5File.

mode

mode used for file The following modes are supported by h5file:

r

Read only, file must exist.

r+

Read/write, file must exist.

w

Create file, truncate if exists.

w-

Create file, fail if exists.

a

Read/write if exists, create otherwise (default).

.Object

H5File; S4 object of class H5File;

Reading and Writing Files

HDF5 files can be created and accessed using h5file(): file <- h5file(name = "test.h5", mode = "a")

The following access-modes are defined:

Mode Description
a Read/write if exists, create otherwise (default).
r Read only, file must exist.
r+ Read/write, file must exist.
w Create file, truncate if exists.

Show File Contents

HDF5 objects stored in a file are shown with the following symbols:

Mode Description
+ HDF5 Group.
D HDF5 Dataset.

Extract/List File Contents

The following functions are defined to extract HDF5 file contents:

list.groups

List HDF5 groups in file.

list.datasets

List HDF5 datasets in file.

list.attributes

List Attributes of HDF5 object (file, group or dataset).

Details

HDF5 files can be opened or generated using the h5file() function and a specified file access mode. h5file() returns a H5File object which can be used to access H5Groups and DataSets using subsetting parameters or according class methods.

HDF5 files which have been created or opened through h5file() need to be closed afterwards using h5close().

h5flush() can be used to flush unwritten data to an HDF5 file.

HDF5 Files can contain the following objects:

Groups

Similar to a file system folder, used to organize HDF5 objects in a hierarchical way.

Datasets

Objects to store actual data.

Attributes

Meta data objects to store extra informatino about Files, Groups and Datasets.

See Also

CommonFG CommonFG-Group CommonFG-DataSet H5Location-Attribute

Examples

Run this code
# NOT RUN {
# The following examples generates a HDF5 file with the different HDF5 
# Objects and shows its contents:
file <- h5file(name = "test1.h5", mode = "a")
file["testdataset"] <- 1:10
h5attr(file, "testattrib") <- LETTERS[1:10]
file["testgroup/testdataset2"] <- 1:10
file
# Close file and delete
h5close(file)
if(file.exists("test.h5")) file.remove("test.h5")

# The following example shows hdf5 file contents and how to use them to iterate over HDF5 elements:
file <- h5file(name = "test2.h5", mode = "a")
file["testgroup1/testset1"] <- 1:10
file["testgroup2/testset2"] <- 11:20
file["testgroup3/testset3"] <- 21:30

# Extract first 3 elements from each dataset and combine result to matrix
sapply(list.datasets(file, recursive = TRUE), function(x) file[x][1:3])
# Add new dataset to each group in HDF5 file
for(g in list.groups(file)) {
  file[paste(g, "testsetx", collapse = "/")] <- 1:10
}
list.datasets(file, recursive = TRUE)
# Close file
h5close(file)
# }

Run the code above in your browser using DataLab