set.seed(10)
library(omxr)
.omx
files are HDF5 containers storing many different matrices and their
attributes. You can create a new container with the create_omx
command.
zones <- 1:10 rhdf5::H5close() omxfile <- tempfile(fileext = ".omx") create_omx(omxfile, numrows = length(zones), numcols = length(zones))
Let's start with a 10-zone system with random trips and costs.
trips <- matrix(rnorm(n = length(zones)^2, 200, 50), nrow = length(zones), ncol = length(zones)) cost <- matrix(rlnorm(n = length(zones)^2, 1, 1), nrow = length(zones), ncol = length(zones))
We can store these in the omx container.
write_omx(file = omxfile, matrix = trips, "trips", description = "Total Trips") write_omx(file = omxfile, matrix = cost, "cost", description = "Generalized Cost")
Now that the matrices are in the file, we can try reading them.
read_omx(omxfile, "trips") read_omx(omxfile, "cost")
You can transform a matrix to long format, which is more tidyverse
friendly.
library(tidyverse) read_omx(omxfile, "trips") %>% gather_matrix("trips")
You can also read subsets of matrices.
read_omx(omxfile, "trips", row_index = 2:4, col_index = 2:5)
There are also functions to get the attributes of an OMX file. A call to
list_omx()
will show the names of the matrices inside the file.
get_omx_attr(omxfile) list_omx(omxfile)
Perhaps a better way to read a subset of a matrix is with a defined "lookup."
These can be stored in the omx container in addition to the matrices. You specify
the lookup with a vector of the appropriate length, where TRUE
means the
row or column should be included, and FALSE
means otherwise.
lookup <- zones %in% c(1, 2:5, 9) lookup write_lookup(omxfile, lookup_v = lookup, name = "trial", description = "test lookup", replace = TRUE)
Now we can get the selected rows and columns from either matrix automatically.
read_selected_omx(omxfile, "trips", row_selection = "trial", col_selection = "trial")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.