Skip to contents

save_png(), save_svg(), and save_pdf() render a drawable or sketch straight to a raster (PNG) or vector (SVG/PDF) file, without the caller needing to manage a grDevices device by hand. Each is a thin wrapper: open the appropriate device, call draw(), and always close the device afterward (via on.exit(), so a device is never left open even if draw() itself errors).

Usage

save_png(
  object,
  filename,
  width = 7,
  height = 7,
  dpi = 300,
  bg = "white",
  units = "in",
  ...
)

save_svg(object, filename, width = 7, height = 7, bg = "white", ...)

save_pdf(object, filename, width = 7, height = 7, bg = "white", ...)

Arguments

object

A drawable or sketch object.

filename

A single string, the path to write to.

width, height

Image dimensions, in units for save_png() (default inches) or always in inches for save_svg()/save_pdf(), which have no units argument of their own. Default 7.

dpi

Resolution in dots per inch. Only meaningful for save_png() (a raster format); ignored by save_svg()/save_pdf(), which are drawn at vector resolution. Default 300.

bg

Background colour passed to the underlying device, e.g. "white" (the default) or "transparent". This is the device's own page colour, independent of any canvas() background a sketch itself already carries – the two compose, so a transparent bg here still shows an opaque canvas() background underneath, and vice versa a sketch with no canvas() background shows bg through any of its own shapes that don't fully cover the page.

units

One of "in" (the default), "cm", "mm", or "px", forwarded to grDevices::png()'s own units argument – only meaningful for save_png(); save_svg()/save_pdf() have no units argument, since grDevices::svg()/grDevices::pdf() always take width/height in inches.

...

Passed on to draw(), e.g. xlim/ylim.

Value

filename, invisibly.

Examples

file <- tempfile(fileext = ".png")
save_png(shape_circle(radius = 1), file)

file <- tempfile(fileext = ".svg")
save_svg(shape_circle(radius = 1), file)

file <- tempfile(fileext = ".pdf")
save_pdf(shape_circle(radius = 1), file)