From bef35bf0b33f4f448026de773abb7c1827983d95 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 7 Oct 2024 11:13:54 +0200 Subject: [PATCH 1/3] allow for alternate leading characters --- R/label-date.R | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/R/label-date.R b/R/label-date.R index 466b1b25..8b34268d 100644 --- a/R/label-date.R +++ b/R/label-date.R @@ -53,8 +53,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 +91,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 } } From e01e43995b0884e2b81e1a9939c285fdaf2f616c Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 7 Oct 2024 11:36:29 +0200 Subject: [PATCH 2/3] add test --- tests/testthat/test-label-date.R | 9 +++++++++ 1 file changed, 9 insertions(+) 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() From 4d2f74073f9c826fc4c40a6b2ce93d735b80d19e Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 7 Oct 2024 11:36:41 +0200 Subject: [PATCH 3/3] document --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/label-date.R | 2 ++ man/label_date.Rd | 9 ++++++++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 38d99259..0ee8fe58 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -39,4 +39,4 @@ Config/testthat/edition: 3 Encoding: UTF-8 LazyLoad: yes Roxygen: list(markdown = TRUE, r6 = FALSE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 diff --git a/NEWS.md b/NEWS.md index 9646d4a5..9e2f72f3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # scales (development version) +* New `label_date_short(leading)` argument to replace leading zeroes (#442) + # scales 1.3.0 ## Better type support diff --git a/R/label-date.R b/R/label-date.R index 8b34268d..adb82262 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) { diff --git a/man/label_date.Rd b/man/label_date.Rd index ea06c22d..e713f019 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?}