knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
fastMatMR
is an R package that provides high-performance reading and writing
of Matrix Market files. It wraps around the fast_matrix_market C++
library, ensuring optimal
performance.
Performance vignettes for write and read operations are also provided.
Unlike other packages, such as Matrix
, fastMatMR
offers extended support
for:
.mtx
files..mtx
files.For performance benchmarks, see performance our vignettes for write and read operations.
To install the development version of fastMatMR
from GitHub:
install.packages("devtools") devtools::install_github("ropensci/fastMatMR")
Load the fastMatMR
package:
library(fastMatMR)
vec <- c(1, 2, 3) temp_file_vec <- tempfile(fileext = ".mtx") write_fmm(vec, temp_file_vec)
mat <- matrix(c(1, 2, 3, 4), nrow = 2) temp_file_mat <- tempfile(fileext = ".mtx") write_fmm(mat, temp_file_mat)
sp_mat <- Matrix::sparseMatrix(i = c(1, 3), j = c(2, 4), x = 7:8) temp_file_sp_mat <- tempfile(fileext = ".mtx") write_fmm(sp_mat, temp_file_sp_mat)
fastMatMR
will correctly roundtrip NaN
values.NA
values are not supported by the Matrix Market format, and are coerced to NaN
vec <- c(1, 2, 3.32, 225.61) temp_file_vec_r <- tempfile(fileext = ".mtx") vec_to_fmm(vec, temp_file_vec_r) fmm_to_vec(temp_file_vec_r)
Similarly, other fmm_to_
functions can be used to read from .mtx
files.
Sparse matrices can be written and read by the Matrix
library:
spmat <- Matrix::Matrix(c(1, 0, 3, NA), nrow = 2, sparse = TRUE) temp_file_sp_na <- tempfile(fileext = ".mtx") Matrix::writeMM(spmat, temp_file_sp_na) Matrix::readMM(temp_file_sp_na) ## NULL ## 2 x 2 sparse Matrix of class "dgTMatrix" ## [1,] 1 3e+00 ## [2,] . 1e+308
However, as can be seen above, NA
values are handled incorrectly, and cause
overflow. Dense matrices or vectors cannot be read or written in the matrix
market format by the Matrix
library.
Since the Matrix Market format is language agnostic, the .mtx
files produced
can even be read into Python:
{bash, eval=FALSE}
pip install fast_matrix_market
python -c 'import fast_matrix_market as fmm; print(fmm.read_array_or_coo("sparse.mtx"))'
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.