Skip to content

Commit 08817e7

Browse files
committed
cleaned up check_mod_val
1 parent 073370a commit 08817e7

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

R/aaa_models.R

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,32 @@ check_eng_val <- function(eng) {
9797
invisible(NULL)
9898
}
9999

100-
check_mod_val <- function(model, new = FALSE, existence = FALSE) {
101-
if (rlang::is_missing(model) || length(model) != 1)
100+
101+
check_model_exists <- function(model) {
102+
if (rlang::is_missing(model) || length(model) != 1) {
102103
stop("Please supply a character string for a model name (e.g. `'linear_reg'`)",
103104
call. = FALSE)
105+
}
106+
107+
current <- get_model_env()
104108

105-
if (new | existence) {
106-
current <- get_model_env()
109+
if (!any(current$models == model)) {
110+
stop("Model `", model, "` has not been registered.", call. = FALSE)
107111
}
108112

109-
if (new) {
110-
if (any(current$models == model)) {
111-
stop("Model `", model, "` already exists", call. = FALSE)
112-
}
113+
invisible(NULL)
114+
}
115+
116+
check_model_doesnt_exist <- function(model) {
117+
if (rlang::is_missing(model) || length(model) != 1) {
118+
stop("Please supply a character string for a model name (e.g. `'linear_reg'`)",
119+
call. = FALSE)
113120
}
114121

115-
if (existence) {
116-
current <- get_model_env()
117-
if (!any(current$models == model)) {
118-
stop("Model `", model, "` has not been registered.", call. = FALSE)
119-
}
122+
current <- get_model_env()
123+
124+
if (any(current$models == model)) {
125+
stop("Model `", model, "` already exists", call. = FALSE)
120126
}
121127

122128
invisible(NULL)
@@ -265,10 +271,6 @@ check_interface_val <- function(x) {
265271
#'
266272
#' @param model A single character string for the model type (e.g.
267273
#' `"rand_forest"`, etc).
268-
#' @param new A single logical to check to see if the model that you are check
269-
#' has not already been registered.
270-
#' @param existence A single logical to check to see if the model has already
271-
#' been registered.
272274
#' @param mode A single character string for the model mode (e.g. "regression").
273275
#' @param eng A single character string for the model engine.
274276
#' @param arg A single character string for the model argument name.
@@ -312,6 +314,11 @@ check_interface_val <- function(x) {
312314
#' models to that environment as well as helper functions that can
313315
#' be used to makes sure that the model data is in the right
314316
#' format.
317+
#'
318+
#' `check_model_exists()` checks the model value and ensures that the model has
319+
#' already been registered. `check_model_doesnt_exist()` checks the model value
320+
#' and also checks to see if it is novel in the environment.
321+
#'
315322
#' @references "Making a parsnip model from scratch"
316323
#' \url{https://tidymodels.github.io/parsnip/articles/articles/Scratch.html}
317324
#' @examples
@@ -322,7 +329,7 @@ check_interface_val <- function(x) {
322329
#' @keywords internal
323330
#' @export
324331
set_new_model <- function(model) {
325-
check_mod_val(model, new = TRUE)
332+
check_model_doesnt_exist(model)
326333

327334
current <- get_model_env()
328335

@@ -370,7 +377,7 @@ set_new_model <- function(model) {
370377
#' @keywords internal
371378
#' @export
372379
set_model_mode <- function(model, mode) {
373-
check_mod_val(model, existence = TRUE)
380+
check_model_exists(model)
374381
check_mode_val(mode)
375382

376383
current <- get_model_env()
@@ -392,7 +399,7 @@ set_model_mode <- function(model, mode) {
392399
#' @keywords internal
393400
#' @export
394401
set_model_engine <- function(model, mode, eng) {
395-
check_mod_val(model, existence = TRUE)
402+
check_model_exists(model)
396403
check_mode_val(mode)
397404
check_eng_val(eng)
398405
check_mode_val(eng)
@@ -419,7 +426,7 @@ set_model_engine <- function(model, mode, eng) {
419426
#' @keywords internal
420427
#' @export
421428
set_model_arg <- function(model, eng, parsnip, original, func, has_submodel) {
422-
check_mod_val(model, existence = TRUE)
429+
check_model_exists(model)
423430
check_eng_val(eng)
424431
check_arg_val(parsnip)
425432
check_arg_val(original)
@@ -456,7 +463,7 @@ set_model_arg <- function(model, eng, parsnip, original, func, has_submodel) {
456463
#' @keywords internal
457464
#' @export
458465
set_dependency <- function(model, eng, pkg) {
459-
check_mod_val(model, existence = TRUE)
466+
check_model_exists(model)
460467
check_eng_val(eng)
461468
check_pkg_val(pkg)
462469

@@ -501,7 +508,7 @@ set_dependency <- function(model, eng, pkg) {
501508
#' @keywords internal
502509
#' @export
503510
get_dependency <- function(model) {
504-
check_mod_val(model, existence = TRUE)
511+
check_model_exists(model)
505512
pkg_name <- paste0(model, "_pkgs")
506513
if (!any(pkg_name != rlang::env_names(get_model_env()))) {
507514
stop("`", model, "` does not have a dependency list in parsnip.", call. = FALSE)
@@ -516,7 +523,7 @@ get_dependency <- function(model) {
516523
#' @keywords internal
517524
#' @export
518525
set_fit <- function(model, mode, eng, value) {
519-
check_mod_val(model, existence = TRUE)
526+
check_model_exists(model)
520527
check_eng_val(eng)
521528
check_mode_val(mode)
522529
check_engine_val(eng)
@@ -571,7 +578,7 @@ set_fit <- function(model, mode, eng, value) {
571578
#' @keywords internal
572579
#' @export
573580
get_fit <- function(model) {
574-
check_mod_val(model, existence = TRUE)
581+
check_model_exists(model)
575582
fit_name <- paste0(model, "_fit")
576583
if (!any(fit_name != rlang::env_names(get_model_env()))) {
577584
stop("`", model, "` does not have a `fit` method in parsnip.", call. = FALSE)
@@ -585,7 +592,7 @@ get_fit <- function(model) {
585592
#' @keywords internal
586593
#' @export
587594
set_pred <- function(model, mode, eng, type, value) {
588-
check_mod_val(model, existence = TRUE)
595+
check_model_exists(model)
589596
check_eng_val(eng)
590597
check_mode_val(mode)
591598
check_engine_val(eng)
@@ -638,7 +645,7 @@ set_pred <- function(model, mode, eng, type, value) {
638645
#' @keywords internal
639646
#' @export
640647
get_pred_type <- function(model, type) {
641-
check_mod_val(model, existence = TRUE)
648+
check_model_exists(model)
642649
pred_name <- paste0(model, "_predict")
643650
if (!any(pred_name != rlang::env_names(get_model_env()))) {
644651
stop("`", model, "` does not have any `pred` methods in parsnip.", call. = FALSE)
@@ -657,7 +664,7 @@ get_pred_type <- function(model, type) {
657664
#' @keywords internal
658665
#' @export
659666
show_model_info <- function(model) {
660-
check_mod_val(model, existence = TRUE)
667+
check_model_exists(model)
661668
current <- get_model_env()
662669

663670
cat("Information for `", model, "`\n", sep = "")

man/set_new_model.Rd

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)