Safely checks if a file, an object within a file, or an attribute on an object exists.
Arguments
- file
Path to the file.
- name
The full path of the object to check (e.g.,
"/data/matrix"). Defaults to"/"to test file validity.- attr
The name of an attribute to check. If provided, the function tests for the existence of this attribute on
name.- assert
Logical. If
TRUEand the target does not exist, the function will stop with an informative error message instead of returningFALSE. Defaults toFALSE.
Details
This function provides a robust, error-free way to test for existence.
Testing for a File: If
nameis/andattrisNULL, the function checks iffileis a valid, readable HDF5 file.Testing for an Object: If
nameis a path (e.g.,/data/matrix) andattrisNULL, the function checks if the specific object exists.Testing for an Attribute: If
attris provided, the function checks if that attribute exists on the specified objectname.
Examples
file <- tempfile(fileext = ".h5")
h5_exists(file) # FALSE
#> [1] FALSE
h5_create_file(file)
h5_exists(file) # TRUE
#> [1] TRUE
h5_exists(file, "missing_object") # FALSE
#> [1] FALSE
h5_write(1:10, file, "my_ints")
h5_exists(file, "my_ints") # TRUE
#> [1] TRUE
h5_exists(file, "my_ints", "missing_attr") # FALSE
#> [1] FALSE
h5_write(1:10, file, "my_ints", attr = "my_attr")
h5_exists(file, "my_ints", "my_attr") # TRUE
#> [1] TRUE
unlink(file)
