Skip to content

Commit

Permalink
Integers as valid input to theme (#5370)
Browse files Browse the repository at this point in the history
* Theme accepts integers

* Add test

* Add news bullet
  • Loading branch information
teunbrand committed Aug 1, 2023
1 parent 1f27ffe commit d21ac51
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* Integers are once again valid input to theme arguments that expect numeric
input (@teunbrand, #5369)

* Nicer error messages for xlim/ylim arguments in coord-* functions
(@92amartins, #4601, #5297).

Expand Down
12 changes: 6 additions & 6 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,12 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
legend.key.height = el_def("unit", "legend.key.size"),
legend.key.width = el_def("unit", "legend.key.size"),
legend.text = el_def("element_text", "text"),
legend.text.align = el_def("numeric"),
legend.text.align = el_def(c("numeric", "integer")),
legend.title = el_def("element_text", "title"),
legend.title.align = el_def("numeric"),
legend.position = el_def(c("character", "numeric")),
legend.title.align = el_def(c("numeric", "integer")),
legend.position = el_def(c("character", "numeric", "integer")),
legend.direction = el_def("character"),
legend.justification = el_def(c("character", "numeric")),
legend.justification = el_def(c("character", "numeric", "integer")),
legend.box = el_def("character"),
legend.box.just = el_def("character"),
legend.box.margin = el_def("margin"),
Expand Down Expand Up @@ -522,11 +522,11 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
plot.caption = el_def("element_text", "title"),
plot.caption.position = el_def("character"),
plot.tag = el_def("element_text", "title"),
plot.tag.position = el_def(c("character", "numeric")), # Need to also accept numbers
plot.tag.position = el_def(c("character", "numeric", "integer")), # Need to also accept numbers
plot.tag.location = el_def("character"),
plot.margin = el_def("margin"),

aspect.ratio = el_def("numeric")
aspect.ratio = el_def(c("numeric", "integer"))
)

# Check that an element object has the proper class
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/labels.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# plot.tag.position rejects invalid input

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

---

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@
`plot.tag.position` must be one of "topleft", "top", "topright", "left", "right", "bottomleft", "bottom", or "bottomright", not "test".
i Did you mean "left"?

# Theme validation behaves as expected

The `aspect.ratio` theme element must be a <numeric/integer> object.

7 changes: 7 additions & 0 deletions tests/testthat/test-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,13 @@ test_that("Theme elements are checked during build", {
expect_snapshot_error(ggplotGrob(p))
})

test_that("Theme validation behaves as expected", {
tree <- get_element_tree()
expect_silent(validate_element(1, "aspect.ratio", tree))
expect_silent(validate_element(1L, "aspect.ratio", tree))
expect_snapshot_error(validate_element("A", "aspect.ratio", tree))
})

# Visual tests ------------------------------------------------------------

test_that("aspect ratio is honored", {
Expand Down

0 comments on commit d21ac51

Please sign in to comment.