Need to open H5 files

I am writing code for multiple people in my group to use. Some are very familiar with R and others are not. So I need to write code that is rather user friendly. The less the end user has to type the better. I need to access H5 files from multiple files( which I cannot combine) and having switch cases for the multiple files that will need to be pulled from the 100 files in each folder.
I am able to open all the files on my own with no problem and do the needed calculations and export the graphs.

This is the easy part

# a crap ton of libraries have been cut out and I took out my while loop 

 askUser_file_x <- readline(prompt="Please, type on DS you wish to analyze, else type none (format DS##):   ")

  x <- str_extract(askUser_file_x, "(?<=DS)[0-9]+")
  x <-as.numeric(x) 
if (x==23){
  }else if  (x>=24 & x<=26){
  }else if  (x>=27 & x<=29){  
  }else if  (x>=30 & x<=36){  
  }else if  (x>=01 & x<=06){
  }else if  (x>=37 & x<=44){
  }else if  (x>=07 & x<=18){
  }else if  (x>=19 & x<=22){
  }else if  (x>=45 & x<=46){
  }else if  (x>=47 & x<=50){
    print( "DataSet does not exist")

       DS01= (file <-paste0("VA_000",c(282:286))),
       DS03= (file <-paste0("VA_000",c(266:271))),
       DS04= (file <-paste0("VA_000",c(272:276))),
       DS05= (file <-paste0("VA_000",c(277:281))),
       DS06= (file <-paste0("VA_000",c(290:294))),
       DS07= (file <-paste0("VA_000",c(316:320))),
       DS08 =(file <-paste0("VA_000",c(321:325))),
       DS09 =(file <-paste0("VA_000",c(326:330))),
       DS10 =(file <-paste0("VA_000",c(331:335))),
       DS11 =(file <-paste0("VA_000",c(295:300))),
       DS12 =(file <-paste0("VA_000",c(301:305)))
    files <-paste0(file, ".h5")
   H5_files<-lapply(files, H5Fopen)
   askUser_test_1 <- readline(prompt="Please, select from list:   ")
  x_str_test_1 <- as.character( str_extract(askUser_test_1, "(Vout_TM)[0-9]+") )

      # shows data in raw form

      #lists all the available sub directories in the .h5 file

All of that was easy works great no problems. But now I need to drill deeper in the by subsetting over a loop it just does not work. And I am at a loss what to do.

# h5ls(H5_files[[1]])  

  for (i in H5_files){
      File1<-H5Fopen(H5_files[[i]], askUser_test_1)#access the file for comparision.  
     File2<-h5read(H5_files[[i]], askUser_test_1)# the acessed files must be of the same type
     #  File3<-h5read(H5_files[[3]], askUser_test_1)  this works if it is run independently so I know that I have not made mistakes in the previous code.  

I have tried both H5Fopen and h5 read if the numbers are in there it works but the i does not.
This is the error that I get Error in H5_files[[i]] : invalid subscript type 'S4'. If some one could point me in the right direction that would be great. Thanks in advance

1 Like

Interesting question and pretty obvious why a reproducible example, called a reprex can be impractical sometimes.

I'll go out on a limb and guess that you're coming to R from a procedural/imperative programming background and you think of i as simply an iterator that is going to take on a succession of integer values equal to the length of H5_files. But that's not what's happening in the code.

Inspect the structure of HS_files


and I'm darned sure you'll see that these are S4 objects, not integers. And, of course, you need an integer to subset, not an S4 object.

But that's only the immediate obstacle. Although you can probably fix it with syntactical R code, I'd suggest instead writing a function to deal with a single H5 object to get the result you want, and then use purrr::map to apply it to the collection of those.

Come on back if I've sent you down a rabbit hole.

Okay White Rabbit, I have hit first a beautiful then a sour note.
I have the beautiful:

      #lists all the available subdirectories in the ##.h5 file
data_subsets<- purrr::map2(H5_files, askUser_test_1,  h5read)

and that gets me one more layer down and I can see the datasets that I am looking for in all of my [[1]] to [[6]] . What I need to access is the one called Data, but if I use the same purr::map2 it tells me

 df <-purrr::map2(data_subsets, "/Data", H5Fopen)
Error in .f(.x[[i]], .y[[1L]], ...) : 
  'name' must be a character string of length 1

whether or not the / is there or if I use h5read. Is there adifferent function that I need to use? If you can help me that would make you the ricky ist rick of them all.

1 Like

Well! You're catching on quicker than I can follow. Please feed my head. Since we're not directly dealing with H5 objects, could you gin up an reprex to give me a data_subsets object to play with?

I think the problem is that "/Data" isn't a vector, but the Red Queen is walking backwards.

Well I didn't have to make a beast of myself.
So I was able to use purrr to get exactly what I want

It puts all the components of the H5 files into a dataframe regardless of the number of files that I have. I have so many layer that it can get out of hand trying to explain them all. These were files I was give so I have to work with what I have. LOL

data_subsets<- purrr::map2(H5_files, askUser_test_1,  h5read)
df <-purrr::map_dfc(data_subsets, "Data", H5Fopen)

Thanks so much for directing me to the correct package.

1 Like

Great! For the benefit of those who follow, could you mark this as the solution, please? No false modesty.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.