Skip to content

Commit 9cff096

Browse files
topepoDavisVaughan
andauthored
check for non-numeric cols when converting to matrix (#594)
* check for non-numeric cols when converting to matrix * Apply suggestions from code review Co-authored-by: Davis Vaughan <[email protected]> * changes based on review * try to fix xgboost install on win * another CXX14 attempt * remove append arg * yet another CXX14 attempt * proper separators * Makevars.win and no append * initialize file * create directory * typo fix * create .R path * try devel xgboost instead * remove remotes Co-authored-by: Davis Vaughan <[email protected]>
1 parent ae5b810 commit 9cff096

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ jobs:
3737
env:
3838
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
3939
R_KEEP_PKG_SOURCE: yes
40+
CXX14: g++
41+
CXX14STD: -std=c++1y
42+
CXX14FLAGS: -Wall -g -02
4043

4144
steps:
4245
- uses: actions/checkout@v2

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
* The list column produced when creating survival probability predictions is now always called `.pred` (with `.pred_survival` being used inside of the list column).
1414

15+
## Other Changes
16+
17+
* When the xy interface is used and the underlying model expects to use a matrix, a better warning is issued when predictors contain non-numeric columns (including dates).
18+
1519
# parsnip 0.1.7
1620

1721
## Model Specification Changes

R/convert_data.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ check_dup_names <- function(x, y) {
356356
maybe_matrix <- function(x) {
357357
inher(x, c("data.frame", "matrix", "dgCMatrix"), cl = match.call())
358358
if (is.data.frame(x)) {
359+
non_num_cols <- vapply(x, function(x) !is.numeric(x), logical(1))
360+
if (any(non_num_cols)) {
361+
non_num_cols <- names(non_num_cols)[non_num_cols]
362+
non_num_cols <- glue::glue_collapse(glue::single_quote(non_num_cols), sep = ", ")
363+
msg <- glue::glue("Some columns are non-numeric. The data cannot be ",
364+
"converted to numeric matrix: {non_num_cols}.")
365+
rlang::abort(msg)
366+
}
359367
x <- as.matrix(x)
360368
}
361369
# leave alone if matrix or sparse matrix

tests/testthat/test_convert_data.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,16 @@ test_that("convert to matrix", {
617617
inherits(parsnip::maybe_matrix(Matrix::Matrix(as.matrix(mtcars), sparse = TRUE)),
618618
"dgCMatrix")
619619
)
620+
621+
data(ames, package = "modeldata")
622+
expect_error(
623+
parsnip::maybe_matrix(ames[, c("Year_Built", "Neighborhood")]),
624+
"Some columns are non-numeric. The data cannot be converted to numeric matrix: 'Neighborhood'."
625+
)
626+
# Also for date columns
627+
data(Chicago, package = "modeldata")
628+
expect_error(
629+
parsnip::maybe_matrix(Chicago[, c("ridership", "date")]),
630+
"Some columns are non-numeric. The data cannot be converted to numeric matrix: 'date'."
631+
)
620632
})

0 commit comments

Comments
 (0)