R/toCsparse.R

Defines functions toCsparse

Documented in toCsparse

#' Convert a SparseArraySeed to a CsparseMatrix
#'
#' Exactly what it says in the title.
#'
#' @param x Any object produced by block processing with \code{\link{colBlockApply}} or \code{\link{rowBlockApply}}.
#' This can be a matrix, sparse matrix or a two-dimensional \linkS4class{SparseArraySeed}.
#'
#' @return \code{x} is returned unless it was a \linkS4class{SparseArraySeed},
#' in which case an appropriate \linkS4class{CsparseMatrix} object is returned instead.
#'
#' @details 
#' This is intended for use inside functions to be passed to \code{\link{colBlockApply}} or \code{\link{rowBlockApply}}.
#' The idea is to pre-process blocks for user-defined functions that don't know how to deal with SparseArraySeed objects,
#' which is often the case for R-defined functions that do not benefit from \pkg{beachmat}'s C++ abstraction.
#'
#' @author Aaron Lun
#'
#' @examples
#' library(DelayedArray)
#' out <- SparseArraySeed(c(10, 10), 
#'     nzindex=cbind(1:10, sample(10)),
#'     nzdata=runif(10))
#' toCsparse(out)
#'
#' @export
toCsparse <- function(x) {
    if (is(x, "SparseArraySeed")) {
        x <- as(x, "sparseMatrix")
    }
    x
}
LTLA/beachmat documentation built on April 1, 2024, 1:22 p.m.