Skip to content

Commit

Permalink
Merge branch 'main' into deriv
Browse files Browse the repository at this point in the history
  • Loading branch information
mjskay committed Apr 18, 2022
2 parents a08583e + a2552d2 commit 30fed0e
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 31 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
^pkgdown$
^\.github/workflows/R\.yaml$
^\.github/workflows/pr-commands\.yaml$
^CRAN-SUBMISSION$
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: scales
Title: Scale Functions for Visualization
Version: 1.1.1.9000
Version: 1.2.0.9000
Authors@R: c(
person("Hadley", "Wickham", , "[email protected]", role = c("aut", "cre")),
person("Dana", "Seidel", role = "aut"),
Expand Down
10 changes: 7 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## New features

* Transformation objects can optionally include the derivatives of the transform
and the inverse transform (@mjskay, #322).

# scales 1.2.0

## New features

* `label_number()`:

* New `style_positive` and `style_negative` argument control how positive and
Expand Down Expand Up @@ -40,9 +47,6 @@
previously have passed the name of a single transformer. For example,
`scale_y_continuous(trans = c("log10", "reverse"))` will create a
reverse log-10 scale (#287).

* Transformation objects can optionally include the derivatives of the transform
and the inverse transform (@mjskay, #322).

## Bug fixes and minor improvements

Expand Down
6 changes: 3 additions & 3 deletions R/label-number-si.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' @export
#' @family labels for continuous scales
#' @family labels for log scales
#' @examples
#' @examplesIf getRversion() >= "3.5"
#' # label_number_si() doesn't actually produce SI prefixes:
#' demo_continuous(c(1, 1e9), labels = label_number_si("g"))
#'
Expand All @@ -44,7 +44,7 @@ label_number_si <- function(unit = "", accuracy = NULL, scale = 1, suffix = "",
}

cut_bad_si <- function(unit) {
out <- cut_short_scale()
names(out) <- paste0(if (unit != "") " ", names(out), unit)
out <- cut_short_scale(unit != "")
names(out) <- paste0(names(out), unit)
out
}
28 changes: 19 additions & 9 deletions R/label-number.r
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#' width (see [base::format()]).
#' @param ... Other arguments passed on to [base::format()].
#' @export
#' @examples
#' @examplesIf getRversion() >= "3.5"
#' demo_continuous(c(-1e6, 1e6))
#' demo_continuous(c(-1e6, 1e6), labels = label_number())
#' demo_continuous(c(-1e6, 1e6), labels = label_comma())
Expand Down Expand Up @@ -339,18 +339,18 @@ scale_cut <- function(x, breaks, scale = 1, accuracy = NULL, suffix = "") {
))
break_suffix[is.na(break_suffix)] <- names(which.min(breaks))

scale <- scale * unname(1 / breaks[break_suffix])
scale[which(scale %in% c(Inf, NA))] <- 1
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

suffix <- paste0(break_suffix, suffix)
accuracy <- accuracy %||% stats::ave(x * scale, scale, FUN = precision)
accuracy <- accuracy %||% stats::ave(x * break_scale, break_scale, FUN = precision)

list(
scale = scale,
scale = break_scale,
suffix = suffix,
accuracy = accuracy
)
Expand All @@ -361,14 +361,23 @@ scale_cut <- function(x, breaks, scale = 1, accuracy = NULL, suffix = "") {

#' @export
#' @rdname number
cut_short_scale <- function() {
c(0, K = 1e3, M = 1e6, B = 1e9, T = 1e12)
#' @param space Add a space before the scale suffix?
cut_short_scale <- function(space = FALSE) {
out <- c(0, K = 1e3, M = 1e6, B = 1e9, T = 1e12)
if (space) {
names(out) <- paste0(" ", names(out))
}
out
}

#' @export
#' @rdname number
cut_long_scale <- function() {
c(0, K = 1e3, M = 1e6, B = 1e12, T = 1e18)
cut_long_scale <- function(space = FALSE) {
out <- c(0, K = 1e3, M = 1e6, B = 1e12, T = 1e18)
if (space) {
names(out) <- paste0(" ", names(out))
}
out
}

# power-of-ten prefixes used by the International System of Units (SI)
Expand All @@ -388,6 +397,7 @@ names(si_powers)[si_powers == -6] <- "\u00b5"

#' @export
#' @rdname number
#' @param unit SI unit abbreviation.
cut_si <- function(unit) {
out <- c(0, 10^si_powers)
names(out) <- c(paste0(" ", unit), paste0(" ", names(si_powers), unit))
Expand Down
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ reference:

news:
releases:
- text: "Version 1.2.0"
href: https://www.tidyverse.org/blog/2022/04/scales-1-2-0/
- text: "Version 1.1.0"
href: https://www.tidyverse.org/blog/2019/11/scales-1-1-0/
- text: "Version 1.0.0"
Expand Down
12 changes: 0 additions & 12 deletions cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,10 @@ I'm seeing a warning from the JSTOR links, presumably because of their overactiv

We checked 464 reverse dependencies, comparing R CMD check results across CRAN and dev versions of this package.

* We saw 2 new problems
* We failed to check 21 packages

Issues with CRAN packages are summarised below.

### New problems
(This reports the first line of each new failure)

* BIGL
checking whether package ‘BIGL’ can be installed ... WARNING
False positive because RGL does not work on our check machine.

* gt
checking tests ... ERROR
I provided a fix to the authors on May 7

### Failed to check

* anglr (NA)
Expand Down
2 changes: 2 additions & 0 deletions man/label_number.Rd

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

2 changes: 2 additions & 0 deletions man/label_number_si.Rd

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

8 changes: 6 additions & 2 deletions man/number.Rd

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

2 changes: 1 addition & 1 deletion man/viridis_pal.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-label-number.r
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,26 @@ test_that("zero and special values don't get units", {

test_that("scale applied before scale_cut", {
expect_equal(number(500, scale = 2, scale_cut = cut_short_scale()), "1K")
expect_equal(
number(c(0, 5e5, 1.2e6), scale = 1/1000, scale_cut = cut_short_scale()),
c("0", "500", "1K")
)
})

test_that("cut_si() adds space before unit", {
skip_if_not(getRversion() >= "3.5")
expect_equal(number(c(0, 1, 1000), scale_cut = cut_si("m")), c("0 m", "1 m", "1 km"))
})

test_that("handles out-of-range inputs", {
expect_equal(number(1e15, scale_cut = cut_short_scale()), "1 000T")
})

test_that("short and long scale can add spaces", {
expect_equal(number(1000, scale_cut = cut_short_scale(TRUE)), "1 K")
expect_equal(number(1000, scale_cut = cut_long_scale(TRUE)), "1 K")
})

test_that("scale_cut checks its inputs", {
expect_snapshot(error = TRUE, {
number(1, scale_cut = 0)
Expand All @@ -167,6 +177,8 @@ test_that("scale_cut checks its inputs", {
})

test_that("built-in functions return expected values", {
skip_if_not(getRversion() >= "3.5")

expect_equal(number(1e9, scale_cut = cut_short_scale()), "1B")
expect_equal(number(1e9, scale_cut = cut_long_scale()), "1 000M")
expect_equal(number(1e9, scale_cut = cut_si("m")), "1 Gm")
Expand Down

0 comments on commit 30fed0e

Please sign in to comment.