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
- filename
A single string, the path to write to.
- width, height
Image dimensions, in
unitsforsave_png()(default inches) or always in inches forsave_svg()/save_pdf(), which have nounitsargument of their own. Default7.- dpi
Resolution in dots per inch. Only meaningful for
save_png()(a raster format); ignored bysave_svg()/save_pdf(), which are drawn at vector resolution. Default300.- 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 anycanvas()backgroundasketchitself already carries – the two compose, so a transparentbghere still shows an opaquecanvas()background underneath, and vice versa asketchwith nocanvas()background showsbgthrough any of its own shapes that don't fully cover the page.- units
One of
"in"(the default),"cm","mm", or"px", forwarded togrDevices::png()'s ownunitsargument – only meaningful forsave_png();save_svg()/save_pdf()have nounitsargument, sincegrDevices::svg()/grDevices::pdf()always takewidth/heightin inches.- ...
Passed on to
draw(), e.g.xlim/ylim.
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)