Skip to content

file extension parameter for ggsave() to allow device specification for files without extension #939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2015
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
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ ggplot2 1.0.1.9000
* `theme_minimal()` got slightly more minimal by removing the axis ticks:
labels now line up beneath the grid lines (@tomschloss, #1084)

* Parameter `device` now supports character argument to specify which supported
device to use ('pdf', 'png', 'jpeg', etc.), for when it cannot be correctly
inferred from the file extension (for example when a temporay filename is
supplied server side in shiny apps) (@sebkopf, #939)

* `ggsave()` has been simplified a little to make it easier to maintain.
It no longer checks that you're printing a ggplot2 object (so now also
works with any grid grob) (#970), and always requires a filename.
Expand Down
22 changes: 21 additions & 1 deletion R/save.r
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#'
#' @param filename File name to create on disk.
#' @param plot Plot to save, defaults to last plot displayed.
#' @param device Device to use. By default, extracted from extension.
#' @param device Device to use (function or any of the recognized extensions,
#' e.g. \code{"pdf"}). By default, extracted from filename extension.
#' \code{ggsave} currently recognises eps/ps, tex (pictex), pdf, jpeg, tiff,
#' png, bmp, svg and wmf (windows only).
#' @param path Path to save plot to (combined with filename).
Expand All @@ -34,6 +35,12 @@
#'
#' unlink("ratings.pdf")
#' unlink("ratings.png")
#'
#' # specify device when saving to a file with unknown extension
#' # (for example a server supplied temporary file)
#' file <- tempfile()
#' ggsave(file, device = "pdf")
#' unlink(file)
#' }
ggsave <- function(filename, plot = last_plot(),
device = default_device(filename), path = NULL, scale = 1,
Expand Down Expand Up @@ -65,9 +72,22 @@ ggsave <- function(filename, plot = last_plot(),
default_device <- function(filename) {
pieces <- strsplit(filename, "\\.")[[1]]
ext <- tolower(pieces[length(pieces)])
match_device(ext)
}

match_device <- function(ext) {
if(!exists(ext, mode = "function")) {
stop("No graphics device defined for the file extension '", ext, "'. ",
"Make sure to specify a filename with supported extension or ",
"set the device parameter.", call. = FALSE)
}
match.fun(ext)
}

if (is.character(device)) {
device <- match_device(device)
}

dim <- plot_dim(c(width, height), scale = scale, units = units,
limitsize = limitsize)

Expand Down
9 changes: 8 additions & 1 deletion man/ggsave.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ ggsave(filename, plot = last_plot(), device = default_device(filename),

\item{plot}{Plot to save, defaults to last plot displayed.}

\item{device}{Device to use. By default, extracted from extension.
\item{device}{Device to use (function or any of the recognized extensions,
e.g. \code{"pdf"}). By default, extracted from filename extension.
\code{ggsave} currently recognises eps/ps, tex (pictex), pdf, jpeg, tiff,
png, bmp, svg and wmf (windows only).}

Expand Down Expand Up @@ -53,6 +54,12 @@ ggsave("ratings.pdf", width = 20, height = 20, units = "cm")

unlink("ratings.pdf")
unlink("ratings.png")

# specify device when saving to a file with unknown extension
# (for example a server supplied temporary file)
file <- tempfile()
ggsave(file, device = "pdf")
unlink(file)
}
}