I\'m trying to save an array as a HDF5 file using R, but having no luck.
To try and diagnose the problem I ran example(hdf5save)
. This successfully crea
I've also run into the same issue and found a reasonable fix.
The issue seems like it stems from when the hdf5 library finalizes the file. If it doesn't get a chance to finalize the file, then the file is corrupted. I think this happens after the buffer is flushed but the buffer doesn't always flush.
One solution I've found is to do the hdf5save in a separate function. Assign the variables into the globalenv(), then call hdf5save and exit the function. When the function completes, the memory seems to clean up which makes the hdf5 libarary flush the buffer and finalize the file.
Hope this helps!
I have had this problem. I am not sure of the cause and neither are the hdf5 maintainers. The authors of the R package have not replied.
Alternatives that work
In the time since I originally answered, the hdf5 package has been archived, and suitable alternatives (h5r
, rhdf5
, and ncdf4) have been created; I am currently using
ncdf4`:
Workarounds Two functional but unsatisfactory workarounds that I used prior to finding the alternatives above:
ncdf4
library).A minimal, reproducible demonstration of the issue:
Here is a reproducible example that sends an error
First R session
library(hdf5)
dat <- 1:10
hdf5save("test.h5","dat")
q()
n # do not save workspace
Second R session:
library(hdf5)
hdf5load("test.h5")
output:
HDF5-DIAG: Error detected in HDF5 library version: 1.6.10 thread
47794540500448. Back trace follows.
#000: H5F.c line 2072 in H5Fopen(): unable to open file
major(04): File interface
minor(17): Unable to open file
#001: H5F.c line 1852 in H5F_open(): unable to read superblock
major(04): File interface
minor(24): Read failed
#002: H5Fsuper.c line 114 in H5F_read_superblock(): unable to find file
signature
major(04): File interface
minor(19): Not an HDF5 file
#003: H5F.c line 1304 in H5F_locate_signature(): unable to find a valid
file signature
major(05): Low-level I/O layer
minor(29): Unable to initialize object
Error in hdf5load("test.h5") : unable to open HDF file: test.h5