Skip to content

Commit 6003c42

Browse files
committed
Enhance error handling in glex functions to check for non-numeric columns in data frames and update example usage in documentation.
1 parent 66e4418 commit 6003c42

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

R/glex.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ glex.rpf <- function(object, x, max_interaction = NULL, features = NULL, ...) {
8888
#' params = list(max_depth = 4, eta = .1),
8989
#' nrounds = 10, verbose = 0)
9090
#' glex(xg, x[27:32, ])
91-
#'
91+
#' glex(xg, mtcars[27:32, ])
92+
#'
9293
#' \dontrun{
9394
#' # Parallel execution
9495
#' doParallel::registerDoParallel()
@@ -97,8 +98,8 @@ glex.rpf <- function(object, x, max_interaction = NULL, features = NULL, ...) {
9798
#' }
9899
glex.xgb.Booster <- function(object, x, max_interaction = NULL, features = NULL, max_background_sample_size = NULL, weighting_method = "fastpd", ...) {
99100
if (!is.matrix(x)) {
100-
if (any(sapply(x, is.factor))) {
101-
stop("Input 'x' contains factor columns. Please convert them to numeric before calling glex, the conversion should match what was used to fit the model.")
101+
if (is.data.frame(x) && any(!sapply(x, is.numeric))) {
102+
stop("Input 'x' contains non-numeric columns. Please ensure all columns are numeric or convert them appropriately (e.g., using model.matrix) to match model training data before calling glex.")
102103
}
103104
x <- as.matrix(x)
104105
}
@@ -152,7 +153,8 @@ glex.xgb.Booster <- function(object, x, max_interaction = NULL, features = NULL,
152153
#' num.trees = 5, max.depth = 3,
153154
#' node.stats = TRUE)
154155
#' glex(rf, x[27:32, ])
155-
#'
156+
#' glex(rf, mtcars[27:32, ])
157+
#'
156158
#' \dontrun{
157159
#' # Parallel execution
158160
#' doParallel::registerDoParallel()
@@ -161,8 +163,8 @@ glex.xgb.Booster <- function(object, x, max_interaction = NULL, features = NULL,
161163
#' }
162164
glex.ranger <- function(object, x, max_interaction = NULL, features = NULL, max_background_sample_size = NULL, weighting_method = "fastpd", ...) {
163165
if (!is.matrix(x)) {
164-
if (any(sapply(x, is.factor))) {
165-
stop("Input 'x' contains factor columns. Please convert them to numeric before calling glex, the conversion should match what was used to fit the model.")
166+
if (is.data.frame(x) && any(!sapply(x, is.numeric))) {
167+
stop("Input 'x' contains non-numeric columns. Please ensure all columns are numeric or convert them appropriately (e.g., using model.matrix) to match model training data before calling glex.")
166168
}
167169
x <- as.matrix(x)
168170
}

0 commit comments

Comments
 (0)