Set Up the Environment for the HDF5 package:

# Use the following code
# Install BiocManager first
#install.packages("BiocManager")
#BiocManager::install()

#Bioconductor version 3.10 (BiocManager 1.30.10), R
#  3.6.3 (2020-02-29)
#Installing package(s) 'BiocVersion'
#trying URL 'https://bioconductor.org/packages/3.10/bioc/bin/macosx#/el-capitan/contrib/3.6/BiocVersion_3.10.1.tgz'
#Content type 'application/x-gzip' length 5574 bytes
#==================================================
#downloaded 5574 bytes

#Now install the "rhdf5"
BiocManager::install("rhdf5")
## Bioconductor version 3.11 (BiocManager 1.30.10), R 4.0.2 (2020-06-22)
## Installing package(s) 'rhdf5'
## package 'rhdf5' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'rhdf5'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\angul\Documents\R\win-library\4.0\00LOCK\rhdf5\libs\x64\rhdf5.dll to C:
## \Users\angul\Documents\R\win-library\4.0\rhdf5\libs\x64\rhdf5.dll: Permission
## denied
## Warning: restored 'rhdf5'
## 
## The downloaded binary packages are in
##  C:\Users\angul\AppData\Local\Temp\Rtmp8asTXd\downloaded_packages
## Installation path not writeable, unable to update packages: survival
# The package is now intalled. Load it like every other library
library(rhdf5)
file <- h5createFile("example.h5")
file
## [1] TRUE

Create Groups:

#create groups
created = h5createGroup("example.h5", "foo")
created = h5createGroup("example.h5", "baa")
#create groups with subgroups
created = h5createGroup("example.h5", "foo/foobaa")
#see groups and subgroups
h5ls("example.h5")
##   group   name     otype dclass dim
## 0     /    baa H5I_GROUP           
## 1     /    foo H5I_GROUP           
## 2  /foo foobaa H5I_GROUP

Write to groups:

# create data & write to groups
A = matrix(1:10, nr=5, nc=2)
h5write(A, "example.h5", "foo/A")

B = array(seq(0.1,2.0,by=0.1),dim=c(5,2,2))
attr(B,"scale") <-"liter"
h5write(B, "example.h5", "foo/foobaa/B")
# list out data sets
h5ls("example.h5")
##         group   name       otype  dclass       dim
## 0           /    baa   H5I_GROUP                  
## 1           /    foo   H5I_GROUP                  
## 2        /foo      A H5I_DATASET INTEGER     5 x 2
## 3        /foo foobaa   H5I_GROUP                  
## 4 /foo/foobaa      B H5I_DATASET   FLOAT 5 x 2 x 2

Write a data set:

#create a dataframe
df=data.frame(1L:5L,seq(0,1,length.out = 5),
              c("ab", "cde", "fghi", "a", "s"),
              stringsAsFactors = FALSE)

#write to the top level group
h5write(df,"example.h5","df")

#list out to see the dimensions of the dataset
h5ls("example.h5")
##         group   name       otype   dclass       dim
## 0           /    baa   H5I_GROUP                   
## 1           /     df H5I_DATASET COMPOUND         5
## 2           /    foo   H5I_GROUP                   
## 3        /foo      A H5I_DATASET  INTEGER     5 x 2
## 4        /foo foobaa   H5I_GROUP                   
## 5 /foo/foobaa      B H5I_DATASET    FLOAT 5 x 2 x 2

Reading from HDF5:

#how to read data
readA = h5read("example.h5", "foo/A")
readA
##      [,1] [,2]
## [1,]    1    6
## [2,]    2    7
## [3,]    3    8
## [4,]    4    9
## [5,]    5   10

Writing & reading chunks:

#write to the dataset A
h5write(c(12,13,14), "example.h5", "foo/A", index=list(1:3, 1))
#read the dataset A
h5read("example.h5", "foo/A")
##      [,1] [,2]
## [1,]   12    6
## [2,]   13    7
## [3,]   14    8
## [4,]    4    9
## [5,]    5   10

If you have any questions about these intructions please contact me at https://www.linkedin.com/in/lindangulopez/.