diff --git a/NEWS.md b/NEWS.md index 80d6874e..314347a5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # workflows (development version) +* Improved error message in `workflow_variables()` if either `outcomes` or + `predictors` are missing (#144). + * Removed ellipsis dependency in favor of equivalent functions in rlang. * New `extract_parameter_set_dials()` and `extract_parameter_dials()` methods diff --git a/R/pre-action-variables.R b/R/pre-action-variables.R index 538074ee..5bcdb761 100644 --- a/R/pre-action-variables.R +++ b/R/pre-action-variables.R @@ -251,13 +251,24 @@ workflow_variables <- function(outcomes, predictors) { ) } -new_workflow_variables <- function(outcomes, predictors) { +new_workflow_variables <- function(outcomes, + predictors, + ..., + call = caller_env()) { + check_dots_empty() + if (!is_quosure(outcomes)) { abort("`outcomes` must be a quosure.", .internal = TRUE) } if (!is_quosure(predictors)) { abort("`predictors` must be a quosure.", .internal = TRUE) } + if (quo_is_missing(outcomes)) { + abort("`outcomes` can't be missing.", call = call) + } + if (quo_is_missing(predictors)) { + abort("`predictors` can't be missing.", call = call) + } data <- list( outcomes = outcomes, diff --git a/tests/testthat/_snaps/pre-action-variables.md b/tests/testthat/_snaps/pre-action-variables.md index cdffda4f..57a39254 100644 --- a/tests/testthat/_snaps/pre-action-variables.md +++ b/tests/testthat/_snaps/pre-action-variables.md @@ -14,6 +14,22 @@ Error in `add_variables()`: ! Variables cannot be added when a formula already exists. +# informative error if either `predictors` or `outcomes` aren't provided (#144) + + Code + add_variables(workflow(), outcomes = mpg) + Condition + Error in `workflow_variables()`: + ! `predictors` can't be missing. + +--- + + Code + add_variables(workflow(), predictors = mpg) + Condition + Error in `workflow_variables()`: + ! `outcomes` can't be missing. + # cannot add two variables Code diff --git a/tests/testthat/test-pre-action-variables.R b/tests/testthat/test-pre-action-variables.R index c1cd3a45..69f9f55b 100644 --- a/tests/testthat/test-pre-action-variables.R +++ b/tests/testthat/test-pre-action-variables.R @@ -34,6 +34,11 @@ test_that("cannot add variables if a formula already exist", { expect_snapshot(error = TRUE, add_variables(wf, y, x)) }) +test_that("informative error if either `predictors` or `outcomes` aren't provided (#144)", { + expect_snapshot(error = TRUE, add_variables(workflow(), outcomes = mpg)) + expect_snapshot(error = TRUE, add_variables(workflow(), predictors = mpg)) +}) + test_that("works with fit()", { mod <- parsnip::linear_reg() mod <- parsnip::set_engine(mod, "lm")