Skip to content

Commit 0d0de37

Browse files
authored
Write out units in full in ggsave error (#5091)
* Write units in full * Avoid 'pixels not pixels' error
1 parent afbfe16 commit 0d0de37

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

R/save.r

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,24 @@ plot_dim <- function(dim = c(NA, NA), scale = 1, units = "in",
147147
}
148148

149149
if (limitsize && any(dim >= 50)) {
150+
units <- switch(
151+
units,
152+
"in" = "inches",
153+
"cm" = "centimeters",
154+
"mm" = "millimeters",
155+
"px" = "pixels"
156+
)
157+
msg <- paste0(
158+
"Dimensions exceed 50 inches ({.arg height} and {.arg width} are ",
159+
"specified in {.emph {units}}"
160+
)
161+
if (units == "pixels") {
162+
msg <- paste0(msg, ").")
163+
} else {
164+
msg <- paste0(msg, " not pixels).")
165+
}
150166
cli::cli_abort(c(
151-
"Dimensions exceed 50 inches ({.arg height} and {.arg width} are specified in {.emph {units}} not pixels).",
167+
msg,
152168
"i" = "If you're sure you want a plot that big, use {.code limitsize = FALSE}.
153169
"), call = call)
154170
}

tests/testthat/test-ggsave.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ test_that("uses 7x7 if no graphics device open", {
7474
test_that("warned about large plot unless limitsize = FALSE", {
7575
expect_error(plot_dim(c(50, 50)), "exceed 50 inches")
7676
expect_equal(plot_dim(c(50, 50), limitsize = FALSE), c(50, 50))
77+
expect_error(plot_dim(c(15000, 15000), units = "px"), "in pixels).")
7778
})
7879

7980
test_that("scale multiplies height & width", {

0 commit comments

Comments
 (0)