Skip to content

Commit f8505bd

Browse files
Make userfacing set_* functions generic (#769)
* make set_engine generic * make set_mode generic * make set_args generic
1 parent 3b43abb commit f8505bd

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ 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,model_spec)
61+
S3method(set_engine,model_spec)
62+
S3method(set_mode,model_spec)
6063
S3method(tidy,"_LiblineaR")
6164
S3method(tidy,"_elnet")
6265
S3method(tidy,"_fishnet")

R/arguments.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ check_eng_args <- function(args, obj, core_args) {
4949
#'
5050
#' @export
5151
set_args <- function(object, ...) {
52+
UseMethod("set_args")
53+
}
54+
55+
#' @export
56+
set_args.model_spec <- function(object, ...) {
5257
the_dots <- enquos(...)
5358
if (length(the_dots) == 0)
5459
rlang::abort("Please pass at least one named argument.")
@@ -75,6 +80,11 @@ set_args <- function(object, ...) {
7580
#' @rdname set_args
7681
#' @export
7782
set_mode <- function(object, mode) {
83+
UseMethod("set_mode")
84+
}
85+
86+
#' @export
87+
set_mode.model_spec <- function(object, mode) {
7888
cls <- class(object)[1]
7989
if (rlang::is_missing(mode)) {
8090
spec_modes <- rlang::env_get(get_model_env(), paste0(cls, "_modes"))

R/engines.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ load_libs <- function(x, quiet, attach = FALSE) {
105105
#'
106106
#' @export
107107
set_engine <- function(object, engine, ...) {
108+
UseMethod("set_engine")
109+
}
110+
111+
#' @export
112+
set_engine.model_spec <- function(object, engine, ...) {
108113
mod_type <- class(object)[1]
109-
if (!inherits(object, "model_spec")) {
110-
rlang::abort("`object` should have class 'model_spec'.")
111-
}
112114

113115
if (rlang::is_missing(engine)) {
114116
stop_missing_engine(mod_type)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# set_* functions error when input isn't model_spec
2+
3+
Code
4+
set_mode(mtcars, "regression")
5+
Condition
6+
Error in `UseMethod()`:
7+
! no applicable method for 'set_mode' applied to an object of class "data.frame"
8+
9+
---
10+
11+
Code
12+
set_args(mtcars, blah = "blah")
13+
Condition
14+
Error in `UseMethod()`:
15+
! no applicable method for 'set_args' applied to an object of class "data.frame"
16+

tests/testthat/_snaps/misc.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@
1414
Computational engine: rpart
1515
1616

17+
# set_engine works as a generic
18+
19+
Code
20+
set_engine(mtcars, "rpart")
21+
Condition
22+
Error in `UseMethod()`:
23+
! no applicable method for 'set_engine' applied to an object of class "data.frame"
24+

tests/testthat/test_args_and_modes.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,13 @@ test_that("unavailable modes for an engine and vice-versa", {
101101
)
102102
})
103103

104+
test_that("set_* functions error when input isn't model_spec", {
105+
expect_snapshot(error = TRUE,
106+
set_mode(mtcars, "regression")
107+
)
108+
109+
expect_snapshot(error = TRUE,
110+
set_args(mtcars, blah = "blah")
111+
)
112+
})
104113

tests/testthat/test_misc.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,9 @@ test_that('model type functions message informatively with unknown implementatio
106106
set_engine("rpart")
107107
)
108108
})
109+
110+
test_that('set_engine works as a generic', {
111+
expect_snapshot(error = TRUE,
112+
set_engine(mtcars, "rpart")
113+
)
114+
})

0 commit comments

Comments
 (0)