Skip to content

Commit bf5c657

Browse files
committed
refactor out is_sparse_tibble()
1 parent 883624b commit bf5c657

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

R/convert_data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ maybe_sparse_matrix <- function(x) {
406406
return(x)
407407
}
408408

409-
if (any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))) {
409+
if (is_sparse_tibble(x)) {
410410
res <- sparsevctrs::coerce_to_sparse_matrix(x)
411411
} else {
412412
res <- as.matrix(x)

R/fit.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ fit.model_spec <-
174174
eval_env$formula <- formula
175175
eval_env$weights <- wts
176176

177-
if ((!allow_sparse(object)) &&
178-
any(vapply(data, sparsevctrs::is_sparse_vector, logical(1)))) {
177+
if ((!allow_sparse(object)) && is_sparse_tibble(data)) {
179178
cli::cli_warn(
180179
"{.arg data} is a sparse tibble, but {.fn {class(object)[1]}} with
181180
engine {.code {object$engine}} doesn't accept that. Converting to

R/fit_helpers.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ form_xy <- function(object, control, env,
131131
remove_intercept <- encoding_info %>% dplyr::pull(remove_intercept)
132132
allow_sparse_x <- encoding_info %>% dplyr::pull(allow_sparse_x)
133133

134-
if (allow_sparse_x && any(vapply(env$data, sparsevctrs::is_sparse_vector, logical(1)))) {
134+
if (allow_sparse_x && is_sparse_tibble(env$data)) {
135135
target <- "dgCMatrix"
136136
}
137137

R/sparsevctrs.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ to_sparse_data_frame <- function(x, object) {
88
engine {.code {object$engine}} doesn't accept that.")
99
}
1010
} else if (is.data.frame(x)) {
11-
if ((!allow_sparse(object)) &&
12-
any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))) {
11+
if ((!allow_sparse(object)) && is_sparse_tibble(x)) {
1312
cli::cli_warn(
1413
"{.arg x} is a sparse tibble, but {.fn {class(object)[1]}} with
1514
engine {.code {object$engine}} doesn't accept that. Converting to
@@ -22,4 +21,8 @@ to_sparse_data_frame <- function(x, object) {
2221
}
2322
}
2423
x
24+
}
25+
26+
is_sparse_tibble <- function(x) {
27+
any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))
2528
}

tests/testthat/test-sparsevctrs.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ test_that("maybe_sparse_matrix() is used correctly", {
112112

113113
local_mocked_bindings(
114114
maybe_sparse_matrix = function(x) {
115-
if (any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))) {
115+
if (is_sparse_tibble(x)) {
116116
stop("sparse vectors detected")
117117
} else {
118118
stop("no sparse vectors detected")

0 commit comments

Comments
 (0)