Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions R/ggsave.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-ggsave.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})
Loading