From 7c59ba69dc5019a5c4287477e4239bdcdc081f05 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Sun, 7 May 2023 13:44:39 -0400 Subject: [PATCH] Improve error message when `plot.title.position` is not correctly mentioned. (#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 --- R/plot-build.R | 21 +++++++++++++-------- tests/testthat/_snaps/theme.md | 4 ++-- tests/testthat/test-stat-summary.R | 1 + 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/R/plot-build.R b/R/plot-build.R index 1bf0019324..2c1695e350 100644 --- a/R/plot-build.R +++ b/R/plot-build.R @@ -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") { diff --git a/tests/testthat/_snaps/theme.md b/tests/testthat/_snaps/theme.md index b879a97d01..b0f60cd35a 100644 --- a/tests/testthat/_snaps/theme.md +++ b/tests/testthat/_snaps/theme.md @@ -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". --- diff --git a/tests/testthat/test-stat-summary.R b/tests/testthat/test-stat-summary.R index 5f8cc4b4fd..925ba8d4b1 100644 --- a/tests/testthat/test-stat-summary.R +++ b/tests/testthat/test-stat-summary.R @@ -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))