diff --git a/NAMESPACE b/NAMESPACE index 297f482..b248cd6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(as_sparse_logical) export(coerce_to_sparse_data_frame) export(coerce_to_sparse_matrix) export(coerce_to_sparse_tibble) +export(has_sparse_elements) export(is_sparse_character) export(is_sparse_double) export(is_sparse_integer) diff --git a/R/has_sparse_elements.R b/R/has_sparse_elements.R new file mode 100644 index 0000000..634d28c --- /dev/null +++ b/R/has_sparse_elements.R @@ -0,0 +1,40 @@ +#' Check for sparse elements +#' +#' This function checks to see if a data.frame, tibble or list contains one or +#' more sparse vectors. +#' +#' @param x a data frame, tibble, or list. +#' +#' @details +#' The checking in this function is done using [is_sparse_vector()], but is +#' implemented using an early exit pattern to provide fast performance for wide +#' data.frames. +#' +#' This function does not test whether `x` is a data.frame, tibble or list. It +#' simply iterates over the elements and sees if they are sparse vectors. +#' +#' @return A single logical value. +#' +#' @examplesIf rlang::is_installed("Matrix") +#' set.seed(1234) +#' n_cols <- 10000 +#' mat <- matrix(sample(0:1, n_cols * 10, TRUE, c(0.9, 0.1)), ncol = n_cols) +#' colnames(mat) <- as.character(seq_len(n_cols)) +#' sparse_mat <- Matrix::Matrix(mat, sparse = TRUE) +#' +#' res <- coerce_to_sparse_tibble(sparse_mat) +#' has_sparse_elements(res) +#' +#' has_sparse_elements(mtcars) +#' @export +has_sparse_elements <- function(x) { + res <- FALSE + + for (elt in x) { + if (is_sparse_vector(elt)) { + res <- TRUE + break + } + } + res +} \ No newline at end of file diff --git a/_pkgdown.yml b/_pkgdown.yml index 9a4d12f..5f8820c 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -30,3 +30,4 @@ reference: contents: - type-predicates - extractors + - has_sparse_elements diff --git a/man/has_sparse_elements.Rd b/man/has_sparse_elements.Rd new file mode 100644 index 0000000..c06698d --- /dev/null +++ b/man/has_sparse_elements.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/has_sparse_elements.R +\name{has_sparse_elements} +\alias{has_sparse_elements} +\title{Check for sparse elements} +\usage{ +has_sparse_elements(x) +} +\arguments{ +\item{x}{a data frame, tibble, or list.} +} +\value{ +A single logical value. +} +\description{ +This function checks to see if a data.frame, tibble or list contains one or +more sparse vectors. +} +\details{ +The checking in this function is done using \code{\link[=is_sparse_vector]{is_sparse_vector()}}, but is +implemented using an early exit pattern to provide fast performance for wide +data.frames. + +This function does not test whether \code{x} is a data.frame, tibble or list. It +simply iterates over the elements and sees if they are sparse vectors. +} +\examples{ +\dontshow{if (rlang::is_installed("Matrix")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +set.seed(1234) +n_cols <- 10000 +mat <- matrix(sample(0:1, n_cols * 10, TRUE, c(0.9, 0.1)), ncol = n_cols) +colnames(mat) <- as.character(seq_len(n_cols)) +sparse_mat <- Matrix::Matrix(mat, sparse = TRUE) + +res <- coerce_to_sparse_tibble(sparse_mat) +has_sparse_elements(res) + +has_sparse_elements(mtcars) +\dontshow{\}) # examplesIf} +}