Skip to content

Commit 5ce414e

Browse files
Merge pull request #1211 from tidymodels/misc-sparsevctrs
Misc sparsevctrs
2 parents ee072ce + 3bf37b5 commit 5ce414e

File tree

8 files changed

+30
-12
lines changed

8 files changed

+30
-12
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Imports:
3232
prettyunits,
3333
purrr (>= 1.0.0),
3434
rlang (>= 1.1.0),
35-
sparsevctrs (>= 0.1.0.9000),
35+
sparsevctrs (>= 0.1.0.9002),
3636
stats,
3737
tibble (>= 2.1.1),
3838
tidyr (>= 1.3.0),

R/convert_data.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
)
4949
}
5050

51-
if (is_sparse_tibble(data)) {
51+
if (sparsevctrs::has_sparse_elements(data)) {
5252
cli::cli_abort(
5353
"Sparse data cannot be used with formula interface. Please use
5454
{.fn fit_xy} instead."
@@ -417,7 +417,7 @@ maybe_sparse_matrix <- function(x) {
417417
return(x)
418418
}
419419

420-
if (is_sparse_tibble(x)) {
420+
if (sparsevctrs::has_sparse_elements(x)) {
421421
res <- sparsevctrs::coerce_to_sparse_matrix(x)
422422
} else {
423423
res <- as.matrix(x)

R/fit.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fit.model_spec <-
136136

137137

138138
if (is_sparse_matrix(data)) {
139-
data <- sparsevctrs::coerce_to_sparse_tibble(data)
139+
data <- sparsevctrs::coerce_to_sparse_tibble(data, rlang::caller_env(0))
140140
}
141141

142142
dots <- quos(...)

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 && is_sparse_tibble(env$data)) {
134+
if (allow_sparse_x && sparsevctrs::has_sparse_elements(env$data)) {
135135
target <- "dgCMatrix"
136136
}
137137

R/predict.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ prepare_data <- function(object, new_data) {
470470
if (allow_sparse(object) && inherits(new_data, "dgCMatrix")) {
471471
return(new_data)
472472
}
473-
if (allow_sparse(object) && is_sparse_tibble(new_data)) {
473+
if (allow_sparse(object) && sparsevctrs::has_sparse_elements(new_data)) {
474474
new_data <- sparsevctrs::coerce_to_sparse_matrix(new_data)
475475
return(new_data)
476476
}

R/sparsevctrs.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,12 @@ to_sparse_data_frame <- function(x, object, call = rlang::caller_env()) {
3737
x
3838
}
3939

40-
is_sparse_tibble <- function(x) {
41-
any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))
42-
}
43-
4440
is_sparse_matrix <- function(x) {
4541
methods::is(x, "sparseMatrix")
4642
}
4743

4844
materialize_sparse_tibble <- function(x, object, input) {
49-
if (is_sparse_tibble(x) && (!allow_sparse(object))) {
45+
if (sparsevctrs::has_sparse_elements(x) && (!allow_sparse(object))) {
5046
if (inherits(object, "model_fit")) {
5147
object <- object$spec
5248
}

tests/testthat/_snaps/sparsevctrs.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
Warning:
3131
`data` is a sparse tibble, but `linear_reg()` with engine "lm" doesn't accept that. Converting to non-sparse.
3232

33+
---
34+
35+
Code
36+
fit(spec, avg_price_per_room ~ ., data = hotel_data)
37+
Condition
38+
Error in `fit()`:
39+
! `x` must have column names.
40+
3341
# sparse tibble can be passed to `fit_xy() - unsupported
3442

3543
Code

tests/testthat/test-sparsevctrs.R

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ test_that("maybe_sparse_matrix() is used correctly", {
282282

283283
local_mocked_bindings(
284284
maybe_sparse_matrix = function(x) {
285-
if (is_sparse_tibble(x)) {
285+
if (sparsevctrs::has_sparse_elements(x)) {
286286
stop("sparse vectors detected")
287287
} else {
288288
stop("no sparse vectors detected")
@@ -313,3 +313,17 @@ test_that("maybe_sparse_matrix() is used correctly", {
313313
fit_xy(spec, x = tibble::as_tibble(mtcars)[, -1], y = tibble::as_tibble(mtcars)[, 1])
314314
)
315315
})
316+
317+
test_that("fit() errors if sparse matrix has no colnames", {
318+
hotel_data <- sparse_hotel_rates()
319+
colnames(hotel_data) <- NULL
320+
321+
spec <- boost_tree() %>%
322+
set_mode("regression") %>%
323+
set_engine("xgboost")
324+
325+
expect_snapshot(
326+
error = TRUE,
327+
fit(spec, avg_price_per_room ~ ., data = hotel_data)
328+
)
329+
})

0 commit comments

Comments
 (0)