Skip to content

Commit 344796e

Browse files
Merge pull request #169 from tidymodels/fix160
2 parents 21019d9 + 40af043 commit 344796e

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

R/k_means.R

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,37 @@ check_args.k_means <- function(object) {
250250
#' @keywords internal
251251
#' @export
252252
.k_means_fit_clustMixType <- function(x, k, ...) {
253-
res <- clustMixType::kproto(x, k, ...)
253+
res <- tryCatch(
254+
clustMixType::kproto(x, k, ...),
255+
error = function(cnd) {
256+
if (grepl("No factor variables", cnd$message)) {
257+
cli::cli_abort(
258+
c(
259+
"Engine `clustMixType` requires both numeric and categorical \\
260+
predictors.",
261+
"x" = "Only numeric predictors where used.",
262+
"i" = "Try using the `stats` engine with \\
263+
{.code mod %>% set_engine(\"stats\")}."
264+
),
265+
call = call("fit")
266+
)
267+
}
268+
if (grepl("No numeric variables", cnd$message)) {
269+
cli::cli_abort(
270+
c(
271+
"Engine `clustMixType` requires both numeric and categorical \\
272+
predictors.",
273+
"x" = "Only categorical predictors where used.",
274+
"i" = "Try using the `klaR` engine with \\
275+
{.code mod %>% set_engine(\"klaR\")}."
276+
),
277+
call = call("fit")
278+
)
279+
}
280+
stop(cnd)
281+
}
282+
)
283+
254284
new_order <- unique(res$cluster)
255285
res$cluster <- order(new_order)[res$cluster]
256286
res$centers <- res$centers[new_order, , drop = FALSE]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# modifies errors about suggested other models
2+
3+
Code
4+
k_means(num_clusters = 3) %>% set_engine("clustMixType") %>% fit(~., data = mtcars)
5+
Condition
6+
Error in `fit()`:
7+
! Engine `clustMixType` requires both numeric and categorical predictors.
8+
x Only numeric predictors where used.
9+
i Try using the `stats` engine with `mod %>% set_engine("stats")`.
10+
11+
---
12+
13+
Code
14+
k_means(num_clusters = 3) %>% set_engine("clustMixType") %>% fit(~., data = data.frame(
15+
letters, LETTERS))
16+
Condition
17+
Error in `fit()`:
18+
! Engine `clustMixType` requires both numeric and categorical predictors.
19+
x Only categorical predictors where used.
20+
i Try using the `klaR` engine with `mod %>% set_engine("klaR")`.
21+

tests/testthat/test-k_means-clustMixType.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,22 @@ test_that("extract_cluster_assignment() works", {
9797
expected
9898
)
9999
})
100+
101+
test_that("modifies errors about suggested other models", {
102+
skip_if_not_installed("clustMixType")
103+
104+
expect_snapshot(
105+
error = TRUE,
106+
k_means(num_clusters = 3) %>%
107+
set_engine("clustMixType") %>%
108+
fit(~., data = mtcars)
109+
)
110+
111+
expect_snapshot(
112+
error = TRUE,
113+
k_means(num_clusters = 3) %>%
114+
set_engine("clustMixType") %>%
115+
fit(~., data = data.frame(letters, LETTERS))
116+
)
117+
})
118+

0 commit comments

Comments
 (0)