## ----include=FALSE------------------------------------------------------------ old_options <- options( max.print = 10, # Set the maximum number of rows to display width = 80 # Set the maximum number of columns to display ) ## ----message=F---------------------------------------------------------------- library(dbMatrix) library(Matrix) ## ----------------------------------------------------------------------------- set.seed(42) dgc <- Matrix::rsparsematrix(100, 50, density = 0.1, rand.x = function(n) rpois(n, 5) + 1) rownames(dgc) <- paste0("gene_", seq_len(100)) colnames(dgc) <- paste0("cell_", seq_len(50)) dplyr::glimpse(dgc) ## ----------------------------------------------------------------------------- # create dbSparseMatrix from the same dgc con <- DBI::dbConnect(duckdb::duckdb(), ":memory:") sparse <- dbMatrix( value = dgc, con = con, name = "test_matrix", class = "dbSparseMatrix", overwrite = TRUE ) # preview # show function aims to emulate the show method for dgCMatrix head(sparse) ## ----------------------------------------------------------------------------- dbMatrix::t(sparse) ## ----------------------------------------------------------------------------- dbMatrix::colMeans(sparse) ## ----------------------------------------------------------------------------- dbMatrix::colSums(sparse) ## ----------------------------------------------------------------------------- dbMatrix::rowMeans(sparse) ## ----------------------------------------------------------------------------- dbMatrix::rowSums(sparse) ## ----------------------------------------------------------------------------- dim(sparse) dim(dgc) ## ----------------------------------------------------------------------------- all.equal(dbMatrix::colMeans(sparse, memory = TRUE, names = TRUE), Matrix::colMeans(dgc)) all.equal(dbMatrix::colSums(sparse, memory = TRUE, names = TRUE), Matrix::colSums(dgc)) all.equal(dbMatrix::rowMeans(sparse, memory = TRUE, names = TRUE), Matrix::rowMeans(dgc)) all.equal(dbMatrix::rowSums(sparse, memory = TRUE, names = TRUE), Matrix::rowSums(dgc)) ## ----eval=TRUE, message=FALSE, warning=FALSE---------------------------------- # Create a dense matrix directly set.seed(42) mat <- matrix(rnorm(100), nrow = 10, ncol = 10) rownames(mat) <- paste0("row_", 1:10) colnames(mat) <- paste0("col_", 1:10) # Create dbDenseMatrix con2 <- DBI::dbConnect(duckdb::duckdb(), ":memory:") dense <- dbMatrix( value = mat, con = con2, name = "dense_matrix", class = "dbDenseMatrix", overwrite = TRUE ) # preview dense ## ----------------------------------------------------------------------------- dbMatrix::t(dense) ## ----------------------------------------------------------------------------- dbMatrix::colMeans(dense) ## ----------------------------------------------------------------------------- dbMatrix::colSums(dense) ## ----------------------------------------------------------------------------- dbMatrix::rowMeans(dense) ## ----------------------------------------------------------------------------- dbMatrix::rowSums(dense) ## ----------------------------------------------------------------------------- dbMatrix::mean(dense) ## ----------------------------------------------------------------------------- dim(dense) ## ----------------------------------------------------------------------------- DBI::dbDisconnect(con, shutdown = TRUE) DBI::dbDisconnect(con2, shutdown = TRUE) options(old_options) ## ----eval=TRUE, message=FALSE------------------------------------------------- sessionInfo()