Skip to content

Commit add05b3

Browse files
RodDalBenteunbrand
andauthored
check type for date_breaks and date_minor_breaks (#6044)
* check type for date_breaks and date_minor_breaks #5880 * `date_labels` should be string too * add tests * add news bullet --------- Co-authored-by: Teun van den Brand <[email protected]>
1 parent 7b62271 commit add05b3

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

NEWS.md

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

3+
* Date(time) scales now throw appropriate errors when `date_breaks`,
4+
`date_minor_breaks` or `date_labels` are not strings (@RodDalBen, #5880)
35
* `geom_errorbarh()` is deprecated in favour of
46
`geom_errorbar(orientation = "y")` (@teunbrand, #5961).
57
* `geom_contour()` should be able to recognise a rotated grid of points

R/scale-date.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,15 @@ datetime_scale <- function(aesthetics, transform, trans = deprecated(),
303303
if (is.character(minor_breaks)) minor_breaks <- breaks_width(minor_breaks)
304304

305305
if (!is.waive(date_breaks)) {
306+
check_string(date_breaks)
306307
breaks <- breaks_width(date_breaks)
307308
}
308309
if (!is.waive(date_minor_breaks)) {
310+
check_string(date_minor_breaks)
309311
minor_breaks <- breaks_width(date_minor_breaks)
310312
}
311313
if (!is.waive(date_labels)) {
314+
check_string(date_labels)
312315
labels <- function(self, x) {
313316
tz <- self$timezone %||% "UTC"
314317
label_date(date_labels, tz)(x)

tests/testthat/_snaps/scale-date.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# date(time) scales throw warnings when input is numeric
1+
# date(time) scales throw warnings when input is incorrect
22

33
A <numeric> value was passed to a Date scale.
44
i The value was converted to a <Date> object.
@@ -8,3 +8,27 @@
88
A <numeric> value was passed to a Datetime scale.
99
i The value was converted to a <POSIXt> object.
1010

11+
---
12+
13+
Code
14+
ggplot_build(p + scale_x_date(date_breaks = c(11, 12)))
15+
Condition
16+
Error in `datetime_scale()`:
17+
! `date_breaks` must be a single string, not a double vector.
18+
19+
---
20+
21+
Code
22+
ggplot_build(p + scale_x_date(date_minor_breaks = c(11, 12)))
23+
Condition
24+
Error in `datetime_scale()`:
25+
! `date_minor_breaks` must be a single string, not a double vector.
26+
27+
---
28+
29+
Code
30+
ggplot_build(p + scale_x_date(date_labels = c(11, 12)))
31+
Condition
32+
Error in `datetime_scale()`:
33+
! `date_labels` must be a single string, not a double vector.
34+

tests/testthat/test-scale-date.R

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,24 @@ test_that("datetime colour scales work", {
6969
expect_equal(range(get_layer_data(p)$colour), c("#132B43", "#56B1F7"))
7070
})
7171

72-
test_that("date(time) scales throw warnings when input is numeric", {
72+
test_that("date(time) scales throw warnings when input is incorrect", {
7373
p <- ggplot(data.frame(x = 1, y = 1), aes(x, y)) + geom_point()
7474

7575
expect_snapshot_warning(ggplot_build(p + scale_x_date()))
7676
expect_snapshot_warning(ggplot_build(p + scale_x_datetime()))
77+
78+
expect_snapshot(
79+
ggplot_build(p + scale_x_date(date_breaks = c(11, 12))),
80+
error = TRUE
81+
)
82+
83+
expect_snapshot(
84+
ggplot_build(p + scale_x_date(date_minor_breaks = c(11, 12))),
85+
error = TRUE
86+
)
87+
88+
expect_snapshot(
89+
ggplot_build(p + scale_x_date(date_labels = c(11, 12))),
90+
error = TRUE
91+
)
7792
})

0 commit comments

Comments
 (0)