Skip to content

Commit

Permalink
Merge pull request #5 from tidymodels/better-errors-on-incompatible-m…
Browse files Browse the repository at this point in the history
…odels

throw better errors when methods doesn't exists
  • Loading branch information
EmilHvitfeldt authored Jun 25, 2024
2 parents 23ac50a + 06b5c41 commit 18997f1
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Suggests:
dplyr,
dtplyr,
jsonlite,
kknn,
parsnip,
recipes (>= 1.0.10.9000),
testthat (>= 3.0.0),
Expand Down
19 changes: 18 additions & 1 deletion R/parsnip.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
#' @export
orbital.model_fit <- function(x, ...) {
res <- c(".pred" = deparse1(tidypredict::tidypredict_fit(x)))
res <- tryCatch(
tidypredict::tidypredict_fit(x),
error = function(cnd) {
if (grepl("no applicable method for", cnd$message)) {
cls <- class(x)
cls <- setdiff(cls, "model_fit")
cls <- gsub("^_", "", cls)

cli::cli_abort(
"A model of class {.cls {cls}} is not supported.",
call = rlang::call2("orbital")
)
}
stop(cnd)
}
)

res <- c(".pred" = deparse1(res))

new_orbital_class(res)
}
Expand Down
16 changes: 15 additions & 1 deletion R/recipes.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ orbital.recipe <- function(x, eqs = NULL, ...) {

out <- c(.pred = unname(eqs))
for (i in rev(seq_len(n_steps))) {
res <- orbital(x$steps[[i]], all_vars)
res <- tryCatch(
orbital(x$steps[[i]], all_vars),
error = function(cnd) {
if (grepl("not implemented", cnd$message)) {
cls <- class(x$steps[[i]])
cls <- setdiff(cls, "step")

cli::cli_abort(
"The recipe step {.fun {cls}} is not supported.",
call = rlang::call2("orbital")
)
}
stop(cnd)
}
)

out <- c(res, out)

Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/parsnip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# multiplication works

Code
orbital(wf_fit)
Condition
Error in `orbital()`:
! A model of class <train.kknn> is not supported.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/recipes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# multiplication works

Code
orbital(wf_fit)
Condition
Error in `orbital()`:
! The recipe step `step_impute_knn()` is not supported.

21 changes: 21 additions & 0 deletions tests/testthat/test-parsnip.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("multiplication works", {
skip_if_not_installed("recipes")
skip_if_not_installed("parsnip")
skip_if_not_installed("workflows")
skip_if_not_installed("tidypredict")
skip_if_not_installed("kknn")

rec_spec <- recipes::recipe(mpg ~ ., data = mtcars) %>%
recipes::step_normalize(recipes::all_numeric_predictors())

lm_spec <- parsnip::nearest_neighbor(mode = "regression")

wf_spec <- workflows::workflow(rec_spec, lm_spec)

wf_fit <- parsnip::fit(wf_spec, mtcars)

expect_snapshot(
error = TRUE,
orbital(wf_fit)
)
})
20 changes: 20 additions & 0 deletions tests/testthat/test-recipes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test_that("multiplication works", {
skip_if_not_installed("recipes")
skip_if_not_installed("parsnip")
skip_if_not_installed("workflows")
skip_if_not_installed("tidypredict")

rec_spec <- recipes::recipe(mpg ~ ., data = mtcars) %>%
recipes::step_impute_knn(recipes::all_numeric_predictors())

lm_spec <- parsnip::linear_reg()

wf_spec <- workflows::workflow(rec_spec, lm_spec)

wf_fit <- parsnip::fit(wf_spec, mtcars)

expect_snapshot(
error = TRUE,
orbital(wf_fit)
)
})

0 comments on commit 18997f1

Please sign in to comment.