Skip to content

Commit

Permalink
update sparse_hotel_rates()
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Oct 4, 2024
1 parent 706d1ea commit 78d7fd2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 12 additions & 2 deletions tests/testthat/helper-sparsevctrs.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ------------------------------------------------------------------------------
# For sparse tibble testing

sparse_hotel_rates <- function() {
sparse_hotel_rates <- function(tibble = FALSE) {
# 99.2 sparsity
hotel_rates <- modeldata::hotel_rates

Expand All @@ -22,5 +22,15 @@ sparse_hotel_rates <- function() {
)

res <- as.matrix(res)
Matrix::Matrix(res, sparse = TRUE)
res <- Matrix::Matrix(res, sparse = TRUE)

if (tibble) {
res <- sparsevctrs::coerce_to_sparse_tibble(res)

# materialize outcome
withr::local_options("sparsevctrs.verbose_materialize" = NULL)
res$avg_price_per_room <- res$avg_price_per_room[]
}

res
}
9 changes: 3 additions & 6 deletions tests/testthat/test-sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ test_that("recipe() accepts sparse tibbles", {
skip_if_not_installed("modeldata")
withr::local_options("sparsevctrs.verbose_materialize" = 3)

hotel_data <- sparse_hotel_rates()
hotel_data <- sparsevctrs::coerce_to_sparse_tibble(hotel_data)
hotel_data <- sparse_hotel_rates(tibble = TRUE)

expect_no_condition(
rec_spec <- recipe(avg_price_per_room ~ ., data = hotel_data)
Expand Down Expand Up @@ -34,8 +33,7 @@ test_that("prep() accepts sparse tibbles", {
skip_if_not_installed("modeldata")
withr::local_options("sparsevctrs.verbose_materialize" = 3)

hotel_data <- sparse_hotel_rates()
hotel_data <- sparsevctrs::coerce_to_sparse_tibble(hotel_data)
hotel_data <- sparse_hotel_rates(tibble = TRUE)

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

Expand All @@ -60,8 +58,7 @@ test_that("bake() accepts sparse tibbles", {
skip_if_not_installed("modeldata")
withr::local_options("sparsevctrs.verbose_materialize" = 3)

hotel_data <- sparse_hotel_rates()
hotel_data <- sparsevctrs::coerce_to_sparse_tibble(hotel_data)
hotel_data <- sparse_hotel_rates(tibble = TRUE)

rec_spec <- recipe(avg_price_per_room ~ ., data = hotel_data) %>%
prep()
Expand Down

0 comments on commit 78d7fd2

Please sign in to comment.