Skip to content

Commit d5b77f7

Browse files
Sebastian Kopfsebkopf
authored andcommitted
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 f400a20 commit d5b77f7

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ ggplot2 1.0.1.9000
33

44
* `theme_minimal()` got slightly more minimal by removing the axis ticks:
55
labels now line up beneath the grid lines (@tomschloss, #1084)
6+
* Parameter `device` now supports character argument to specify which supported
7+
device to use ('pdf', 'png', 'jpeg', etc.), for when it cannot be correctly
8+
inferred from the file extension (for example when a temporay filename is
9+
supplied server side in shiny apps) (@sebkopf, #939)
10+
11+
* New `aes_q()` function to generate aesthetic specifications from
12+
quoted calls/names. `aes_string()` uses names `x` and `y` for first
13+
two unnamed arguments.
614

715
* `ggsave()` has been simplified a little to make it easier to maintain.
816
It no longer checks that you're printing a ggplot2 object (so now also

R/save.r

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#'
88
#' @param filename File name to create on disk.
99
#' @param plot Plot to save, defaults to last plot displayed.
10-
#' @param device Device to use. By default, extracted from extension.
10+
#' @param device Device to use (function or any of the recognized extensions,
11+
#' e.g. \code{"pdf"}). By default, extracted from filename extension.
1112
#' \code{ggsave} currently recognises eps/ps, tex (pictex), pdf, jpeg, tiff,
1213
#' png, bmp, svg and wmf (windows only).
1314
#' @param path Path to save plot to (combined with filename).
@@ -34,6 +35,12 @@
3435
#'
3536
#' unlink("ratings.pdf")
3637
#' unlink("ratings.png")
38+
#'
39+
#' # specify device when saving to a file with unknown extension
40+
#' # (for example a server supplied temporary file)
41+
#' file <- tempfile()
42+
#' ggsave(file, device = "pdf")
43+
#' unlink(file)
3744
#' }
3845
ggsave <- function(filename, plot = last_plot(),
3946
device = default_device(filename), path = NULL, scale = 1,
@@ -65,9 +72,22 @@ ggsave <- function(filename, plot = last_plot(),
6572
default_device <- function(filename) {
6673
pieces <- strsplit(filename, "\\.")[[1]]
6774
ext <- tolower(pieces[length(pieces)])
75+
match_device(ext)
76+
}
77+
78+
match_device <- function(ext) {
79+
if(!exists(ext, mode = "function")) {
80+
stop("No graphics device defined for the file extension '", ext, "'. ",
81+
"Make sure to specify a filename with supported extension or ",
82+
"set the device parameter.", call. = FALSE)
83+
}
6884
match.fun(ext)
6985
}
7086

87+
if (is.character(device)) {
88+
device <- match_device(device)
89+
}
90+
7191
dim <- plot_dim(c(width, height), scale = scale, units = units,
7292
limitsize = limitsize)
7393

man/ggsave.Rd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ ggsave(filename, plot = last_plot(), device = default_device(filename),
1313

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

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

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

5455
unlink("ratings.pdf")
5556
unlink("ratings.png")
57+
58+
# specify device when saving to a file with unknown extension
59+
# (for example a server supplied temporary file)
60+
file <- tempfile()
61+
ggsave(file, device = "pdf")
62+
unlink(file)
5663
}
5764
}
5865

0 commit comments

Comments
 (0)