Save a ggplot to a folder, skipping plots that were never built
Source:R/io-export.R
ggsave_rawr.RdLooks up input by the name it was called with (via deparse(substitute()))
and only calls ggplot2::ggsave() if an object of that name exists in the
calling environment — this lets report chunks unconditionally call
ggsave_rawr(p_some_plot, "some_plot", folder = FolderFigures) even when
p_some_plot was conditionally skipped upstream (e.g. no non-NA correlation
to plot), without erroring.
Arguments
- input
The plot object to save. When it is a bare variable name, that name is used to check existence rather than the value.
- file
Output file name, without extension; saved as
<file>.pngunderfolder.- folder
Destination directory for the png.
- width, height
Figure dimensions in cm, passed to
ggplot2::ggsave().
Details
Reconciles two near-duplicate report-inline variants (one hardcoding
"4_figures", the other a width = n_conditions + 5 default and a
chunk-local FolderFigures) into a single canonical version that takes
folder/width/height as ordinary explicit arguments — callers compute
and pass whatever values their report context needs.
A bare symbol is looked up by name in the calling environment; any other
expression (plots[[nm]], a computed plot) is evaluated and judged by its
value, so the idiomatic
for (nm in names(plots)) ggsave_rawr(plots[[nm]], file = nm, ...) export
loop works as well.