Skip to content

Commit

Permalink
Merge branch 'main' into glue_labels
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 authored Oct 22, 2024
2 parents a425c09 + f0cd669 commit f30d644
Show file tree
Hide file tree
Showing 17 changed files with 34 additions and 23 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# scales (development version)
* New `label_glue()` labelling function for interpolated strings (#457).
* `fullseq()` and by extension `breaks_width()` can now deal with unsorted
ranges (#435).
* New `label_date_short(leading)` argument to replace leading zeroes (#442)
* `breaks_pretty()` will return the input limit when it has no range (#446)
* `transform_exp()` now has more sensible breaks, available in `breaks_exp()`
Expand Down
4 changes: 4 additions & 0 deletions R/full-seq.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fullseq <- function(range, size, ...) UseMethod("fullseq")

#' @export
fullseq.numeric <- function(range, size, ..., pad = FALSE) {
range <- sort(range)
if (zero_range(range)) {
return(range + size * c(-1, 1) / 2)
}
Expand All @@ -30,13 +31,15 @@ fullseq.numeric <- function(range, size, ..., pad = FALSE) {

#' @export
fullseq.Date <- function(range, size, ...) {
range <- sort(range)
seq(floor_date(range[1], size), ceiling_date(range[2], size), by = size)
}
#' @export
fullseq.POSIXt <- function(range, size, ...) {

# for subsecond interval support
# seq() does not support partial secs in character strings
range <- sort(range)
parsed <- parse_unit_spec(size)
if (parsed$unit == "sec") {
seq(floor_time(range[1], size), ceiling_time(range[2], size), by = parsed$mult)
Expand All @@ -53,6 +56,7 @@ fullseq.difftime <- function(range, size, ...) {
}

input_units <- units(range)
range <- sort(range)

x <- seq(
round_any(as.numeric(range[1], units = "secs"), size_seconds, floor),
Expand Down
2 changes: 1 addition & 1 deletion R/label-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ format_dt <- function(x, format, tz = "UTC", locale = NULL) {
#' `r lifecycle::badge("superseded")`
#'
#' These functions are kept for backward compatibility; you should switch
#' to [label_date()/[label_time()] for new code.
#' to [label_date()]/[label_time()] for new code.
#'
#' @keywords internal
#' @export
Expand Down
4 changes: 2 additions & 2 deletions R/label-number.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Label numbers in decimal format (e.g. 0.12, 1,234)
#'
#' Use `label_number()` force decimal display of numbers (i.e. don't use
#' Use `label_number()` to force decimal display of numbers (i.e. don't use
#' [scientific][label_scientific] notation). `label_comma()` is a special case
#' that inserts a comma every three digits.
#'
Expand Down Expand Up @@ -46,7 +46,7 @@
#' [number options][number_options].
#' @param style_negative A string that determines the style of negative numbers:
#'
#' * `"hyphen"` (the default): preceded by a standard hypen `-`, e.g. `-1`.
#' * `"hyphen"` (the default): preceded by a standard hyphen `-`, e.g. `-1`.
#' * `"minus"`, uses a proper Unicode minus symbol. This is a typographical
#' nicety that ensures `-` aligns with the horizontal bar of the
#' the horizontal bar of `+`.
Expand Down
2 changes: 1 addition & 1 deletion R/pal-dichromat.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' show_col(pal_dichromat("BluetoOrange.10")(10))
#' show_col(pal_dichromat("BluetoOrange.10")(5))
#'
#' # Can use with gradient_n to create a continous gradient
#' # Can use with gradient_n to create a continuous gradient
#' cols <- pal_dichromat("DarkRedtoBlue.12")(12)
#' show_col(pal_gradient_n(cols)(seq(0, 1, length.out = 30)))
#' }
Expand Down
2 changes: 1 addition & 1 deletion man/comma.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/date_format.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/label_bytes.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/label_currency.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/label_date.Rd

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

4 changes: 2 additions & 2 deletions man/label_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/label_number_si.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/label_ordinal.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/label_percent.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/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/pal_dichromat.Rd

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

19 changes: 12 additions & 7 deletions tests/testthat/test-full-seq.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
test_that("works with numeric", {
x <- c(0, 100)
expect_equal(fullseq(x, 50), c(0, 50, 100))
expected <- c(0, 50, 100)
expect_equal(fullseq(x, 50), expected)
expect_equal(fullseq(rev(x), 50), expected)
})

test_that("works with POSIXct", {
x <- as.POSIXct(c("2000-01-01 08:29:58", "2000-01-01 08:30:10"), tz = "UTC")

expect_equal(
fullseq(x, "1 hour"),
as.POSIXct(c("2000-01-01 8:00:00 UTC", "2000-01-01 9:00:00 UTC"), tz = "UTC")
)
expected <- as.POSIXct(c("2000-01-01 8:00:00 UTC", "2000-01-01 9:00:00 UTC"), tz = "UTC")
expect_equal(fullseq(x, "1 hour"), expected)
expect_equal(fullseq(rev(x), "1 hour"), expected)

expect_equal(
fullseq(x, ".5 secs")[1:2],
as.POSIXct(c("2000-01-01 08:29:58.0 UTC", "2000-01-01 08:29:58.5 UTC"), tz = "UTC")
)
})

test_that("works with Date", {
x <- as.Date("2012-01-01") + 1:30
expect_equal(fullseq(x, "1 month"), as.Date(c("2012-01-01", "2012-02-01")))
x <- as.Date("2012-01-01") + c(1, 30)
expected <- as.Date(c("2012-01-01", "2012-02-01", "2012-03-01"))
expect_equal(fullseq(x, "1 month"), expected)
expect_equal(fullseq(rev(x), "1 month"), expected)
})

test_that("works with hms/difftime", {
x <- hms::hms(hours = 0:1)
y <- as.difftime(c(0, 1800, 3600), units = "secs")
expect_equal(fullseq(x, 1800), y)
expect_equal(fullseq(x, "30 mins"), y)
expect_equal(fullseq(rev(x), "30 mins"), y)

# Preserves units
x <- as.difftime(c(0, 1), units = "hours")
Expand Down

0 comments on commit f30d644

Please sign in to comment.