diff --git a/R/save.r b/R/save.r index f65150e7af..8cfc2bd83e 100644 --- a/R/save.r +++ b/R/save.r @@ -238,6 +238,9 @@ plot_dev <- function(device, filename = NULL, dpi = 300, call = caller_env()) { if (is.null(device)) { device <- to_lower_ascii(tools::file_ext(filename)) + if (identical(device, "")) { + cli::cli_abort("{.arg filename} has no file extension and {.arg device} is {.val NULL}.", call = call) + } } if (!is.character(device) || length(device) != 1) { diff --git a/tests/testthat/test-ggsave.R b/tests/testthat/test-ggsave.R index 6513704538..65e5757b17 100644 --- a/tests/testthat/test-ggsave.R +++ b/tests/testthat/test-ggsave.R @@ -72,6 +72,14 @@ test_that("ggsave warns about empty or multiple filenames", { }) }) +test_that("ggsave fails informatively for no-extension filenames", { + plot <- ggplot(mtcars, aes(disp, mpg)) + geom_point() + expect_error( + ggsave(tempfile(), plot), + '`filename` has no file extension and `device` is "NULL"' + ) +}) + # plot_dim --------------------------------------------------------------- test_that("guesses and informs if dim not specified", {