Skip to content

Commit 369b213

Browse files
committed
Error checking for LiblineaR type and mode
1 parent a7b65c4 commit 369b213

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

R/svm_linear.R

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,31 @@ translate.svm_linear <- function(x, engine = x$engine, ...) {
146146

147147
if (x$engine == "LiblineaR") {
148148

149-
if (x$mode == "regression")
149+
if (is_null(x$eng_args$type)) {
150+
liblinear_type <- NULL
151+
} else {
152+
liblinear_type <- quo_get_expr(x$eng_args$type)
153+
}
154+
155+
if (x$mode == "regression") {
150156
if (is_null(quo_get_expr(x$args$margin)))
151157
arg_vals$svr_eps <- 0.1
152-
158+
if (!is_null(liblinear_type))
159+
if(!liblinear_type %in% 11:13)
160+
rlang::abort(
161+
paste0("The LiblineaR engine argument of `type` = ",
162+
liblinear_type,
163+
" does not correspond to an SVM regression model.")
164+
)
165+
} else if (x$mode == "classification") {
166+
if (!is_null(liblinear_type))
167+
if(!liblinear_type %in% 1:5)
168+
rlang::abort(
169+
paste0("The LiblineaR engine argument of `type` = ",
170+
liblinear_type,
171+
" does not correspond to an SVM classification model.")
172+
)
173+
}
153174
}
154175

155176
x$method$fit$args <- arg_vals

tests/testthat/test_svm_linear.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ test_that('updating', {
7676
test_that('bad input', {
7777
expect_error(svm_linear(mode = "reallyunknown"))
7878
expect_error(translate(svm_linear(mode = "regression") %>% set_engine( NULL)))
79+
expect_error(translate(svm_linear(mode = "regression") %>% set_engine("LiblineaR", type = 3)))
80+
expect_error(translate(svm_linear(mode = "classification") %>% set_engine("LiblineaR", type = 11)))
7981
})
8082

8183
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)