A thin wrapper around ggplot2::ggsave()
that takes a plot as the first
argument and filename
as the second (the reverse of ggplot2::ggsave()
),
but that doesn't save the plot when called interactively. Works best inside R
Markdown documents, allowing you to view the figure when running the code
chunk interactively but ensure that a specific copy of the figure is saved to
a specific directory during the markdown rendering.
ggsave_and_print( plot, filename, width = 20, height = 15, device = fs::path_ext(filename), include = knitr::opts_current$get("include") %||% TRUE, ..., force_save = FALSE, base_dir = getwd() )
plot | Plot to save, defaults to last plot displayed. |
---|---|
filename | File name to create on disk. |
width | Plot size in |
height | Plot size in |
device | Device to use. Can either be a device function
(e.g. |
include | Mimics the knitr |
... | Additional arguments passed on to |
force_save | If |
base_dir | Save figures into a specified directory |
if (FALSE) { iris %>% { ggplot(.) + aes(Sepal.Length, Sepal.Width, color = Species) + geom_point() } %>% ggsave_and_print("iris_plot.png", width = 10, height = 6) }