Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf0cd669

Browse files
authored
fullseq() sortsrange (#463)
1 parentee07c4f commitf0cd669

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

‎NEWS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#scales (development version)
2-
2+
*`fullseq()` and by extension`breaks_width()` can now deal with unsorted
3+
ranges (#435).
34
* New`label_date_short(leading)` argument to replace leading zeroes (#442)
45
*`breaks_pretty()` will return the input limit when it has no range (#446)
56
*`transform_exp()` now has more sensible breaks, available in`breaks_exp()`

‎R/full-seq.R‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fullseq <- function(range, size, ...) UseMethod("fullseq")
99

1010
#' @export
1111
fullseq.numeric<-function(range,size,...,pad=FALSE) {
12+
range<- sort(range)
1213
if (zero_range(range)) {
1314
return(range+size* c(-1,1)/2)
1415
}
@@ -30,13 +31,15 @@ fullseq.numeric <- function(range, size, ..., pad = FALSE) {
3031

3132
#' @export
3233
fullseq.Date<-function(range,size,...) {
34+
range<- sort(range)
3335
seq(floor_date(range[1],size), ceiling_date(range[2],size),by=size)
3436
}
3537
#' @export
3638
fullseq.POSIXt<-function(range,size,...) {
3739

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

5558
input_units<- units(range)
59+
range<- sort(range)
5660

5761
x<- seq(
5862
round_any(as.numeric(range[1],units="secs"),size_seconds,floor),

‎tests/testthat/test-full-seq.R‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
test_that("works with numeric", {
22
x<- c(0,100)
3-
expect_equal(fullseq(x,50), c(0,50,100))
3+
expected<- c(0,50,100)
4+
expect_equal(fullseq(x,50),expected)
5+
expect_equal(fullseq(rev(x),50),expected)
46
})
57

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

9-
expect_equal(
10-
fullseq(x,"1 hour"),
11-
as.POSIXct(c("2000-01-01 8:00:00 UTC","2000-01-01 9:00:00 UTC"),tz="UTC")
12-
)
11+
expected<- as.POSIXct(c("2000-01-01 8:00:00 UTC","2000-01-01 9:00:00 UTC"),tz="UTC")
12+
expect_equal(fullseq(x,"1 hour"),expected)
13+
expect_equal(fullseq(rev(x),"1 hour"),expected)
14+
1315
expect_equal(
1416
fullseq(x,".5 secs")[1:2],
1517
as.POSIXct(c("2000-01-01 08:29:58.0 UTC","2000-01-01 08:29:58.5 UTC"),tz="UTC")
1618
)
1719
})
1820

1921
test_that("works with Date", {
20-
x<- as.Date("2012-01-01")+1:30
21-
expect_equal(fullseq(x,"1 month"), as.Date(c("2012-01-01","2012-02-01")))
22+
x<- as.Date("2012-01-01")+ c(1,30)
23+
expected<- as.Date(c("2012-01-01","2012-02-01","2012-03-01"))
24+
expect_equal(fullseq(x,"1 month"),expected)
25+
expect_equal(fullseq(rev(x),"1 month"),expected)
2226
})
2327

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

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp