I have a hdf5 file with multiple variables that I want to automatically store in a list or a matrix.
library(rhdf5)
file = H5Fopen("myfile.h5")
file
HDF5 FILE
name /
filename
name otype dclass dim
0 AGB_CO H5I_DATASET FLOAT 3
1 AGB_CUT H5I_DATASET FLOAT 17 x 11 x 1
2 AGB_GROWTH H5I_DATASET FLOAT 17 x 11 x 1
3 AGB_MORT H5I_DATASET FLOAT 17 x 11 x 1
4 AGE H5I_DATASET FLOAT 1
5 AREA H5I_DATASET FLOAT 1
6 AREA_SI H5I_DATASET FLOAT 1
7 BALIVE H5I_DATASET FLOAT 3
8 BASAL_AREA_CUT H5I_DATASET FLOAT 17 x 11 x 1
9 BASAL_AREA_GROWTH H5I_DATASET FLOAT 17 x 11 x 1
10 BASAL_AREA_MORT H5I_DATASET FLOAT 17 x 11 x 1
h5info=h5ls("myfile.h5")
names=h5info$name
names
[1] "AGB_CO" "AGB_CUT" "AGB_GROWTH" "AGB_MORT"
[5] "AGE" "AREA" "AREA_SI" "BALIVE"
[9] "BASAL_AREA_CUT" "BASAL_AREA_GROWTH" "BASAL_AREA_MORT" "BA_CO"
I can access any variable in the file by typing
file$AGB_CO
[1] 0.04595303 0.02707645 0.03625819
Now I would like to use a loop to save all variables to memory. Something like
for (i in 1:names) {
vari=paste0("file$",names[i])
assign(vari,NA)
*some list or matrix*=as.name(vari)
}
I have tried assign(), eval(), as.name(), none of them works, but I am not able to save them into another variable. However if I type myvar=file$AGB_CO it works. How to implement this?
There are two solutions I have found. One is to use the above mentioned command h5read("myfile.h5","/"), otherwise the rhdf5 has a specific command h5dump that will take al the content of a file and save it as in a variable while keeping the structure.