diff --git a/NEWS.md b/NEWS.md index 6dc83ab..ca6c877 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,7 @@ the `labs` argument (#13). * `ggtibble` and `gglist` objects now work with the ggplot2 `%+%` operator (#16) * A new `ggsave()` generic function will now enable simpler saving of `ggtibble` - and `gglist` objects. + and `gglist` objects (unique filenames are required to save). # ggtibble 1.0.1 diff --git a/R/ggsave.R b/R/ggsave.R index e90aff2..7d9637d 100644 --- a/R/ggsave.R +++ b/R/ggsave.R @@ -19,7 +19,7 @@ ggsave <- function(filename, } #' @describeIn ggsave Save the figures in a `gglist` object -#' @param filename A vector of file names for each `plot` +#' @param filename A vector of unique file names for each `plot` #' @export ggsave.gglist <- function(filename, plot, @@ -34,7 +34,11 @@ ggsave.gglist <- function(filename, bg = NULL, create.dir = FALSE, ...) { - stopifnot(length(filename) == length(plot)) + if (length(filename) != length(plot)) { + stop("There must be one `filename` per `plot`") + } else if (any(duplicated(filename))) { + stop("Each `filename` must be unique") + } ret <- vapply( X = seq_along(plot), diff --git a/tests/testthat/test-ggsave.R b/tests/testthat/test-ggsave.R index 0088e0a..e290ff7 100644 --- a/tests/testthat/test-ggsave.R +++ b/tests/testthat/test-ggsave.R @@ -23,4 +23,17 @@ test_that("ggsave", { expected_files ) unlink(expected_files) + + # Enough filenames must be given + expect_error( + ggsave(filename = "a.png", plot = all_plots$figure, path = tempdir()), + regexp = "There must be one `filename` per `plot`", + fixed = TRUE + ) + # Filenames must be unique (don't accidentally overwrite anything) + expect_error( + ggsave(filename = "{Bunit}.png", plot = all_plots, path = tempdir()), + regexp = "Each `filename` must be unique", + fixed = TRUE + ) })