Skip to content

Commit 2223d30

Browse files
Change stop() to rlang::abort()
1 parent b056963 commit 2223d30

30 files changed

+298
-280
lines changed

R/aaa_models.R

Lines changed: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ set_in_env <- function(...) {
8080
#' @export
8181
set_env_val <- function(name, value) {
8282
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.")
8484
}
8585
mod_env <- get_model_env()
8686
x <- list(value)
@@ -92,66 +92,60 @@ set_env_val <- function(name, value) {
9292

9393
check_eng_val <- function(eng) {
9494
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'`)")
9796
invisible(NULL)
9897
}
9998

10099

101100
check_model_exists <- function(model) {
102101
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'`)")
105103
}
106104

107105
current <- get_model_env()
108106

109107
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."))
111109
}
112110

113111
invisible(NULL)
114112
}
115113

116114
check_model_doesnt_exist <- function(model) {
117115
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'`)")
120117
}
121118

122119
current <- get_model_env()
123120

124121
if (any(current$models == model)) {
125-
stop("Model `", model, "` already exists", call. = FALSE)
122+
rlang::abort(glue::glue("Model `{model}` already exists"))
126123
}
127124

128125
invisible(NULL)
129126
}
130127

131128
check_mode_val <- function(mode) {
132129
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'`).")
135131
invisible(NULL)
136132
}
137133

138134
check_engine_val <- function(eng) {
139135
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'`).")
142137
invisible(NULL)
143138
}
144139

145140
check_arg_val <- function(arg) {
146141
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.")
149143
invisible(NULL)
150144
}
151145

152146
check_submodels_val <- function(has_submodel) {
153147
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.")
155149
}
156150
invisible(NULL)
157151
}
@@ -165,104 +159,105 @@ check_func_val <- function(func) {
165159
)
166160

167161
if (rlang::is_missing(func) || !is.vector(func))
168-
stop(msg, call. = FALSE)
162+
rlang::abort(msg)
169163

170164
nms <- sort(names(func))
171165

172166
if (all(is.null(nms))) {
173-
stop(msg, call. = FALSE)
167+
rlang::abort(msg)
174168
}
175169

176170
if (length(func) == 1) {
177171
if (isTRUE(any(nms != "fun"))) {
178-
stop(msg, call. = FALSE)
172+
rlang::abort(msg)
179173
}
180174
} else {
181175
# check for extra names:
182176
allow_nms <- c("fun", "pkg", "range", "trans", "values")
183177
nm_check <- nms %in% c("fun", "pkg", "range", "trans", "values")
184178
not_allowed <- nms[!(nms %in% allow_nms)]
185179
if (length(not_allowed) > 0) {
186-
stop(msg, call. = FALSE)
180+
rlang::abort(msg)
187181
}
188182
}
189183

190184
if (!is.character(func[["fun"]])) {
191-
stop(msg, call. = FALSE)
185+
rlang::abort(msg)
192186
}
193187
if (any(nms == "pkg") && !is.character(func[["pkg"]])) {
194-
stop(msg, call. = FALSE)
188+
rlang::abort(msg)
195189
}
196190

197191
invisible(NULL)
198192
}
199193

200194
check_fit_info <- function(fit_obj) {
201195
if (is.null(fit_obj)) {
202-
stop("The `fit` module cannot be NULL.", call. = FALSE)
196+
rlang::abort("The `fit` module cannot be NULL.")
203197
}
204198
exp_nms <- c("defaults", "func", "interface", "protect")
205199
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+
)
209204
}
210205

211206
check_interface_val(fit_obj$interface)
212207
check_func_val(fit_obj$func)
213208

214209
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: ")
216211
}
217212

218213
invisible(NULL)
219214
}
220215

221216
check_pred_info <- function(pred_obj, type) {
222217
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+
)
226222
}
227223

228224
exp_nms <- c("args", "func", "post", "pre")
229225
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+
)
233230
}
234231

235232
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: ")
238234
}
239235
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: ")
242237
}
243238

244239
check_func_val(pred_obj$func)
245240

246241
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. ")
248243
}
249244

250245
invisible(NULL)
251246
}
252247

253248
check_pkg_val <- function(pkg) {
254249
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.")
257251
invisible(NULL)
258252
}
259253

260254
check_interface_val <- function(x) {
261255
exp_interf <- c("data.frame", "formula", "matrix")
262256
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+
)
266261
}
267262
invisible(NULL)
268263
}
@@ -454,7 +449,7 @@ set_model_arg <- function(model, eng, parsnip, original, func, has_submodel) {
454449

455450
updated <- try(dplyr::bind_rows(old_args, new_arg), silent = TRUE)
456451
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.")
458453
}
459454

