Skip to content

Commit 60edee7

Browse files
committed
refactor out materialize_sparse_tibble()
1 parent bf5c657 commit 60edee7

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

R/fit.R

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

177-
if ((!allow_sparse(object)) && is_sparse_tibble(data)) {
178-
cli::cli_warn(
179-
"{.arg data} is a sparse tibble, but {.fn {class(object)[1]}} with
180-
engine {.code {object$engine}} doesn't accept that. Converting to
181-
non-sparse."
182-
)
183-
for (i in seq_along(ncol(data))) {
184-
# materialize with []
185-
data[[i]] <- data[[i]][]
186-
}
187-
}
188-
177+
data <- materialize_sparse_tibble(data, object, "data")
178+
189179
fit_interface <-
190180
check_interface(eval_env$formula, eval_env$data, cl, object)
191181

R/sparsevctrs.R

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ 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)) && is_sparse_tibble(x)) {
12-
cli::cli_warn(
13-
"{.arg x} is a sparse tibble, but {.fn {class(object)[1]}} with
14-
engine {.code {object$engine}} doesn't accept that. Converting to
15-
non-sparse."
16-
)
17-
for (i in seq_along(ncol(x))) {
18-
# materialize with []
19-
x[[i]] <- x[[i]][]
20-
}
21-
}
11+
x <- materialize_sparse_tibble(x, object, "x")
2212
}
2313
x
2414
}
2515

2616
is_sparse_tibble <- function(x) {
2717
any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))
28-
}
18+
}
19+
20+
materialize_sparse_tibble <- function(x, object, input) {
21+
if ((!allow_sparse(object)) && is_sparse_tibble(x)) {
22+
cli::cli_warn(
23+
"{.arg {input}} is a sparse tibble, but {.fn {class(object)[1]}} with
24+
engine {.code {object$engine}} doesn't accept that. Converting to
25+
non-sparse."
26+
)
27+
for (i in seq_along(ncol(x))) {
28+
# materialize with []
29+
x[[i]] <- x[[i]][]
30+
}
31+
}
32+
x
33+
}

0 commit comments

Comments
 (0)