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

Integers as valid input to theme #5370

Merged
merged 3 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading