Skip to content

Commit

Permalink
Write out units in full in ggsave error (#5091)
Browse files Browse the repository at this point in the history
* Write units in full

* Avoid 'pixels not pixels' error
  • Loading branch information
teunbrand committed Dec 11, 2022
1 parent afbfe16 commit 0d0de37
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
18 changes: 17 additions & 1 deletion R/save.r
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,24 @@ plot_dim <- function(dim = c(NA, NA), scale = 1, units = "in",
}

if (limitsize && any(dim >= 50)) {
units <- switch(
units,
"in" = "inches",
"cm" = "centimeters",
"mm" = "millimeters",
"px" = "pixels"
)
msg <- paste0(
"Dimensions exceed 50 inches ({.arg height} and {.arg width} are ",
"specified in {.emph {units}}"
)
if (units == "pixels") {
msg <- paste0(msg, ").")
} else {
msg <- paste0(msg, " not pixels).")
}
cli::cli_abort(c(
"Dimensions exceed 50 inches ({.arg height} and {.arg width} are specified in {.emph {units}} not pixels).",
msg,
"i" = "If you're sure you want a plot that big, use {.code limitsize = FALSE}.
"), call = call)
}
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-ggsave.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ test_that("uses 7x7 if no graphics device open", {
test_that("warned about large plot unless limitsize = FALSE", {
expect_error(plot_dim(c(50, 50)), "exceed 50 inches")
expect_equal(plot_dim(c(50, 50), limitsize = FALSE), c(50, 50))
expect_error(plot_dim(c(15000, 15000), units = "px"), "in pixels).")
})

test_that("scale multiplies height & width", {
Expand Down

0 comments on commit 0d0de37

Please sign in to comment.