|
7 | 7 | #'
|
8 | 8 | #' @param filename File name to create on disk.
|
9 | 9 | #' @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. |
11 | 12 | #' \code{ggsave} currently recognises eps/ps, tex (pictex), pdf, jpeg, tiff,
|
12 | 13 | #' png, bmp, svg and wmf (windows only).
|
13 | 14 | #' @param path Path to save plot to (combined with filename).
|
|
34 | 35 | #'
|
35 | 36 | #' unlink("ratings.pdf")
|
36 | 37 | #' 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) |
37 | 44 | #' }
|
38 | 45 | ggsave <- function(filename, plot = last_plot(),
|
39 | 46 | device = default_device(filename), path = NULL, scale = 1,
|
@@ -65,9 +72,22 @@ ggsave <- function(filename, plot = last_plot(),
|
65 | 72 | default_device <- function(filename) {
|
66 | 73 | pieces <- strsplit(filename, "\\.")[[1]]
|
67 | 74 | 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 | + } |
68 | 84 | match.fun(ext)
|
69 | 85 | }
|
70 | 86 |
|
| 87 | + if (is.character(device)) { |
| 88 | + device <- match_device(device) |
| 89 | + } |
| 90 | + |
71 | 91 | dim <- plot_dim(c(width, height), scale = scale, units = units,
|
72 | 92 | limitsize = limitsize)
|
73 | 93 |
|
|
0 commit comments