Skip to content

Commit 28420f6

Browse files
committed
refactor sparevctrs functions out
1 parent de70c14 commit 28420f6

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

R/convert_data.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,21 @@ maybe_matrix <- function(x) {
374374
"converted to numeric matrix: {non_num_cols}.")
375375
rlang::abort(msg)
376376
}
377-
if (any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))) {
378-
x <- sparsevctrs::coerce_to_sparse_matrix(x)
379-
} else {
380-
x <- as.matrix(x)
381-
}
377+
x <- maybe_sparse_matrix(x)
382378
}
383379
# leave alone if matrix or sparse matrix
384380
x
385381
}
386382

383+
maybe_sparse_matrix <- function(x) {
384+
if (any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))) {
385+
res <- sparsevctrs::coerce_to_sparse_matrix(x)
386+
} else {
387+
res <- as.matrix(x)
388+
}
389+
res
390+
}
391+
387392
#' @rdname maybe_matrix
388393
#' @export
389394
maybe_data_frame <- function(x) {

R/fit.R

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,7 @@ fit_xy.model_spec <-
275275
}
276276
}
277277

278-
if (methods::is(x, "sparseMatrix")) {
279-
if (allow_sparse(object)) {
280-
x <- sparsevctrs::coerce_to_sparse_data_frame(x)
281-
} else {
282-
cli::cli_warn(c(
283-
"!" = "{.arg x} is a sparse matrix, but model doesn't accept that.",
284-
"i" = "Converted {.arg x} to data.frame."
285-
))
286-
x <- as.data.frame(x)
287-
}
288-
}
278+
x <- to_sparse_data_frame(x, object)
289279

290280
cl <- match.call(expand.dots = TRUE)
291281
eval_env <- rlang::env()

R/sparsevctrs.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
to_sparse_data_frame <- function(x, object) {
2+
if (methods::is(x, "sparseMatrix")) {
3+
if (allow_sparse(object)) {
4+
x <- sparsevctrs::coerce_to_sparse_data_frame(x)
5+
} else {
6+
cli::cli_warn(c(
7+
"!" = "{.arg x} is a sparse matrix, but model doesn't accept that.",
8+
"i" = "Converted {.arg x} to data.frame."
9+
))
10+
x <- as.data.frame(x)
11+
}
12+
}
13+
x
14+
}

0 commit comments

Comments
 (0)