Skip to content

Commit

Permalink
Fix #346
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Nov 1, 2023
1 parent 170c51e commit 48f82cf
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 13 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
vectors during training. Mixing of character and factor data will still lead
to different results depending on the training order.
* Add a rescale method for `difftime` objects (#382)
* The `scale_cut` argument in `number()` now works as advertised for values
below the lowest cut value (#346)

# scales 1.2.1

Expand Down
10 changes: 3 additions & 7 deletions R/label-number.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,24 +327,20 @@ scale_cut <- function(x, breaks, scale = 1, accuracy = NULL, suffix = "") {
if (any(is.na(breaks))) {
cli::cli_abort("{.arg scale_cut} values must not be missing")
}
if (!identical(breaks[[1]], 0) && !identical(breaks[[1]], 0L)) {
cli::cli_abort("Smallest value of {.arg scales_cut} must be zero")
}

break_suffix <- as.character(cut(
abs(x * scale),
breaks = c(unname(breaks), Inf),
labels = c(names(breaks)),
right = FALSE
))
break_suffix[is.na(break_suffix)] <- names(which.min(breaks))
break_suffix[is.na(break_suffix)] <- ""

break_scale <- scale * unname(1 / breaks[break_suffix])
break_scale[which(break_scale %in% c(Inf, NA))] <- scale

# exact zero is not scaled
x_zero <- which(abs(x) == 0)
scale[x_zero] <- 1
# exact zero is not scaled, nor are values below lowest break
break_scale[abs(x) == 0 | is.na(break_scale)] <- 1

suffix <- paste0(break_suffix, suffix)
accuracy <- accuracy %||% stats::ave(x * break_scale, break_scale, FUN = precision)
Expand Down
5 changes: 0 additions & 5 deletions tests/testthat/_snaps/label-number.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
Condition
Error in `scale_cut()`:
! `scale_cut` must be a named numeric vector
Code
number(1, scale_cut = c(x = 1, y = 2))
Condition
Error in `scale_cut()`:
! Smallest value of `scales_cut` must be zero
Code
number(1, scale_cut = c(x = 0, NA))
Condition
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-label-number.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ test_that("scale_cut checks its inputs", {
expect_snapshot(error = TRUE, {
number(1, scale_cut = 0)
number(1, scale_cut = "x")
number(1, scale_cut = c(x = 1, y = 2))
number(1, scale_cut = c(x = 0, NA))
})
})
Expand Down

0 comments on commit 48f82cf

Please sign in to comment.