-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
quick_save_plot()
convenience function, based on TJ Mahr's `ggp…
…review()` function.
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#' Save and preview a plot | ||
#' | ||
#' @description Convenience function to quickly save a plot to an image file and immediately inspect it. Based on the function `ggpreview` by Tristan (TJ) Mahr. | ||
#' | ||
#' @param plot A plot object. If unspecified, uses the most recent printed plot. | ||
#' @param fname A filename for the plot to be saved. Uses a temporary file in the user's temporary folder if unspecified. | ||
#' @param inspect A logical defaulting to `TRUE` indicating whether to immediately open the file for inspection. | ||
#' @param device Device to use. Can either be a device function (e.g. png()), or one of "eps", "ps", "tex" (pictex), "pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only). Defaults to 'png'. | ||
#' @param dpi Plot resolution. Also accepts a string input: "retina" (320), "print" (300), or "screen" (72). Applies only to raster output types. | ||
#' @param scale Multiplicative scaling factor. | ||
#' @param \dots Additional arguments passed on to the `ggsave` function. | ||
#' @return Saves a plot to a temporary file in the user's temp directory | ||
#' @export | ||
#' | ||
#' @examples | ||
#' ggplot(mtcars, aes(mpg, wt)) + geom_point() | ||
#' quick_save_plot() | ||
|
||
quick_save_plot <- function(plot = ggplot2::last_plot(), | ||
fname = tempfile(fileext = paste0(".", device)), | ||
inspect = TRUE, | ||
device = "png", dpi = 320, scale = 1, ...) { | ||
|
||
fname <- tempfile(fileext = paste0(".", device)) | ||
ggplot2::ggsave(plot = plot, | ||
filename = fname, | ||
device = device, | ||
dpi = dpi, scale = scale, | ||
...) | ||
if (inspect) { | ||
system2("open", fname) | ||
} | ||
invisible(NULL) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.