Skip to content

Commit 3255172

Browse files
committed
initial loosening of x input format
1 parent cf62381 commit 3255172

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Imports:
3131
prettyunits,
3232
vctrs (>= 0.2.0)
3333
Roxygen: list(markdown = TRUE)
34-
RoxygenNote: 7.1.1
34+
RoxygenNote: 7.1.1.9000
3535
Suggests:
3636
testthat,
3737
knitr,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export(.x)
103103
export(.y)
104104
export(C5.0_train)
105105
export(add_rowindex)
106+
export(as_matrix)
106107
export(boost_tree)
107108
export(check_empty_ellipse)
108109
export(check_final_param)

R/arguments.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ make_xy_call <- function(object, target) {
192192
target,
193193
none = rlang::expr(x),
194194
data.frame = rlang::expr(as.data.frame(x)),
195-
matrix = rlang::expr(as.matrix(x)),
195+
matrix = rlang::expr(as_matrix(x)),
196196
rlang::abort(glue::glue("Invalid data type target: {target}."))
197197
)
198198

R/convert_data.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,21 @@ check_dup_names <- function(x, y) {
323323
)
324324
invisible(NULL)
325325
}
326+
327+
## -----------------------------------------------------------------------------
328+
329+
#' Convert data frame to matrix
330+
#'
331+
#' This is a substitute for `as.matrix()` that will convert a data frame to a
332+
#' ordinary matrix but leave other formats (such as a sparse matrix) alone.
333+
#' @param x A data frame, matrix, or sparse matrix.
334+
#' @return A matrix or sparse matrix.
335+
#' @export
336+
as_matrix <- function(x) {
337+
inher(x, c("data.frame", "matrix", "dgCMatrix"), cl = match.call())
338+
if (is.data.frame(x)) {
339+
x <- as.matrix(x)
340+
}
341+
# leave alone if matrix or sparse matrix
342+
x
343+
}

R/fit.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ check_interface <- function(formula, data, cl, model) {
342342
}
343343

344344
check_xy_interface <- function(x, y, cl, model) {
345-
inher(x, c("data.frame", "matrix"), cl)
345+
inher(x, c("data.frame", "matrix", "dgCMatrix"), cl)
346346

347347
# `y` can be a vector (which is not a class), or a factor (which is not a vector)
348348
if (!is.null(y) && !is.vector(y))
@@ -358,7 +358,7 @@ check_xy_interface <- function(x, y, cl, model) {
358358
)
359359

360360
# Determine the `fit()` interface
361-
matrix_interface <- !is.null(x) & !is.null(y) && is.matrix(x)
361+
matrix_interface <- !is.null(x) & !is.null(y) && (is.matrix(x) | inherits(x, "dgCMatrix"))
362362
df_interface <- !is.null(x) & !is.null(y) && is.data.frame(x)
363363

364364
if (inherits(model, "surv_reg") &&

man/as_matrix.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)