Skip to content

Commit f7999c0

Browse files
authored
error informatively with bad object in set_* functions (#789)
adds `set_*` default methods
1 parent e1eb30a commit f7999c0

File tree

7 files changed

+113
-6
lines changed

7 files changed

+113
-6
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ S3method(req_pkgs,model_fit)
5757
S3method(req_pkgs,model_spec)
5858
S3method(required_pkgs,model_fit)
5959
S3method(required_pkgs,model_spec)
60+
S3method(set_args,default)
6061
S3method(set_args,model_spec)
62+
S3method(set_engine,default)
6163
S3method(set_engine,model_spec)
64+
S3method(set_mode,default)
6265
S3method(set_mode,model_spec)
6366
S3method(tidy,"_LiblineaR")
6467
S3method(tidy,"_elnet")

R/aaa_models.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ set_env_val <- function(name, value) {
9292

9393
# ------------------------------------------------------------------------------
9494

95+
error_set_object <- function(object, func) {
96+
msg <-
97+
"`{func}()` expected a model specification to be supplied to the \
98+
`object` argument, but received a(n) `{class(object)[1]}` object."
99+
100+
if (inherits(object, "function") &&
101+
isTRUE(environment(object)$.packageName == "parsnip")) {
102+
msg <- c(
103+
msg,
104+
"i" = "Did you mistakenly pass `model_function` rather than `model_function()`?"
105+
)
106+
}
107+
108+
cli::cli_abort(msg, call = call2(func))
109+
}
110+
95111
check_eng_val <- function(eng) {
96112
if (rlang::is_missing(eng) || length(eng) != 1 || !is.character(eng))
97113
rlang::abort("Please supply a character string for an engine name (e.g. `'lm'`)")

R/arguments.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ set_args.model_spec <- function(object, ...) {
7777
)
7878
}
7979

80+
#' @export
81+
set_args.default <- function(object,...) {
82+
error_set_object(object, func = "set_args")
83+
84+
invisible(FALSE)
85+
}
86+
8087
#' @rdname set_args
8188
#' @export
8289
set_mode <- function(object, mode) {
@@ -95,6 +102,13 @@ set_mode.model_spec <- function(object, mode) {
95102
object
96103
}
97104

105+
#' @export
106+
set_mode.default <- function(object, mode) {
107+
error_set_object(object, func = "set_mode")
108+
109+
invisible(FALSE)
110+
}
111+
98112
# ------------------------------------------------------------------------------
99113

100114
maybe_eval <- function(x) {

R/engines.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ set_engine.model_spec <- function(object, engine, ...) {
136136
)
137137
}
138138

139+
#' @export
140+
set_engine.default <- function(object, engine, ...) {
141+
error_set_object(object, func = "set_engine")
142+
143+
invisible(FALSE)
144+
}
145+
139146
#' Display currently available engines for a model
140147
#'
141148
#' The possible engines for a model can depend on what packages are loaded.

tests/testthat/_snaps/args_and_modes.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,58 @@
33
Code
44
set_mode(mtcars, "regression")
55
Condition
6-
Error in `UseMethod()`:
7-
! no applicable method for 'set_mode' applied to an object of class "data.frame"
6+
Error in `set_mode()`:
7+
! `set_mode()` expected a model specification to be supplied to the `object` argument, but received a(n) `data.frame` object.
88

99
---
1010

1111
Code
1212
set_args(mtcars, blah = "blah")
1313
Condition
14-
Error in `UseMethod()`:
15-
! no applicable method for 'set_args' applied to an object of class "data.frame"
14+
Error in `set_args()`:
15+
! `set_args()` expected a model specification to be supplied to the `object` argument, but received a(n) `data.frame` object.
16+
17+
---
18+
19+
Code
20+
bag_tree %>% set_mode("classification")
21+
Condition
22+
Error in `set_mode()`:
23+
! `set_mode()` expected a model specification to be supplied to the `object` argument, but received a(n) `function` object.
24+
i Did you mistakenly pass `model_function` rather than `model_function()`?
25+
26+
---
27+
28+
Code
29+
bag_tree %>% set_engine("rpart")
30+
Condition
31+
Error in `set_engine()`:
32+
! `set_engine()` expected a model specification to be supplied to the `object` argument, but received a(n) `function` object.
33+
i Did you mistakenly pass `model_function` rather than `model_function()`?
34+
35+
---
36+
37+
Code
38+
bag_tree %>% set_args(boop = "bop")
39+
Condition
40+
Error in `set_args()`:
41+
! `set_args()` expected a model specification to be supplied to the `object` argument, but received a(n) `function` object.
42+
i Did you mistakenly pass `model_function` rather than `model_function()`?
43+
44+
---
45+
46+
Code
47+
1L %>% set_args(mode = "classification")
48+
Condition
49+
Error in `set_args()`:
50+
! `set_args()` expected a model specification to be supplied to the `object` argument, but received a(n) `integer` object.
51+
52+
---
53+
54+
Code
55+
bag_tree %>% set_mode("classification")
56+
Condition
57+
Error in `set_mode()`:
58+
! `set_mode()` expected a model specification to be supplied to the `object` argument, but received a(n) `function` object.
59+
i Did you mistakenly pass `model_function` rather than `model_function()`?
1660

tests/testthat/_snaps/misc.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
Code
4848
set_engine(mtcars, "rpart")
4949
Condition
50-
Error in `UseMethod()`:
51-
! no applicable method for 'set_engine' applied to an object of class "data.frame"
50+
Error in `set_engine()`:
51+
! `set_engine()` expected a model specification to be supplied to the `object` argument, but received a(n) `data.frame` object.
5252

tests/testthat/test_args_and_modes.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,28 @@ test_that("set_* functions error when input isn't model_spec", {
109109
expect_snapshot(error = TRUE,
110110
set_args(mtcars, blah = "blah")
111111
)
112+
113+
expect_snapshot(error = TRUE,
114+
bag_tree %>% set_mode("classification")
115+
)
116+
117+
expect_snapshot(error = TRUE,
118+
bag_tree %>% set_engine("rpart")
119+
)
120+
121+
expect_snapshot(error = TRUE,
122+
bag_tree %>% set_args(boop = "bop")
123+
)
124+
125+
# won't raise "info" part of error if not a parsnip-namespaced function
126+
# not a function
127+
expect_snapshot(error = TRUE,
128+
1L %>% set_args(mode = "classification")
129+
)
130+
131+
# not from parsnip
132+
expect_snapshot(error = TRUE,
133+
bag_tree %>% set_mode("classification")
134+
)
112135
})
113136

0 commit comments

Comments
 (0)