# Create example data
input = list(
group_empty = list(),
dataset_empty = numeric(0),
group_dataframe = data.frame(
ID = as.integer(1:3),
Name = c("Alice", "Bob", "Charlie"),
Salary = c(2341.2, 3534.2, 4541.9),
Employed = c(TRUE, TRUE, FALSE)
),
dataset_parameters = 1.234,
group_integers = list(int32 = as.integer(123),
int64 = bit64::as.integer64(123),
vector = bit64::as.integer64((1:3)+12345678912345)),
dataset_nonnumeric = c(NA, NaN, Inf, -Inf),
group_mixed = list(
header = 'test header',
subgroup1 = list(
dataset1 = c("A", "%}~&^", "x1y2z3", "", " "),
dataset2 = matrix(as.integer(1:10), nrow = 2),
dataset3 = array(runif(30), dim = c(2, 5, 3))
),
subgroup2 = list(date = as.character(as.Date("2025-01-01")),
location = 'Perth')
)
)
# Add attributes to some datasets
attr(input$dataset_empty,'Comment') = 'This is a test file.'
attr(input$group_mixed$subgroup1$dataset3,'Type') = '3D array'
attr(input$group_integers$vector,'Comment') = 'Vector of long integers'
attr(input$group_integers$vector,'Package') = 'bit64'
# Add attributes to some groups
attr(input$group_dataframe,'Company branch') = 'Sales'
attr(input$group_integers,'Comment') = 'Testing different integers'
attr(input$group_empty,'Timestamp') = date()
attr(input$group_empty,'Working directory') = getwd()
# Write list to HDF5 file
filename = tempfile()
writehdf5(input, filename)
# Read HDF5 file into a new list
output = readhdf5(filename)
# Check if input and output lists are identical
# (up to alphabetic ordering and data frame-to-list conversion)
print(all.equal(sortlist(input, convert.data.frames = TRUE), output))
# Write list to HDF5 file again, this time with inherent attributes, allowing
# to keep track of the data frames
filename = tempfile()
writehdf5(input, filename, inherent.attributes = TRUE)
# Read HDF5 file into a new list
output = readhdf5(filename)
# Check if input and output lists are identical
# (up to alphabetic ordering only)
print(all.equal(sortlist(input), output))
Run the code above in your browser using DataLab