diff --git a/DESCRIPTION b/DESCRIPTION index 114c326..781eae4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,6 +17,7 @@ Suggests: dplyr, dtplyr, jsonlite, + kknn, parsnip, recipes (>= 1.0.10.9000), testthat (>= 3.0.0), diff --git a/R/parsnip.R b/R/parsnip.R index c54a6b9..921737b 100644 --- a/R/parsnip.R +++ b/R/parsnip.R @@ -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) } diff --git a/R/recipes.R b/R/recipes.R index 9e93601..b21e109 100644 --- a/R/recipes.R +++ b/R/recipes.R @@ -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) diff --git a/tests/testthat/_snaps/parsnip.md b/tests/testthat/_snaps/parsnip.md new file mode 100644 index 0000000..7d54614 --- /dev/null +++ b/tests/testthat/_snaps/parsnip.md @@ -0,0 +1,8 @@ +# multiplication works + + Code + orbital(wf_fit) + Condition + Error in `orbital()`: + ! A model of class is not supported. + diff --git a/tests/testthat/_snaps/recipes.md b/tests/testthat/_snaps/recipes.md new file mode 100644 index 0000000..310ab3a --- /dev/null +++ b/tests/testthat/_snaps/recipes.md @@ -0,0 +1,8 @@ +# multiplication works + + Code + orbital(wf_fit) + Condition + Error in `orbital()`: + ! The recipe step `step_impute_knn()` is not supported. + diff --git a/tests/testthat/test-parsnip.R b/tests/testthat/test-parsnip.R new file mode 100644 index 0000000..bff9988 --- /dev/null +++ b/tests/testthat/test-parsnip.R @@ -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) + ) +}) diff --git a/tests/testthat/test-recipes.R b/tests/testthat/test-recipes.R new file mode 100644 index 0000000..9658526 --- /dev/null +++ b/tests/testthat/test-recipes.R @@ -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) + ) +})