R Under development (unstable) (2023-11-29 r85646 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ## for R_DEFAULT_PACKAGES=NULL : > library(utils) > > library(Matrix) > > #### Read / Write (sparse) Matrix objects ---------------------- > > ### Rebuild the 'mm' example matrix, now in KNex data > > ### This is no longer really important, as we now use > ### ../data/KNex.R which creates the S4 object *every time* > data(KNex, package = "Matrix") > > ## recreate 'mm' from list : > sNms <- c("Dim", "i","p","x") > L <- lapply(sNms, function(SN) slot(KNex$mm, SN)); names(L) <- sNms > mm2 <- new(class(KNex$mm)) > for (n in sNms) slot(mm2, n) <- L[[n]] > > stopifnot(validObject(mm2), + identical(mm2, KNex$mm)) > L$y <- KNex$y > ## save(L, file = "/u/maechler/R/Pkgs/Matrix/inst/external/KNex_slots.rda") > > > ## recreate 'mm' from ASCI file : > mmT <- as(KNex$mm, "TsparseMatrix") > str(mmT) Formal class 'dgTMatrix' [package "Matrix"] with 6 slots ..@ i : int [1:8755] 0 2 25 27 163 165 1258 1261 1276 1278 ... ..@ j : int [1:8755] 0 0 0 0 0 0 0 0 0 0 ... ..@ Dim : int [1:2] 1850 712 ..@ Dimnames:List of 2 .. ..$ : NULL .. ..$ : NULL ..@ x : num [1:8755] 0.277 0.277 0.277 0.277 0.277 ... ..@ factors : list() > mm3 <- cbind(i = mmT@i, j = mmT@j, x = mmT@x) > write.table(mm3, file = "mm-Matrix.tab", row.names=FALSE)# -> ASCII version > > str(mmr <- read.table("mm-Matrix.tab", header = TRUE)) 'data.frame': 8755 obs. of 3 variables: $ i: int 0 2 25 27 163 165 1258 1261 1276 1278 ... $ j: int 0 0 0 0 0 0 0 0 0 0 ... $ x: num 0.277 0.277 0.277 0.277 0.277 ... > mmr$i <- as.integer(mmr$i) > mmr$j <- as.integer(mmr$j) > > mmN <- with(mmr, new("dgTMatrix", Dim = c(max(i)+1:1,max(j)+1:1), + i = i, j = j, x = x)) > > stopifnot(identical(mmT, mmN)) # !! > ## weaker (and hence TRUE too): > stopifnot(all.equal(as(mmN, "matrix"), + as(mmT, "matrix"), tolerance=0)) > > mm <- as(mmN, "CsparseMatrix") > stopifnot(all.equal(mm, KNex$mm)) > ## save(mm, file = "....../Matrix/data/mm.rda", compress = TRUE) > > > A <- Matrix(c(1,0,3,0,0,5), 10, 10, sparse = TRUE) # warning about [6] vs [10] Warning message: In Matrix(c(1, 0, 3, 0, 0, 5), 10, 10, sparse = TRUE) : data length [6] is not a sub-multiple or multiple of the number of rows [10] > (fname <- file.path(tempdir(), "kk.mm")) [1] "D:\\temp\\Rtmp8shCB3/kk.mm" > writeMM(A, fname) NULL > (B <- readMM(fname)) 10 x 10 sparse Matrix of class "dgTMatrix" [1,] 1 . 3 1 . 3 1 . 3 1 [2,] . 5 . . 5 . . 5 . . [3,] 3 1 . 3 1 . 3 1 . 3 [4,] . . 5 . . 5 . . 5 . [5,] . 3 1 . 3 1 . 3 1 . [6,] 5 . . 5 . . 5 . . 5 [7,] 1 . 3 1 . 3 1 . 3 1 [8,] . 5 . . 5 . . 5 . . [9,] 3 1 . 3 1 . 3 1 . 3 [10,] . . 5 . . 5 . . 5 . > validObject(B) [1] TRUE > Bc <- as(B, "CsparseMatrix") > stopifnot(identical(A, Bc)) > > fname <- system.file("external", "wrong.mtx", package = "Matrix") > r <- try(readMM(fname)) Error : readMM(): row indices 'i' are not in 1:nrow[=2] > stopifnot(inherits(r, "try-error"), length(grep("readMM.*row.*1:nr", r)) == 1) > ## gave a much less intelligible error message > > proc.time() user system elapsed 1.14 0.04 1.17