Skip to content

Check valid spec mode #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 3, 2021
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

* Re-licensed package from GPL-2 to MIT. See [consent from copyright holders here](https://github.com/tidymodels/parsnip/issues/462).

* `set_mode()` now checks if `mode` is compatible with the model class, similar to `new_model_spec()` (@jtlandis, #467).

* Re-organized model documentation for `update` methods (#479).


# parsnip 0.1.5

* An RStudio add-in is available that makes writing multiple `parsnip` model specifications to the source window. It can be accessed via the IDE addin menus or by calling `parsnip_addin()`.
Expand Down
13 changes: 13 additions & 0 deletions R/aaa_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ check_mode_val <- function(mode) {
invisible(NULL)
}

# check if class and mode are compatible
check_spec_mode_val <- function(cls, mode) {
spec_modes <- rlang::env_get(get_model_env(), paste0(cls, "_modes"))
if (!(mode %in% spec_modes))
rlang::abort(
glue::glue(
"`mode` should be one of: ",
glue::glue_collapse(glue::glue("'{spec_modes}'"), sep = ", ")
)
)
invisible(NULL)
}

check_engine_val <- function(eng) {
if (rlang::is_missing(eng) || length(eng) != 1 || !is.character(eng))
rlang::abort("Please supply a character string for an engine (e.g. `'lm'`).")
Expand Down
1 change: 1 addition & 0 deletions R/arguments.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ set_mode <- function(object, mode) {
)
)
}
check_spec_mode_val(class(object)[1], mode)
object$mode <- mode
object
}
Expand Down
10 changes: 2 additions & 8 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,8 @@ update_dot_check <- function(...) {
#' @keywords internal
#' @rdname add_on_exports
new_model_spec <- function(cls, args, eng_args, mode, method, engine) {
spec_modes <- rlang::env_get(get_model_env(), paste0(cls, "_modes"))
if (!(mode %in% spec_modes))
rlang::abort(
glue::glue(
"`mode` should be one of: ",
glue::glue_collapse(glue::glue("'{spec_modes}'"), sep = ", ")
)
)

check_spec_mode_val(cls, mode)

out <- list(args = args, eng_args = eng_args,
mode = mode, method = method, engine = engine)
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test_args_and_modes.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ test_that('pipe engine', {
expect_error(rand_forest() %>% set_mode(2))
expect_error(rand_forest() %>% set_mode("haberdashery"))
})

test_that("can't set a mode that isn't allowed by the model spec", {
expect_error(
set_mode(linear_reg(), "classification"),
"`mode` should be one of"
)
})