Skip to content

Commit

Permalink
Merge pull request #1232 from tidymodels/fix-tidy.step_cut
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt authored Oct 10, 2023
2 parents 5824c9b + c5f5459 commit 5605ebe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Documentation for tidy methods for all steps has been added when missing and improved to describe the return value more accurately. (#936)

* Fixed bug where `tidy.step_cut()` always returned zero row tibbles for trained recipes. (#1229)

* It is now documented that `step_spline_b()` can be made periodic. (#1223)

# recipes 1.0.8
Expand Down
15 changes: 6 additions & 9 deletions R/cut.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{value}{character, the location of the cuts}
#' \item{value}{numeric, the location of the cuts}
#' \item{id}{character, id of this step}
#' }
#'
Expand Down Expand Up @@ -217,17 +217,14 @@ print.step_cut <-
#' @export
tidy.step_cut <- function(x, ...) {
if (is_trained(x)) {
values <- vapply(
unname(x$class_list),
FUN = function(x) paste0(x, collapse = "-"),
FUN.VALUE = character(1)
res <- tibble(
terms = rep(names(x$breaks), lengths(x$breaks)),
value = unlist(x$breaks, use.names = FALSE) %||% double()
)

res <- tibble(terms = names(x$breaks), value = values)
} else {
term_names <- sel2char(x$terms)
res <- tibble(terms = term_names, value = na_chr)
res <- tibble(terms = term_names, value = na_dbl)
}
res$id <- x$id
res$id <- rep(x$id, nrow(res))
res
}
2 changes: 1 addition & 1 deletion man/step_cut.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion tests/testthat/test-cut.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,26 @@ test_that("step_cut integration test", {
)
})

test_that("tidy method works", {
rec <- recipe(~., data = mtcars) %>%
step_cut(disp, hp, breaks = 200) %>%
prep()

res <- tidy(rec, 1)

expect_identical(
rep(c("disp", "hp"), each = 3),
res$terms
)

expect_identical(
c(min(mtcars$disp), 200, max(mtcars$disp),
min(mtcars$hp), 200, max(mtcars$hp)),
res$value
)
})


# Infrastructure ---------------------------------------------------------------

test_that("bake method errors when needed non-standard role columns are missing", {
Expand Down Expand Up @@ -194,7 +214,7 @@ test_that("empty selection tidy method works", {
rec <- recipe(mpg ~ ., mtcars)
rec <- step_cut(rec, breaks = 5)

expect <- tibble(terms = character(), value = character(), id = character())
expect <- tibble(terms = character(), value = double(), id = character())

expect_identical(tidy(rec, number = 1), expect)

Expand Down

0 comments on commit 5605ebe

Please sign in to comment.