Skip to content

Commit d21ac51

Browse files
authored
Integers as valid input to theme (#5370)
* Theme accepts integers * Add test * Add news bullet
1 parent 1f27ffe commit d21ac51

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

NEWS.md

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

3+
* Integers are once again valid input to theme arguments that expect numeric
4+
input (@teunbrand, #5369)
5+
36
* Nicer error messages for xlim/ylim arguments in coord-* functions
47
(@92amartins, #4601, #5297).
58

R/theme-elements.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,12 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
476476
legend.key.height = el_def("unit", "legend.key.size"),
477477
legend.key.width = el_def("unit", "legend.key.size"),
478478
legend.text = el_def("element_text", "text"),
479-
legend.text.align = el_def("numeric"),
479+
legend.text.align = el_def(c("numeric", "integer")),
480480
legend.title = el_def("element_text", "title"),
481-
legend.title.align = el_def("numeric"),
482-
legend.position = el_def(c("character", "numeric")),
481+
legend.title.align = el_def(c("numeric", "integer")),
482+
legend.position = el_def(c("character", "numeric", "integer")),
483483
legend.direction = el_def("character"),
484-
legend.justification = el_def(c("character", "numeric")),
484+
legend.justification = el_def(c("character", "numeric", "integer")),
485485
legend.box = el_def("character"),
486486
legend.box.just = el_def("character"),
487487
legend.box.margin = el_def("margin"),
@@ -522,11 +522,11 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
522522
plot.caption = el_def("element_text", "title"),
523523
plot.caption.position = el_def("character"),
524524
plot.tag = el_def("element_text", "title"),
525-
plot.tag.position = el_def(c("character", "numeric")), # Need to also accept numbers
525+
plot.tag.position = el_def(c("character", "numeric", "integer")), # Need to also accept numbers
526526
plot.tag.location = el_def("character"),
527527
plot.margin = el_def("margin"),
528528

529-
aspect.ratio = el_def("numeric")
529+
aspect.ratio = el_def(c("numeric", "integer"))
530530
)
531531

532532
# Check that an element object has the proper class

tests/testthat/_snaps/labels.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# plot.tag.position rejects invalid input
22

3-
The `plot.tag.position` theme element must be a <character/numeric> object.
3+
The `plot.tag.position` theme element must be a <character/numeric/integer> object.
44

55
---
66

tests/testthat/_snaps/theme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@
5353
`plot.tag.position` must be one of "topleft", "top", "topright", "left", "right", "bottomleft", "bottom", or "bottomright", not "test".
5454
i Did you mean "left"?
5555

56+
# Theme validation behaves as expected
57+
58+
The `aspect.ratio` theme element must be a <numeric/integer> object.
59+

tests/testthat/test-theme.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,13 @@ test_that("Theme elements are checked during build", {
492492
expect_snapshot_error(ggplotGrob(p))
493493
})
494494

495+
test_that("Theme validation behaves as expected", {
496+
tree <- get_element_tree()
497+
expect_silent(validate_element(1, "aspect.ratio", tree))
498+
expect_silent(validate_element(1L, "aspect.ratio", tree))
499+
expect_snapshot_error(validate_element("A", "aspect.ratio", tree))
500+
})
501+
495502
# Visual tests ------------------------------------------------------------
496503

497504
test_that("aspect ratio is honored", {

0 commit comments

Comments
 (0)