
*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*.
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)
character; File path pointing to H5File.
mode used for file
The following modes are supported by h5file
:
Read only, file must exist.
Read/write, file must exist.
Create file, truncate if exists.
Create file, fail if exists.
Read/write if exists, create otherwise (default).
H5File; S4 object of class H5File
;
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. |
HDF5 objects stored in a file are shown with the following symbols:
Mode | Description |
+ | HDF5 Group. |
D | HDF5 Dataset. |
The following functions are defined to extract HDF5 file contents:
List HDF5 groups in file.
List HDF5 datasets in file.
List Attributes of HDF5 object (file, group or dataset).
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 H5Group
s and DataSet
s
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:
Similar to a file system folder, used to organize HDF5 objects in a hierarchical way.
Objects to store actual data.
Meta data objects to store extra informatino about Files, Groups and Datasets.
CommonFG
CommonFG-Group
CommonFG-DataSet
H5Location-Attribute
# 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