Skip to content

Commit

Permalink
Fix configuration of breaks when using recent scales package (#140)
Browse files Browse the repository at this point in the history
* Fix configuration of breaks when using recent scales package.

The latest release of the scales package has added a pair of optional
arguments, `d_transform` and `d_inverse`, in the middle of the parameter
list for `trans_new` (see r-lib/scales#341). As
a consequence, this has shifted the position of the `breaks` parameter
from 4th to 6th.

Because the `bench_time_trans` and `bench_bytes_trans` functions from
this package were passing in the breaks object positionally, this change
in the scales package means that the breaks object is now matched with
the new `d_transform` formal parameter and it ends up being ignored.

This commit changes the arguments to `trans_new` from being positional
to being named, which should be more robust to future changes to the
function.

* Tweak formatting

* NEWS bullet

---------

Co-authored-by: Davis Vaughan <[email protected]>
  • Loading branch information
plietar and DavisVaughan authored Jan 16, 2025
1 parent 0c98d82 commit 3dd50d7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# bench (development version)

* Fixed an issue in `bench_time_trans()` and `bench_bytes_trans()` where pretty
breaks were not being applied correctly (#140, @plietar, @simonpcouch).

* R >=4.0.0 is now required, which is aligned with tidyverse standards.

* Switched to modern ggplot2 conventions internally (#141, @olivroy).
Expand Down
17 changes: 13 additions & 4 deletions R/bytes.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,25 @@ type_sum.bench_bytes <- function(x) {
bench_bytes_trans <- function(base = 2) {
if (is.null(base)) {
return(
scales::trans_new("bch:byt", as.numeric, as_bench_bytes,
scales::pretty_breaks(), domain = c(1e-100, Inf)
scales::trans_new(
name = "bch:byt",
transform = as.numeric,
inverse = as_bench_bytes,
breaks = scales::pretty_breaks(),
domain = c(1e-100, Inf)
)
)
}
trans <- function(x) log(as.numeric(x), base)
inv <- function(x) as_bench_bytes(base ^ as.numeric(x))

scales::trans_new(paste0("bch:byt-", format(base)), trans, inv,
scales::log_breaks(base = base), domain = c(1e-100, Inf))
scales::trans_new(
name = paste0("bch:byt-", format(base)),
transform = trans,
inverse = inv,
breaks = scales::log_breaks(base = base),
domain = c(1e-100, Inf)
)
}

# Lazily registered in `.onLoad()`
Expand Down
17 changes: 13 additions & 4 deletions R/time.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,26 @@ type_sum.bench_time <- function(x) {
bench_time_trans <- function(base = 10) {
if (is.null(base)) {
return(
scales::trans_new("bch:tm", as.numeric, as_bench_time,
scales::pretty_breaks(), domain = c(1e-100, Inf)
scales::trans_new(
name = "bch:tm",
transform = as.numeric,
inverse = as_bench_time,
breaks = scales::pretty_breaks(),
domain = c(1e-100, Inf)
)
)
}

trans <- function(x) log(as.numeric(x), base)
inv <- function(x) as_bench_time(base ^ as.numeric(x))

scales::trans_new(paste0("bch:tm-", format(base)), trans, inv,
scales::log_breaks(base = base), domain = c(1e-100, Inf))
scales::trans_new(
name = paste0("bch:tm-", format(base)),
transform = trans,
inverse = inv,
breaks = scales::log_breaks(base = base),
domain = c(1e-100, Inf)
)
}

# Lazily registered in `.onLoad()`
Expand Down

0 comments on commit 3dd50d7

Please sign in to comment.