Skip to content

Commit 8149ae2

Browse files
author
Sebastian Kopf
committed
device parameter supports character argument
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)
1 parent ca4c732 commit 8149ae2

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
ggplot2 0.9.3.1.99
22
----------------------------------------------------------------
33

4+
* Parameter `device` now supports character argument to specify which supported
5+
device to use ('pdf', 'png', 'jpeg', etc.), for when it cannot be correctly
6+
inferred from the file extension (for example when a temporay filename is
7+
supplied server side in shiny apps) (@sebkopf, #939)
8+
49
* New `aes_q()` function to generate aesthetic specifications from
510
quoted calls/names. `aes_string()` uses names `x` and `y` for first
611
two unnamed arguments.

R/save.r

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
#'
1212
#' @param filename file name/filename of plot
1313
#' @param plot plot to save, defaults to last plot displayed
14-
#' @param device device to use, automatically extract from file name extension
14+
#' @param device device to use, can be a function or any of the currently
15+
#' recognized extensions (\code{"pdf"}, \code{"jpeg"}, etc.), defaults to
16+
#' automatically inferring the correct device from the file name
1517
#' @param path path to save plot to (if you just want to set path and not
1618
#' filename)
1719
#' @param scale scaling factor
@@ -32,6 +34,9 @@
3234
#' ggsave("length-hist.png")
3335
#' ggsave("ratings.pdf", ratings)
3436
#' ggsave("ratings.pdf", ratings, width=4, height=4)
37+
#' # specify device when saving to a file with unknown extension
38+
#' # (for example a server supplied temporary file)
39+
#' ggsave(tempfile(), device = "pdf")
3540
#' # make twice as big as on screen
3641
#' ggsave("ratings.pdf", ratings, scale=2)
3742
#' }
@@ -72,9 +77,22 @@ ggsave <- function(filename = default_name(plot), plot = last_plot(),
7277
default_device <- function(filename) {
7378
pieces <- strsplit(filename, "\\.")[[1]]
7479
ext <- tolower(pieces[length(pieces)])
75-
match.fun(ext)
80+
match_device(ext)
7681
}
7782

83+
match_device <- function(ext) {
84+
if(!exists(ext, mode = "function")) {
85+
stop("No graphics device defined for the file extension '", ext, "'. ",
86+
"Make sure to specify a filename with supported extension or ",
87+
"set the device parameter.")
88+
}
89+
match.fun(ext)
90+
}
91+
92+
if (is.character(device)) {
93+
device <- match_device(device)
94+
}
95+
7896
units <- match.arg(units)
7997
convert_to_inches <- function(x, units) {
8098
x <- switch(units,

man/ggsave.Rd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ ggsave(filename = default_name(plot), plot = last_plot(),
1313

1414
\item{plot}{plot to save, defaults to last plot displayed}
1515

16-
\item{device}{device to use, automatically extract from file name extension}
17-
16+
\item{device}{device to use, can be a function or any of
17+
the currently recognized extensions (\code{"pdf"},
18+
\code{"jpeg"}, etc.), defaults to automatically inferring
19+
the correct device from the file name}
20+
1821
\item{path}{path to save plot to (if you just want to set path and not
1922
filename)}
2023

@@ -53,6 +56,9 @@ ggsave("length-hist.pdf")
5356
ggsave("length-hist.png")
5457
ggsave("ratings.pdf", ratings)
5558
ggsave("ratings.pdf", ratings, width=4, height=4)
59+
# specify device when saving to a file with unknown extension
60+
# (for example a server supplied temporary file)
61+
ggsave(tempfile(), device = "pdf")
5662
# make twice as big as on screen
5763
ggsave("ratings.pdf", ratings, scale=2)
5864
}

0 commit comments

Comments
 (0)