Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make recipe() work with sparse matrices #1367

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* `recipe()` now works with sparse tibbles. (#1364)

* `recipe()` now works with sparse matrices. (#1364)

# recipes 1.1.0

## Improvements
Expand Down
11 changes: 11 additions & 0 deletions R/recipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ recipe <- function(x, ...) {
#' @rdname recipe
#' @export
recipe.default <- function(x, ...) {

# Doing this here since it should work for all types of Matrix classes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it is a S4 class, I found this way of handling it the cleanest. It happens when you go recipe(data, ...).

if (is_sparse_matrix(x)) {
x <- sparsevctrs::coerce_to_sparse_tibble(x)
return(recipe(x, ...))
}

cli::cli_abort(c(
x = "{.arg x} should be a data frame, matrix, formula, or tibble.",
i = "{.arg x} is {.obj_type_friendly {x}}."
Expand Down Expand Up @@ -208,6 +215,10 @@ recipe.formula <- function(formula, data, ...) {
))
}

if (is_sparse_matrix(data)) {
data <- sparsevctrs::coerce_to_sparse_tibble(data)
}

if (!is_tibble(data)) {
data <- as_tibble(data)
}
Expand Down
4 changes: 4 additions & 0 deletions R/sparsevctrs.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
is_sparse_tibble <- function(x) {
any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))
}

is_sparse_matrix <- function(x) {
methods::is(x, "sparseMatrix")
}
30 changes: 30 additions & 0 deletions tests/testthat/test-sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,33 @@ test_that("recipe() accepts sparse tibbles", {
)
})

test_that("recipe() accepts sparse matrices", {
skip_if_not_installed("modeldata")

hotel_data <- sparse_hotel_rates()

expect_no_condition(
rec_spec <- recipe(avg_price_per_room ~ ., data = hotel_data)
)

expect_true(
is_sparse_tibble(rec_spec$template)
)

expect_no_condition(
rec_spec <- recipe(hotel_data)
)

expect_true(
is_sparse_tibble(rec_spec$template)
)

expect_no_condition(
rec_spec <- recipe(hotel_data, avg_price_per_room ~ .)
)

expect_true(
is_sparse_tibble(rec_spec$template)
)
})

Loading