Skip to content

Commit 505e2ca

Browse files
committed
fix/feat: add as.double methods for int_period to match as.integer
1 parent 0d8aeba commit 505e2ca

6 files changed

Lines changed: 24 additions & 6 deletions

File tree

pkg/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ S3method(as.data.frame,grates_year)
108108
S3method(as.data.frame,grates_yearmonth)
109109
S3method(as.data.frame,grates_yearquarter)
110110
S3method(as.data.frame,grates_yearweek)
111+
S3method(as.double,grates_int_period)
111112
S3method(as.double,grates_isoweek)
112113
S3method(as.double,grates_year)
113114
S3method(as.double,grates_yearmonth)

pkg/NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# grates (development version)
22

3+
## breaking change
4+
5+
* `as.double()` for `<grates_int_period>` objects now returns the same result
6+
(subject to type) as `as.integer()`. Both return the lower bound of related
7+
interval. Note that `<grates_int_period>` objects should still be treated
8+
as experimental for the time being.
9+
310
# grates 1.7.2
411

512
* Small updates to the vignette's css and js.

pkg/R/int_period-class.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ as.integer.grates_int_period <- function(x, ...) {
282282
as.integer(x * n)
283283
}
284284

285+
# -------------------------------------------------------------------------
286+
#' @export
287+
as.double.grates_int_period <- function(x, ...) {
288+
n <- attr(x, "n")
289+
x <- unclass(x)
290+
as.double(x * n)
291+
}
292+
285293
# -------------------------------------------------------------------------
286294
#' @export
287295
as.Date.grates_int_period <- function(x, ...) {

pkg/R/int_period-scale.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ scale_type.grates_int_period <- function(x) {
129129
# breaks function
130130
brks <- function(x) {
131131
if (inherits(breaks, "waiver")) {
132-
dat <- scales::breaks_pretty(n.breaks)(as.numeric(x))
132+
dat <- scales::breaks_pretty(n.breaks)(as.numeric(unclass(x)))
133133
dat <- as.integer(floor(dat))
134-
dat <- as.numeric(new_int_period(dat, n = n))
134+
dat <- as.numeric(unclass(new_int_period(dat, n = n)))
135135
} else {
136-
dat <- as.numeric(breaks)
136+
dat <- as.numeric(unclass(breaks))
137137
}
138138
dat - shift
139139
}
@@ -151,8 +151,8 @@ scale_type.grates_int_period <- function(x) {
151151

152152
scales::trans_new(
153153
"grates_int_period",
154-
transform = as.numeric,
155-
inverse = as.numeric,
154+
transform = function(x) as.numeric(unclass(x)),
155+
inverse = function(x) as.numeric(unclass(x)),
156156
breaks = brks,
157157
format = fmt
158158
)

pkg/R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ stopf <- function(fmt, ..., .use_call = TRUE, .call = sys.call(-1L)) {
3131
grates_classes <- c(
3232
"grates_yearweek" , "grates_isoweek", "grates_epiweek",
3333
"grates_yearmonth", "grates_month" , "grates_yearquarter",
34-
"grates_year" , "grates_period"
34+
"grates_year" , "grates_period" , "grates_int_period"
3535
)
3636
# nolint end
3737

pkg/tests/testthat/test-int_period.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ test_that("int_period, negative values work", {
2828
dates2 <- seq.int(from = -7 * 30 - 28, length.out = 4, by = 7)
2929
expect_identical(as.integer(as_int_period(dates, n = 7)), dates)
3030
expect_identical(as.integer(as_int_period(dates, n = 7) - 4), dates2)
31+
expect_identical(as.double(as_int_period(dates, n = 7)), as.double(dates))
32+
expect_identical(as.double(as_int_period(dates, n = 7) - 4), as.double(dates2))
3133
})
3234

3335
test_that("as_int_period, misc errors and warnings", {

0 commit comments

Comments
 (0)