3

I have created a dummy HDF5 object as

libray(rhdf5)
handle = h5createFile("rhd5file.h5")
df_A <- data.frame(height = rnorm(20,55,3), index = c(1:20))
df_B <- data.frame(age = rnorm(10,30,5), address = sample(letters,size= 10,replace = TRUE))
h5write(df_A, "rhd5file.h5", "A")
h5write(df_B, "rhd5file.h5", "B")
h5close(handle)

Now, I want to retrieve only column names of objects df_A and df_B without reading the entire file rhd5file.h5. Is it possible?

I tried h5readAttributes() and h5ls(), but none of them shows column names without reading entire object

Required output:

A: height, index
B: age, address
Haroon Lone
  • 2,837
  • 5
  • 29
  • 65

1 Answers1

2

You could write in the attributes the colnames, then you can read them without reading the entire file:

library(rhdf5)
 handle = h5createFile("rhd5file.h5")
 df_A <- data.frame(height = rnorm(20,55,3), index = c(1:20))
 df_B <- data.frame(age = rnorm(10,30,5), address = sample(letters,size= 10,replace = TRUE))
 h5write(df_A, "rhd5file.h5", "A")
 h5write(df_B, "rhd5file.h5", "B")

 file=H5Fopen("rhd5file.h5")
 did <- H5Dopen(file,"A")
 h5writeAttribute(did, attr=names(df_A),name="colnames")

 H5Dclose(did)
 H5close()

 h5readAttributes("rhd5file.h5","A")

Hope this help.

user3507085
  • 700
  • 5
  • 17