55
55
# ' a "reverse Kaplan-Meier" curve that models the probability of censoring. This
56
56
# ' may be used later to compute inverse probability censoring weights for
57
57
# ' performance measures.
58
- # '
58
+ # '
59
59
# ' Sparse data is supported, with the use of the `x` argument in `fit_xy()`. See
60
- # ' `allow_sparse_x` column of [parsnip::get_encoding()] for sparse input
60
+ # ' `allow_sparse_x` column of [parsnip::get_encoding()] for sparse input
61
61
# ' compatibility.
62
- # '
62
+ # '
63
63
# ' @examplesIf !parsnip:::is_cran_check()
64
64
# ' # Although `glm()` only has a formula interface, different
65
65
# ' # methods for specifying the model can be used
@@ -121,21 +121,17 @@ fit.model_spec <-
121
121
control <- condense_control(control , control_parsnip())
122
122
check_case_weights(case_weights , object )
123
123
124
- if (! inherits(formula , " formula" )) {
125
- msg <- " The {.arg formula} argument must be a formula, but it is a \\
126
- {.cls {class(formula)[1]}}."
127
-
128
- if (inherits(formula , " recipe" )) {
129
- msg <-
130
- c(
131
- msg ,
132
- " i" = " To fit a model with a recipe preprocessor, please use a \\
124
+ if (inherits(formula , " recipe" )) {
125
+ cli :: cli_abort(
126
+ c(
127
+ " The {.arg formula} argument must be a formula." ,
128
+ " i" = " To fit a model with a recipe preprocessor, please use a \\
133
129
{.help [workflow](workflows::workflow)}."
134
- )
135
- }
136
-
137
- cli :: cli_abort(msg )
130
+ )
131
+ )
138
132
}
133
+ check_formula(formula )
134
+
139
135
140
136
if (is_sparse_matrix(data )) {
141
137
data <- sparsevctrs :: coerce_to_sparse_tibble(data )
@@ -179,7 +175,7 @@ fit.model_spec <-
179
175
eval_env $ weights <- wts
180
176
181
177
data <- materialize_sparse_tibble(data , object , " data" )
182
-
178
+
183
179
fit_interface <-
184
180
check_interface(eval_env $ formula , eval_env $ data , cl , object )
185
181
@@ -297,10 +293,11 @@ fit_xy.model_spec <-
297
293
# TODO case weights: pass in eval_env not individual elements
298
294
fit_interface <- check_xy_interface(eval_env $ x , eval_env $ y , cl , object )
299
295
300
- if (object $ engine == " spark" )
296
+ if (object $ engine == " spark" ) {
301
297
cli :: cli_abort(
302
- " spark objects can only be used with the formula interface to {.fn fit} with a spark data object."
298
+ " spark objects can only be used with the formula interface to {.fn fit} with a spark data object."
303
299
)
300
+ }
304
301
305
302
# populate `method` with the details for this model type
306
303
object <- add_methods(object , engine = object $ engine )
@@ -373,59 +370,47 @@ eval_mod <- function(e, capture = FALSE, catch = FALSE, envir = NULL, ...) {
373
370
374
371
# ------------------------------------------------------------------------------
375
372
376
- inher <- function (x , cls , cl ) {
377
- if (! is.null(x ) && ! inherits(x , cls )) {
378
-
379
- call <- match.call()
380
- obj <- deparse(call [[" x" ]])
381
-
382
- if (length(cls ) > 1 )
383
- cli :: cli_abort(
384
- " {.arg {obj}} should be one of the following classes: {.cls {cls}}." )
385
-
386
- else
387
- cli :: cli_abort(" {.arg {obj}} should be a {.cls {cls}} object" )
388
- }
389
- invisible (x )
390
- }
391
-
392
- # ------------------------------------------------------------------------------
393
-
394
- check_interface <- function (formula , data , cl , model ) {
395
- inher(formula , " formula" , cl )
396
- inher(data , c(" data.frame" , " dgCMatrix" , " tbl_spark" ), cl )
373
+ check_interface <- function (formula , data , cl , model , call = caller_env()) {
374
+ check_formula(formula , call = call )
375
+ check_inherits(data , c(" data.frame" , " dgCMatrix" , " tbl_spark" ), call = call )
397
376
398
377
# Determine the `fit()` interface
399
378
form_interface <- ! is.null(formula ) & ! is.null(data )
400
379
401
380
if (form_interface )
402
381
return (" formula" )
403
- cli :: cli_abort(" Error when checking the interface." )
382
+ cli :: cli_abort(" Error when checking the interface." , call = call )
404
383
}
405
384
406
- check_xy_interface <- function (x , y , cl , model ) {
385
+ check_xy_interface <- function (x , y , cl , model , call = caller_env() ) {
407
386
408
387
sparse_ok <- allow_sparse(model )
409
388
sparse_x <- inherits(x , " dgCMatrix" )
410
389
if (! sparse_ok & sparse_x ) {
411
- cli :: cli_abort(" Sparse matrices not supported by this model/engine combination." )
390
+ cli :: cli_abort(
391
+ " Sparse matrices not supported by this model/engine combination." ,
392
+ call = call
393
+ )
412
394
}
413
395
414
396
if (sparse_ok ) {
415
- inher (x , c(" data.frame" , " matrix" , " dgCMatrix" ), cl )
397
+ check_inherits (x , c(" data.frame" , " matrix" , " dgCMatrix" ), call = call )
416
398
} else {
417
- inher (x , c(" data.frame" , " matrix" ), cl )
399
+ check_inherits (x , c(" data.frame" , " matrix" ), call = call )
418
400
}
419
401
420
- if (! is.null(y ) && ! is.atomic(y ))
421
- inher(y , c(" data.frame" , " matrix" ), cl )
402
+ if (! is.null(y ) && ! is.atomic(y )) {
403
+ check_inherits(y , c(" data.frame" , " matrix" ), call = call )
404
+ }
422
405
423
406
# rule out spark data sets that don't use the formula interface
424
- if (inherits(x , " tbl_spark" ) | inherits(y , " tbl_spark" ))
407
+ if (inherits(x , " tbl_spark" ) | inherits(y , " tbl_spark" )) {
425
408
cli :: cli_abort(
426
- " spark objects can only be used with the formula interface via {.fn fit} with a spark data object."
427
- )
428
-
409
+ " spark objects can only be used with the formula interface via
410
+ {.fn fit} with a spark data object." ,
411
+ call = call
412
+ )
413
+ }
429
414
430
415
if (sparse_ok ) {
431
416
matrix_interface <- ! is.null(x ) && ! is.null(y ) && (is.matrix(x ) | sparse_x )
@@ -444,7 +429,7 @@ check_xy_interface <- function(x, y, cl, model) {
444
429
445
430
check_outcome(y , model )
446
431
447
- cli :: cli_abort(" Error when checking the interface" )
432
+ cli :: cli_abort(" Error when checking the interface. " , call = call )
448
433
}
449
434
450
435
allow_sparse <- function (x ) {
0 commit comments