Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change leading characters in label_date_short() #456

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 15 additions & 3 deletions R/label-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
}

Expand Down
9 changes: 8 additions & 1 deletion man/label_date.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test-label-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading