Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fullseq() sorts range #463

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# scales (development version)

* `fullseq()` and by extension `breaks_width()` can now deal with unsorted
ranges (#435).

# scales 1.3.0

## Better type support
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
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")))
Comment on lines -20 to -21
Copy link
Contributor Author

@teunbrand teunbrand Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was wonky. fullseq() only considers range[1] and range[2], but this is a vector of 30 dates instead of a range of 2 dates.

In addition, ceiling_date("2012-01-01", "1 month") gives 1st of March because 31 days from the end of January is in March due to February typically having only 28 days. I consider this out of scope for this PR, but it isn't doing any real harm anyway.

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
Loading