Skip to content

Commit

Permalink
initial loosening of x input format
Browse files Browse the repository at this point in the history
  • Loading branch information
topepo committed Sep 29, 2020
1 parent cf62381 commit 3255172
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Imports:
prettyunits,
vctrs (>= 0.2.0)
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.1.1.9000
Suggests:
testthat,
knitr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export(.x)
export(.y)
export(C5.0_train)
export(add_rowindex)
export(as_matrix)
export(boost_tree)
export(check_empty_ellipse)
export(check_final_param)
Expand Down
2 changes: 1 addition & 1 deletion R/arguments.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ make_xy_call <- function(object, target) {
target,
none = rlang::expr(x),
data.frame = rlang::expr(as.data.frame(x)),
matrix = rlang::expr(as.matrix(x)),
matrix = rlang::expr(as_matrix(x)),
rlang::abort(glue::glue("Invalid data type target: {target}."))
)

Expand Down
18 changes: 18 additions & 0 deletions R/convert_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,21 @@ check_dup_names <- function(x, y) {
)
invisible(NULL)
}

## -----------------------------------------------------------------------------

#' Convert data frame to matrix
#'
#' This is a substitute for `as.matrix()` that will convert a data frame to a
#' ordinary matrix but leave other formats (such as a sparse matrix) alone.
#' @param x A data frame, matrix, or sparse matrix.
#' @return A matrix or sparse matrix.
#' @export
as_matrix <- function(x) {
inher(x, c("data.frame", "matrix", "dgCMatrix"), cl = match.call())
if (is.data.frame(x)) {
x <- as.matrix(x)
}
# leave alone if matrix or sparse matrix
x
}
4 changes: 2 additions & 2 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ check_interface <- function(formula, data, cl, model) {
}

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

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

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

if (inherits(model, "surv_reg") &&
Expand Down
18 changes: 18 additions & 0 deletions man/as_matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3255172

Please sign in to comment.