From 8ca05e077d5fb8c95eb38c5d552c5f54100c83c9 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:18:10 +0200 Subject: [PATCH] Change leading characters in `label_date_short()` (#456) --- NEWS.md | 1 + R/label-date.R | 18 +++++++++++++++--- man/label_date.Rd | 9 ++++++++- tests/testthat/test-label-date.R | 9 +++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2677cfd9..21c97925 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # scales (development version) +* 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()` (@teunbrand, #405). diff --git a/R/label-date.R b/R/label-date.R index 0e2865fd..b2735eac 100644 --- a/R/label-date.R +++ b/R/label-date.R @@ -22,6 +22,8 @@ #' uses the current locale. Setting this argument requires stringi, and you #' can see a complete list of supported locales with #' [stringi::stri_locale_list()]. +#' @param leading A string to replace leading zeroes with. Can be `""` to +#' disable leading characters or `"\u2007"` for figure-spaces. #' @export #' @examples #' date_range <- function(start, days) { @@ -53,8 +55,9 @@ label_date <- function(format = "%Y-%m-%d", tz = "UTC", locale = NULL) { #' @export #' @rdname label_date #' @param sep Separator to use when combining date formats into a single string. -label_date_short <- function(format = c("%Y", "%b", "%d", "%H:%M"), sep = "\n") { - force_all(format, sep) +label_date_short <- function(format = c("%Y", "%b", "%d", "%H:%M"), sep = "\n", + leading = "0") { + force_all(format, sep, leading) function(x) { dt <- unclass(as.POSIXlt(x)) @@ -90,7 +93,16 @@ label_date_short <- function(format = c("%Y", "%b", "%d", "%H:%M"), sep = "\n") ) format <- apply(for_mat, 1, function(x) paste(rev(x[!is.na(x)]), collapse = sep)) - format(x, format) + x <- format(x, format) + + if (isTRUE(leading == "0")) { + return(x) + } + + # Replace leading 0s with `leading` character + x <- gsub("^0", leading, x) + x <- gsub(paste0(sep, "0"), paste0(sep, leading), x, fixed = TRUE) + x } } diff --git a/man/label_date.Rd b/man/label_date.Rd index 0e6c64da..928f8cd5 100644 --- a/man/label_date.Rd +++ b/man/label_date.Rd @@ -9,7 +9,11 @@ \usage{ label_date(format = "\%Y-\%m-\%d", tz = "UTC", locale = NULL) -label_date_short(format = c("\%Y", "\%b", "\%d", "\%H:\%M"), sep = "\\n") +label_date_short( + format = c("\%Y", "\%b", "\%d", "\%H:\%M"), + sep = "\\n", + leading = "0" +) label_time(format = "\%H:\%M:\%S", tz = "UTC", locale = NULL) @@ -36,6 +40,9 @@ can see a complete list of supported locales with \item{sep}{Separator to use when combining date formats into a single string.} +\item{leading}{A string to replace leading zeroes with. Can be \code{""} to +disable leading characters or \code{"\\u2007"} for figure-spaces.} + \item{unit}{The unit used to interpret numeric input} \item{space}{Add a space before the time unit?} diff --git a/tests/testthat/test-label-date.R b/tests/testthat/test-label-date.R index 62b56cea..29d4e71e 100644 --- a/tests/testthat/test-label-date.R +++ b/tests/testthat/test-label-date.R @@ -24,6 +24,15 @@ test_that("can set locale", { expect_equal(time_format("%B", locale = "fr")(x), "janvier") }) +test_that("label_date_short can replace leading zeroes", { + x <- seq(as.Date("2024-01-01"), as.Date("2025-01-01"), by = "1 month") + labels <- label_date_short( + format = c("%Y", "%m", "%d"), + sep = "-", leading = "x" + )(x) + expect_equal(labels, c("x1-2024", paste0("x", 2:9), c(10:12), "x1-2025")) +}) + test_that("date_short doesn't change unexpectedly", { expect_snapshot({ dformat <- label_date_short()