Skip to contents

Writes the page at its exact size: PDF through the Cairo device with embedded TrueType fonts, PNG through 'ragg' at the style's resolution (falling back to the 'png' Cairo device when 'ragg' is absent).

Usage

SaveFigure(figure, pdf = NULL, png = NULL, dpi = NULL)

Arguments

figure

A ComposeFigure() result.

pdf

Output PDF path, or NULL.

png

Output PNG path, or NULL.

dpi

Positive finite raster resolution; defaults to the style's.

Value

The output paths, invisibly.

Details

With 'ragg', the page is drawn on a new capture device for every save and the captured pixels are written by ragg::agg_png(). 'gridtext' keeps rich text metrics for the session under the device name, not its resolution, so a fresh device name makes the PNG independent of what was drawn before in the session. Without 'ragg', PNGs saved at different resolutions in one session can place rich text a pixel apart.

Both outputs are rendered to temporary files beside their destinations before replacing either destination. A rendering failure leaves existing files unchanged. If replacement fails, previous files are restored where the file system permits it; any backup that cannot be restored is retained and its path is reported. This is recovery from reported errors, not a transaction across process termination or a concurrent writer. Destinations must be distinct files in existing directories.

Examples

p <- ggplot2::ggplot(data.frame(x = 1:3, y = c(2, 1, 3)), ggplot2::aes(x, y)) +
  ggplot2::geom_point()
fig <- ComposeFigure(list(FigurePanel(p, c(10, 8, 50, 30), letter = "a")),
                     height_mm = 45, width_mm = 70)
if (capabilities("cairo")) {
  out <- SaveFigure(fig, pdf = tempfile(fileext = ".pdf"),
                    png = tempfile(fileext = ".png"), dpi = 72)
  file.exists(out)
}
#> [1] TRUE TRUE