Skip to content

Commit 3627402

Browse files
committed
transition existing checks to type checkers
1 parent 4076a38 commit 3627402

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

R/Misc.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,20 @@ ggplotly2 <- function(x, ...) {
185185
})
186186
gg
187187
}
188+
189+
# ad-hoc check functions ----------------------------------------------------
190+
check_inherits <- function(x, cls, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) {
191+
if (!allow_null && is.null(x)) {
192+
return(invisible(x))
193+
}
194+
195+
if (!inherits(x, cls)) {
196+
stop_input_type(
197+
x,
198+
glue::glue("a {cls}"),
199+
arg = arg,
200+
call = call
201+
)
202+
}
203+
}
204+

R/organize_data.R

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,38 @@ organize_data.tune_results <-
7272
}
7373
# ------------------------------------------------------------------------------
7474

75-
new_shiny_data <- function(predictions, y_name, subclass, numeric_cols, factor_cols, x, default_config) {
76-
if (!inherits(predictions, "data.frame")) {
77-
cli::cli_abort("{.arg predictions} should be a data frame.")
78-
}
75+
new_shiny_data <- function(predictions,
76+
y_name,
77+
subclass,
78+
numeric_cols,
79+
factor_cols,
80+
x,
81+
default_config,
82+
call = caller_env()) {
83+
check_inherits(predictions, "data.frame", call = call)
7984
if (nrow(predictions) == 0) {
80-
cli::cli_abort("There should be at least one row of predictions.")
85+
cli::cli_abort(
86+
"There should be at least one row of predictions.",
87+
call = call
88+
)
8189
}
90+
check_string(y_name, allow_empty = FALSE, call = call)
8291
if (!(y_name %in% names(predictions))) {
83-
cli::cli_abort("{.field {y_name}} should be a column in the predictions.")
84-
}
85-
if (!is.character(y_name)) {
86-
cli::cli_abort("{.arg y_name} should be a character string.")
87-
}
88-
if (!is.character(numeric_cols)) {
89-
cli::cli_abort("{.arg numeric_cols} should be a character string.")
90-
}
91-
if (!is.character(factor_cols)) {
92-
cli::cli_abort("{.arg factor_cols} should be a character string.")
93-
}
94-
if (!is.character(default_config)) {
95-
cli::cli_abort("{.arg default_config} should be a character string.")
92+
cli::cli_abort(
93+
"{.field {y_name}} should be a column in the predictions.",
94+
call = call
95+
)
9696
}
97+
98+
check_inherits(numeric_cols, "character", call = call)
99+
check_inherits(factor_cols, "character", call = call)
100+
check_inherits(default_config, "character", call = call)
101+
97102
if (!(default_config %in% predictions$.config)) {
98-
cli::cli_abort("{.arg default_config} should be a character string in predictions.")
103+
cli::cli_abort(
104+
"{.arg default_config} should be a character string in predictions.",
105+
call = call
106+
)
99107
}
100108
res <- list(
101109
predictions = predictions,

0 commit comments

Comments
 (0)