From de1091164ce210b313a4e1b9033099b1a0a13776 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Thu, 24 Mar 2022 14:13:22 -0500 Subject: [PATCH] Use correct types for date-time domains (#336) And fix partial match warnings Fixes #298 --- NEWS.md | 3 +++ R/trans-date.r | 14 ++++++++++++-- tests/testthat/test-trans-date.r | 18 +++++++++++++----- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index c249f2f2..b4ab395d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # scales (development version) +* `time_trans()` and `date_trans()` have `domains` of the correct type so that + they can be transformed without error (#298). + * `label_date()` and `label_time()` gain a `locale` argument that allows you to set the locale used to generate day and month names (#309). diff --git a/R/trans-date.r b/R/trans-date.r index f4f252b2..904bdefd 100644 --- a/R/trans-date.r +++ b/R/trans-date.r @@ -8,7 +8,12 @@ #' t$inverse(t$transform(years)) #' t$format(t$breaks(range(years))) date_trans <- function() { - trans_new("date", "from_date", "to_date", breaks = breaks_pretty()) + trans_new("date", + transform = "from_date", + inverse = "to_date", + breaks = breaks_pretty(), + domain = to_date(c(-Inf, Inf)) + ) } to_date <- function(x) structure(x, class = "Date") @@ -51,7 +56,12 @@ time_trans <- function(tz = NULL) { structure(as.numeric(x), names = names(x)) } - trans_new("time", "from_time", "to_time", breaks = breaks_pretty()) + trans_new("time", + transform = "from_time", + inverse = "to_time", + breaks = breaks_pretty(), + domain = to_time(c(-Inf, Inf)) + ) } #' Transformation for times (class hms) diff --git a/tests/testthat/test-trans-date.r b/tests/testthat/test-trans-date.r index fed589bb..52957836 100644 --- a/tests/testthat/test-trans-date.r +++ b/tests/testthat/test-trans-date.r @@ -10,22 +10,22 @@ with_tz <- function(x, value) { test_that("date/time scales raise error on incorrect inputs", { time <- time_trans() - expect_error(time$trans(a_date), "Invalid input") + expect_error(time$transform(a_date), "Invalid input") date <- date_trans() - expect_error(date$trans(a_time), "Invalid input") + expect_error(date$transform(a_time), "Invalid input") }) test_that("time scales learn timezones", { skip_if_not(getRversion() > "3.3.3") time <- time_trans() - x <- time$inv(time$trans(a_time)) + x <- time$inverse(time$transform(a_time)) expect_equal(tz(x), "UTC") expect_equal(tz2(x), "UTC") time <- time_trans() - x <- time$inv(time$trans(with_tz(a_time, "GMT"))) + x <- time$inverse(time$transform(with_tz(a_time, "GMT"))) expect_equal(tz(x), "GMT") expect_equal(tz2(x), "GMT") @@ -33,7 +33,7 @@ test_that("time scales learn timezones", { test_that("tz arugment overrules default time zone", { time <- time_trans("GMT") - x <- time$inv(time$trans(a_time)) + x <- time$inverse(time$transform(a_time)) expect_equal(tz(x), "GMT") expect_equal(tz2(x), "GMT") @@ -57,3 +57,11 @@ test_that("date_breaks() works", { as.Date(c("2012-01-01", "2012-02-01")) ) }) + +test_that("can invert domain", { + t <- date_trans() + expect_equal(t$transform(t$domain), c(-Inf, Inf)) + + t <- time_trans() + expect_equal(t$transform(t$domain), c(-Inf, Inf)) +})