@@ -80,7 +80,7 @@ set_in_env <- function(...) {
80
80
# ' @export
81
81
set_env_val <- function (name , value ) {
82
82
if (length(name ) != 1 || ! is.character(name )) {
83
- stop (" `name` should be a single character value." , call. = FALSE )
83
+ rlang :: abort (" `name` should be a single character value." )
84
84
}
85
85
mod_env <- get_model_env()
86
86
x <- list (value )
@@ -92,66 +92,60 @@ set_env_val <- function(name, value) {
92
92
93
93
check_eng_val <- function (eng ) {
94
94
if (rlang :: is_missing(eng ) || length(eng ) != 1 || ! is.character(eng ))
95
- stop(" Please supply a character string for an engine name (e.g. `'lm'`)" ,
96
- call. = FALSE )
95
+ rlang :: abort(" Please supply a character string for an engine name (e.g. `'lm'`)" )
97
96
invisible (NULL )
98
97
}
99
98
100
99
101
100
check_model_exists <- function (model ) {
102
101
if (rlang :: is_missing(model ) || length(model ) != 1 || ! is.character(model )) {
103
- stop(" Please supply a character string for a model name (e.g. `'linear_reg'`)" ,
104
- call. = FALSE )
102
+ rlang :: abort(" Please supply a character string for a model name (e.g. `'linear_reg'`)" )
105
103
}
106
104
107
105
current <- get_model_env()
108
106
109
107
if (! any(current $ models == model )) {
110
- stop( " Model `" , model , " ` has not been registered." , call. = FALSE )
108
+ rlang :: abort( glue :: glue( " Model `{ model} ` has not been registered." ) )
111
109
}
112
110
113
111
invisible (NULL )
114
112
}
115
113
116
114
check_model_doesnt_exist <- function (model ) {
117
115
if (rlang :: is_missing(model ) || length(model ) != 1 || ! is.character(model )) {
118
- stop(" Please supply a character string for a model name (e.g. `'linear_reg'`)" ,
119
- call. = FALSE )
116
+ rlang :: abort(" Please supply a character string for a model name (e.g. `'linear_reg'`)" )
120
117
}
121
118
122
119
current <- get_model_env()
123
120
124
121
if (any(current $ models == model )) {
125
- stop( " Model `" , model , " ` already exists" , call. = FALSE )
122
+ rlang :: abort( glue :: glue( " Model `{ model} ` already exists" ) )
126
123
}
127
124
128
125
invisible (NULL )
129
126
}
130
127
131
128
check_mode_val <- function (mode ) {
132
129
if (rlang :: is_missing(mode ) || length(mode ) != 1 || ! is.character(mode ))
133
- stop(" Please supply a character string for a mode (e.g. `'regression'`)" ,
134
- call. = FALSE )
130
+ rlang :: abort(" Please supply a character string for a mode (e.g. `'regression'`)." )
135
131
invisible (NULL )
136
132
}
137
133
138
134
check_engine_val <- function (eng ) {
139
135
if (rlang :: is_missing(eng ) || length(eng ) != 1 || ! is.character(eng ))
140
- stop(" Please supply a character string for an engine (e.g. `'lm'`)" ,
141
- call. = FALSE )
136
+ rlang :: abort(" Please supply a character string for an engine (e.g. `'lm'`)." )
142
137
invisible (NULL )
143
138
}
144
139
145
140
check_arg_val <- function (arg ) {
146
141
if (rlang :: is_missing(arg ) || length(arg ) != 1 || ! is.character(arg ))
147
- stop(" Please supply a character string for the argument" ,
148
- call. = FALSE )
142
+ rlang :: abort(" Please supply a character string for the argument." )
149
143
invisible (NULL )
150
144
}
151
145
152
146
check_submodels_val <- function (has_submodel ) {
153
147
if (! is.logical(has_submodel ) || length(has_submodel ) != 1 ) {
154
- stop (" The `submodels` argument should be a single logical." , call. = FALSE )
148
+ rlang :: abort (" The `submodels` argument should be a single logical." )
155
149
}
156
150
invisible (NULL )
157
151
}
@@ -165,104 +159,105 @@ check_func_val <- function(func) {
165
159
)
166
160
167
161
if (rlang :: is_missing(func ) || ! is.vector(func ))
168
- stop (msg , call. = FALSE )
162
+ rlang :: abort (msg )
169
163
170
164
nms <- sort(names(func ))
171
165
172
166
if (all(is.null(nms ))) {
173
- stop (msg , call. = FALSE )
167
+ rlang :: abort (msg )
174
168
}
175
169
176
170
if (length(func ) == 1 ) {
177
171
if (isTRUE(any(nms != " fun" ))) {
178
- stop (msg , call. = FALSE )
172
+ rlang :: abort (msg )
179
173
}
180
174
} else {
181
175
# check for extra names:
182
176
allow_nms <- c(" fun" , " pkg" , " range" , " trans" , " values" )
183
177
nm_check <- nms %in% c(" fun" , " pkg" , " range" , " trans" , " values" )
184
178
not_allowed <- nms [! (nms %in% allow_nms )]
185
179
if (length(not_allowed ) > 0 ) {
186
- stop (msg , call. = FALSE )
180
+ rlang :: abort (msg )
187
181
}
188
182
}
189
183
190
184
if (! is.character(func [[" fun" ]])) {
191
- stop (msg , call. = FALSE )
185
+ rlang :: abort (msg )
192
186
}
193
187
if (any(nms == " pkg" ) && ! is.character(func [[" pkg" ]])) {
194
- stop (msg , call. = FALSE )
188
+ rlang :: abort (msg )
195
189
}
196
190
197
191
invisible (NULL )
198
192
}
199
193
200
194
check_fit_info <- function (fit_obj ) {
201
195
if (is.null(fit_obj )) {
202
- stop (" The `fit` module cannot be NULL." , call. = FALSE )
196
+ rlang :: abort (" The `fit` module cannot be NULL." )
203
197
}
204
198
exp_nms <- c(" defaults" , " func" , " interface" , " protect" )
205
199
if (! isTRUE(all.equal(sort(names(fit_obj )), exp_nms ))) {
206
- stop(" The `fit` module should have elements: " ,
207
- paste0(" `" , exp_nms , " `" , collapse = " , " ),
208
- call. = FALSE )
200
+ rlang :: abort(
201
+ glue :: glue(" The `fit` module should have elements: " ,
202
+ glue :: glue_collapse(glue :: glue(" `{exp_nms}`" ), sep = " , " ))
203
+ )
209
204
}
210
205
211
206
check_interface_val(fit_obj $ interface )
212
207
check_func_val(fit_obj $ func )
213
208
214
209
if (! is.list(fit_obj $ defaults )) {
215
- stop (" The `defaults` element should be a list: " , call. = FALSE )
210
+ rlang :: abort (" The `defaults` element should be a list: " )
216
211
}
217
212
218
213
invisible (NULL )
219
214
}
220
215
221
216
check_pred_info <- function (pred_obj , type ) {
222
217
if (all(type != pred_types )) {
223
- stop(" The prediction type should be one of: " ,
224
- paste0(" '" , pred_types , " '" , collapse = " , " ),
225
- call. = FALSE )
218
+ rlang :: abort(
219
+ glue :: glue(" The prediction type should be one of: " ,
220
+ glue :: glue_collapse(glue :: glue(" '{pred_types}'" ), sep = " , " ))
221
+ )
226
222
}
227
223
228
224
exp_nms <- c(" args" , " func" , " post" , " pre" )
229
225
if (! isTRUE(all.equal(sort(names(pred_obj )), exp_nms ))) {
230
- stop(" The `predict` module should have elements: " ,
231
- paste0(" `" , exp_nms , " `" , collapse = " , " ),
232
- call. = FALSE )
226
+ rlang :: abort(
227
+ glue :: glue(" The `predict` module should have elements: " ,
228
+ glue :: glue_collapse(glue :: glue(" `{exp_nms}`" ), sep = " , " ))
229
+ )
233
230
}
234
231
235
232
if (! is.null(pred_obj $ pre ) & ! is.function(pred_obj $ pre )) {
236
- stop(" The `pre` module should be null or a function: " ,
237
- call. = FALSE )
233
+ rlang :: abort(" The `pre` module should be null or a function: " )
238
234
}
239
235
if (! is.null(pred_obj $ post ) & ! is.function(pred_obj $ post )) {
240
- stop(" The `post` module should be null or a function: " ,
241
- call. = FALSE )
236
+ rlang :: abort(" The `post` module should be null or a function: " )
242
237
}
243
238
244
239
check_func_val(pred_obj $ func )
245
240
246
241
if (! is.list(pred_obj $ args )) {
247
- stop (" The `args` element should be a list. " , call. = FALSE )
242
+ rlang :: abort (" The `args` element should be a list. " )
248
243
}
249
244
250
245
invisible (NULL )
251
246
}
252
247
253
248
check_pkg_val <- function (pkg ) {
254
249
if (rlang :: is_missing(pkg ) || length(pkg ) != 1 || ! is.character(pkg ))
255
- stop(" Please supply a single character vale for the package name" ,
256
- call. = FALSE )
250
+ rlang :: abort(" Please supply a single character vale for the package name." )
257
251
invisible (NULL )
258
252
}
259
253
260
254
check_interface_val <- function (x ) {
261
255
exp_interf <- c(" data.frame" , " formula" , " matrix" )
262
256
if (length(x ) != 1 || ! (x %in% exp_interf )) {
263
- stop(" The `interface` element should have a single value of : " ,
264
- paste0(" `" , exp_interf , " `" , collapse = " , " ),
265
- call. = FALSE )
257
+ rlang :: abort(
258
+ glue :: glue(" The `interface` element should have a single value of: " ,
259
+ glue :: glue_collapse(glue :: glue(" `{exp_interf}`" ), sep = " , " ))
260
+ )
266
261
}
267
262
invisible (NULL )
268
263
}
@@ -454,7 +449,7 @@ set_model_arg <- function(model, eng, parsnip, original, func, has_submodel) {
454
449
455
450
updated <- try(dplyr :: bind_rows(old_args , new_arg ), silent = TRUE )
456
451
if (inherits(updated , " try-error" )) {
457
- stop (" An error occured when adding the new argument." , call. = FALSE )
452
+ rlang :: abort (" An error occured when adding the new argument." )
458
453
}
459
454
460
455
updated <- vctrs :: vec_unique(updated )
@@ -484,8 +479,7 @@ set_dependency <- function(model, eng, pkg) {
484
479
dplyr :: filter(engine == eng ) %> %
485
480
nrow()
486
481
if (has_engine != 1 ) {
487
- stop(" The engine '" , eng , " ' has not been registered for model '" ,
488
- model , " '. " , call. = FALSE )
482
+ rlang :: abort(" The engine '{eng}' has not been registered for model '{model}'." )
489
483
}
490
484
491
485
existing_pkgs <-
@@ -518,7 +512,7 @@ get_dependency <- function(model) {
518
512
check_model_exists(model )
519
513
pkg_name <- paste0(model , " _pkgs" )
520
514
if (! any(pkg_name != rlang :: env_names(get_model_env()))) {
521
- stop( " ` " , model , " ` does not have a dependency list in parsnip." , call. = FALSE )
515
+ rlang :: abort( glue :: glue( " `{ model} ` does not have a dependency list in parsnip." ) )
522
516
}
523
517
rlang :: env_get(get_model_env(), pkg_name )
524
518
}
@@ -545,9 +539,8 @@ set_fit <- function(model, mode, eng, value) {
545
539
dplyr :: filter(engine == eng & mode == !! mode ) %> %
546
540
nrow()
547
541
if (has_engine != 1 ) {
548
- stop(" The combination of engine '" , eng , " ' and mode '" ,
549
- mode , " ' has not been registered for model '" ,
550
- model , " '. " , call. = FALSE )
542
+ rlang :: abort(glue :: glue(" The combination of '{eng}' and mode '{mode}' has not" ,
543
+ " been registered for model '{model}'." ))
551
544
}
552
545
553
546
has_fit <-
@@ -556,9 +549,8 @@ set_fit <- function(model, mode, eng, value) {
556
549
nrow()
557
550
558
551
if (has_fit > 0 ) {
559
- stop(" The combination of engine '" , eng , " ' and mode '" ,
560
- mode , " ' already has a fit component for model '" ,
561
- model , " '. " , call. = FALSE )
552
+ rlang :: abort(glue :: glue(" The combination of '{eng}' and mode '{mode}'" ,
553
+ " already has a fit component for model '{model}'." ))
562
554
}
563
555
564
556
new_fit <-
@@ -570,7 +562,7 @@ set_fit <- function(model, mode, eng, value) {
570
562
571
563
updated <- try(dplyr :: bind_rows(old_fits , new_fit ), silent = TRUE )
572
564
if (inherits(updated , " try-error" )) {
573
- stop (" An error occured when adding the new fit module" , call. = FALSE )
565
+ rlang :: abort (" An error occured when adding the new fit module. " )
574
566
}
575
567
576
568
set_env_val(
@@ -588,7 +580,7 @@ get_fit <- function(model) {
588
580
check_model_exists(model )
589
581
fit_name <- paste0(model , " _fit" )
590
582
if (! any(fit_name != rlang :: env_names(get_model_env()))) {
591
- stop( " ` " , model , " ` does not have a `fit` method in parsnip." , call. = FALSE )
583
+ rlang :: abort( glue :: glue( " `{ model} ` does not have a `fit` method in parsnip." ) )
592
584
}
593
585
rlang :: env_get(get_model_env(), fit_name )
594
586
}
@@ -614,20 +606,18 @@ set_pred <- function(model, mode, eng, type, value) {
614
606
dplyr :: filter(engine == eng & mode == !! mode ) %> %
615
607
nrow()
616
608
if (has_engine != 1 ) {
617
- stop(" The combination of engine '" , eng , " ' and mode '" ,
618
- mode , " ' has not been registered for model '" ,
619
- model , " '. " , call. = FALSE )
609
+ rlang :: abort(glue :: glue(" The combination of '{eng}' and mode '{mode}'" ,
610
+ " has not been registered for model '{model}'." ))
620
611
}
621
612
622
613
has_pred <-
623
614
old_fits %> %
624
615
dplyr :: filter(engine == eng & mode == !! mode & type == !! type ) %> %
625
616
nrow()
626
617
if (has_pred > 0 ) {
627
- stop(" The combination of engine '" , eng , " ', mode '" ,
628
- mode , " ', and type '" , type ,
629
- " ' already has a prediction component for model '" ,
630
- model , " '. " , call. = FALSE )
618
+ rlang :: abort(glue :: glue(" The combination of '{eng}', mode '{mode}', " ,
619
+ " and type '{type}' already has a prediction component" ,
620
+ " for model '{model}'." ))
631
621
}
632
622
633
623
new_fit <-
@@ -640,7 +630,7 @@ set_pred <- function(model, mode, eng, type, value) {
640
630
641
631
updated <- try(dplyr :: bind_rows(old_fits , new_fit ), silent = TRUE )
642
632
if (inherits(updated , " try-error" )) {
643
- stop (" An error occured when adding the new fit module" , call. = FALSE )
633
+ rlang :: abort (" An error occured when adding the new fit module. " )
644
634
}
645
635
646
636
set_env_val(paste0(model , " _predict" ), updated )
@@ -655,12 +645,11 @@ get_pred_type <- function(model, type) {
655
645
check_model_exists(model )
656
646
pred_name <- paste0(model , " _predict" )
657
647
if (! any(pred_name != rlang :: env_names(get_model_env()))) {
658
- stop( " ` " , model , " ` does not have any `pred` methods in parsnip." , call. = FALSE )
648
+ rlang :: abort( glue :: glue( " `{ model} ` does not have any `pred` methods in parsnip." ) )
659
649
}
660
650
all_preds <- rlang :: env_get(get_model_env(), pred_name )
661
651
if (! any(all_preds $ type == type )) {
662
- stop(" `" , model , " ` does not have any `" , type ,
663
- " ` prediction methods in parsnip." , call. = FALSE )
652
+ rlang :: abort(glue :: glue(" `{model}` does not have any prediction methods in parsnip." ))
664
653
}
665
654
dplyr :: filter(all_preds , type == !! type )
666
655
}
@@ -765,7 +754,7 @@ show_model_info <- function(model) {
765
754
# ' @export
766
755
pred_value_template <- function (pre = NULL , post = NULL , func , ... ) {
767
756
if (rlang :: is_missing(func )) {
768
- stop (" Please supply a value to `func`. See `?set_pred`." , call. = FALSE )
757
+ rlang :: abort (" Please supply a value to `func`. See `?set_pred`." )
769
758
}
770
759
list (pre = pre , post = post , func = func , args = list (... ))
771
760
}
0 commit comments