file <- tempfile(fileext = ".h5")
# --- Write Data ---
h5_write(c(10L, 20L), file, "ints")
h5_write(I(TRUE), file, "ints", attr = "ready")
h5_write(c(10.5, 18), file, "floats")
h5_write(I("meters"), file, "floats", attr = "unit")
# --- Read Data ---
# Read dataset
x <- h5_read(file, "ints")
print(x)
# Read dataset with attributes
y <- h5_read(file, "floats")
print(attr(y, "unit"))
# Read a specific attribute directly
unit <- h5_read(file, "floats", attr = "unit")
print(unit)
# --- Type Conversion Examples ---
# Force integer dataset to be read as numeric (double)
x_dbl <- h5_read(file, "ints", as = "double")
class(x_dbl)
# Force attached attribute to be read as logical
# Note the "@" prefix to target the attribute
z <- h5_read(file, "ints", as = c("@ready" = "logical"))
print(z)
unlink(file)
Run the code above in your browser using DataLab