Safely checks if a file is a valid HDF5 file or if a specific object (group or dataset) exists within a valid HDF5 file.
Details
This function provides a robust, error-free way to test for existence.
Testing for a File: If
nameis/(the default), the function checks iffileis a valid, readable HDF5 file. It will returnFALSEfor non-existent files, non-HDF5 files, or corrupted files without raising an error.Testing for an Object: If
nameis a path (e.g.,/data/matrix), the function first confirms the file is valid HDF5, and then checks if the specific object exists within it.
Examples
file <- tempfile(fileext = ".h5")
h5_write(file, "my_data", 1:10)
# --- Test 1: Check for a specific object ---
h5_exists(file, "my_data") # TRUE
#> [1] TRUE
h5_exists(file, "nonexistent_data") # FALSE
#> [1] FALSE
# --- Test 2: Check for a valid HDF5 file ---
h5_exists(file) # TRUE
#> [1] TRUE
h5_exists(file, "/") # TRUE
#> [1] TRUE
# --- Test 3: Check invalid or non-existent files ---
h5_exists("not_a_real_file.h5") # FALSE
#> [1] FALSE
text_file <- tempfile()
writeLines("this is not hdf5", text_file)
h5_exists(text_file) # FALSE
#> [1] FALSE
# Check for an object in an invalid file (also FALSE)
h5_exists(text_file, "my_data") # FALSE
#> [1] FALSE
unlink(file)
unlink(text_file)
