Skip to content

Commit f84ee7e

Browse files
Friendlier error when supplying extension-less files (#5202)
1 parent e0ee749 commit f84ee7e

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

R/save.r

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ plot_dev <- function(device, filename = NULL, dpi = 300, call = caller_env()) {
238238

239239
if (is.null(device)) {
240240
device <- to_lower_ascii(tools::file_ext(filename))
241+
if (identical(device, "")) {
242+
cli::cli_abort("{.arg filename} has no file extension and {.arg device} is {.val NULL}.", call = call)
243+
}
241244
}
242245

243246
if (!is.character(device) || length(device) != 1) {

tests/testthat/test-ggsave.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ test_that("ggsave warns about empty or multiple filenames", {
7474
)
7575
})
7676

77+
test_that("ggsave fails informatively for no-extension filenames", {
78+
plot <- ggplot(mtcars, aes(disp, mpg)) + geom_point()
79+
expect_error(
80+
ggsave(tempfile(), plot),
81+
'`filename` has no file extension and `device` is "NULL"'
82+
)
83+
})
84+
7785
# plot_dim ---------------------------------------------------------------
7886

7987
test_that("guesses and informs if dim not specified", {

0 commit comments

Comments
 (0)