Skip to content

Commit

Permalink
Error informatively if predictors or outcomes are missing
Browse files Browse the repository at this point in the history
Closes #144
  • Loading branch information
DavisVaughan committed Mar 16, 2022
1 parent a9a9265 commit 6733722
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 12 additions & 1 deletion R/pre-action-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/pre-action-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-pre-action-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6733722

Please sign in to comment.