Skip to content

Commit

Permalink
Improve error message when plot.title.position is not correctly men…
Browse files Browse the repository at this point in the history
…tioned. (#5296)

* Remove extra "

* Update snapshot tests

* Require the hexbin package for this test.

* Remove the `call` arg I was trying.

* Updating the error message with `arg_match0()` and update snapshot
  • Loading branch information
olivroy committed May 7, 2023
1 parent f89212d commit 7c59ba6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
21 changes: 13 additions & 8 deletions R/plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,19 @@ ggplot_gtable.ggplot_built <- function(data) {
# positioning of caption is governed by plot.caption.position
# "panel" means align to the panel(s)
# "plot" means align to the entire plot (except margins and tag)
title_pos <- theme$plot.title.position %||% "panel"
if (!(title_pos %in% c("panel", "plot"))) {
cli::cli_abort('{.var plot.title.position} should be either {.val "panel"} or {.val "plot"}.')
}
caption_pos <- theme$plot.caption.position %||% "panel"
if (!(caption_pos %in% c("panel", "plot"))) {
cli::cli_abort('{.var plot.caption.position} should be either {.val "panel"} or {.val "plot"}.')
}
title_pos <- arg_match0(
theme$plot.title.position %||% "panel",
c("panel", "plot"),
arg_nm = "plot.title.position",
error_call = expr(theme())
)

caption_pos <- arg_match0(
theme$plot.caption.position %||% "panel",
values = c("panel", "plot"),
arg_nm = "plot.caption.position",
error_call = expr(theme())
)

pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), , drop = FALSE]
if (title_pos == "panel") {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@

# Theme elements are checked during build

`plot.title.position` should be either "\"panel\"" or "\"plot\"".
`plot.title.position` must be one of "panel" or "plot", not "test".

---

`plot.caption.position` should be either "\"panel\"" or "\"plot\"".
`plot.caption.position` must be one of "panel" or "plot", not "test".

---

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-stat-summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ test_that("stat_summary_(2d|hex) work with lambda expressions", {

# stat_summary_hex
# this plot is a bit funky, but easy to reason through
skip_if_not_installed("hexbin")
p1 <- ggplot(dat, aes(x, y, z = z)) +
stat_summary_hex(fun = function(x) mean(x))

Expand Down

0 comments on commit 7c59ba6

Please sign in to comment.