|
11 | 11 | #'
|
12 | 12 | #' @param filename file name/filename of plot
|
13 | 13 | #' @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 |
15 | 17 | #' @param path path to save plot to (if you just want to set path and not
|
16 | 18 | #' filename)
|
17 | 19 | #' @param scale scaling factor
|
|
32 | 34 | #' ggsave("length-hist.png")
|
33 | 35 | #' ggsave("ratings.pdf", ratings)
|
34 | 36 | #' 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") |
35 | 40 | #' # make twice as big as on screen
|
36 | 41 | #' ggsave("ratings.pdf", ratings, scale=2)
|
37 | 42 | #' }
|
@@ -72,9 +77,22 @@ ggsave <- function(filename = default_name(plot), plot = last_plot(),
|
72 | 77 | default_device <- function(filename) {
|
73 | 78 | pieces <- strsplit(filename, "\\.")[[1]]
|
74 | 79 | ext <- tolower(pieces[length(pieces)])
|
75 |
| - match.fun(ext) |
| 80 | + match_device(ext) |
76 | 81 | }
|
77 | 82 |
|
| 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 | + |
78 | 96 | units <- match.arg(units)
|
79 | 97 | convert_to_inches <- function(x, units) {
|
80 | 98 | x <- switch(units,
|
|
0 commit comments