rrhdf5

Reading many .h5 files at once


I am using rhdf5 library. And I want to read many files at once. I have my .h5 files in folder input, then I was tring:

filenames <- list.files("input", pattern="*.h5", full.names=TRUE)
read_h5<- function(file) {h5read(file, "/datasets/data1/data0")}
for (i in 1:length(filenames)) {
   read_h5(filenames[i])
}

It did not show any error. Just I execute it and nothing happens. I also tried lapply(filenames,h5read(filenames,name="/datasets/data1/data0"),.GlobalEnv)

but here I receive an error: "the condition has length > 1 and only the first element will be used".

Why it does't work?


Solution

  • In case that someone will have similar problem, I will share with you how I deal with this problem. Unfortunately, I had to change a little bit my attempt.

    My script in R looks like this (test.R):

    library(rhdf5)
    args <- commandArgs(trailingOnly = TRUE)
    filename <- args[1]
    output<-args[2]
    my_data<-h5read(filename, "/datasets/data1/data0")
    write.table(my_data, file=output, row.names=FALSE, col.names=FALSE)
    

    Later, I wrote a bash script:

    #!/bin/bash
    
    for file in input/*.h5; do 
        [ -f "$file" ] || continue
        beg="${file%%.*}";
        Rscript test.R "$file" "$beg".txt
    done
    

    And this works for me!