460455
updated <- vctrs::vec_unique(updated)
@@ -484,8 +479,7 @@ set_dependency <- function(model, eng, pkg) {
484479
dplyr::filter(engine == eng) %>%
485480
nrow()
486481
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}'.")
489483
}
490484

491485
existing_pkgs <-
@@ -518,7 +512,7 @@ get_dependency <- function(model) {
518512
check_model_exists(model)
519513
pkg_name <- paste0(model, "_pkgs")
520514
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."))
522516
}
523517
rlang::env_get(get_model_env(), pkg_name)
524518
}
@@ -545,9 +539,8 @@ set_fit <- function(model, mode, eng, value) {
545539
dplyr::filter(engine == eng & mode == !!mode) %>%
546540
nrow()
547541
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}'."))
551544
}
552545

553546
has_fit <-
@@ -556,9 +549,8 @@ set_fit <- function(model, mode, eng, value) {
556549
nrow()
557550

558551
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}'."))
562554
}
563555

564556
new_fit <-
@@ -570,7 +562,7 @@ set_fit <- function(model, mode, eng, value) {
570562

571563
updated <- try(dplyr::bind_rows(old_fits, new_fit), silent = TRUE)
572564
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.")
574566
}
575567

576568
set_env_val(
@@ -588,7 +580,7 @@ get_fit <- function(model) {
588580
check_model_exists(model)
589581
fit_name <- paste0(model, "_fit")
590582
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."))
592584
}
593585
rlang::env_get(get_model_env(), fit_name)
594586
}
@@ -614,20 +606,18 @@ set_pred <- function(model, mode, eng, type, value) {
614606
dplyr::filter(engine == eng & mode == !!mode) %>%
615607
nrow()
616608
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}'."))
620611
}
621612

622613
has_pred <-
623614
old_fits %>%
624615
dplyr::filter(engine == eng & mode == !!mode & type == !!type) %>%
625616
nrow()
626617
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}'."))
631621
}
632622

633623
new_fit <-
@@ -640,7 +630,7 @@ set_pred <- function(model, mode, eng, type, value) {
640630

641631
updated <- try(dplyr::bind_rows(old_fits, new_fit), silent = TRUE)
642632
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.")
644634
}
645635

646636
set_env_val(paste0(model, "_predict"), updated)
@@ -655,12 +645,11 @@ get_pred_type <- function(model, type) {
655645
check_model_exists(model)
656646
pred_name <- paste0(model, "_predict")
657647
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."))
659649
}
660650
all_preds <- rlang::env_get(get_model_env(), pred_name)
661651
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."))
664653
}
665654
dplyr::filter(all_preds, type == !!type)
666655
}
@@ -765,7 +754,7 @@ show_model_info <- function(model) {
765754
#' @export
766755
pred_value_template <- function(pre = NULL, post = NULL, func, ...) {
767756
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`.")
769758
}
770759
list(pre = pre, post = post, func = func, args = list(...))
771760
}

R/aaa_multi_predict.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ multi_predict <- function(object, ...) {
2828
#' @export
2929
#' @rdname multi_predict
3030
multi_predict.default <- function(object, ...)
31-
stop("No `multi_predict` method exists for objects with classes ",
32-
paste0("'", class(), "'", collapse = ", "), call. = FALSE)
31+
rlang::abort(
32+
glue::glue(
33+
"No `multi_predict` method exists for objects with classes ",
34+
glue::glue_collapse(glue::glue("'{class()}'"), sep = ", ")
35+
)
36+
)
3337

3438
#' @export
3539
predict.model_spec <- function(object, ...) {
36-
stop("You must use `fit()` on your model specification before you can use `predict()`.", call. = FALSE)
40+
rlang::abort("You must use `fit()` on your model specification before you can use `predict()`.")
3741
}
3842

3943
#' Tools for models that predict on sub-models

R/adds.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @importFrom dplyr mutate
99
add_rowindex <- function(x) {
1010
if (!is.data.frame(x)) {
11-
stop("`x` should be a data frame.", call. = FALSE)
11+
rlang::abort("`x` should be a data frame.")
1212
}
1313
if (nrow(x) > 0) {
1414
x <- dplyr::mutate(x, .row = 1:nrow(x))

0 commit comments

Comments
 (0)