rsvg R package provides bindings to librsvg-2 to render SVG files into bitmaps directly from R. On Linux systems, you need to install librsvg from
yum before installing the R package from CRAN. On Windows and MacOS this is not needed.
This Mozilla example shows that the SVG specification allows for a
style element, which is also supported in all browsers. Embedded CSS is supported in all versions of librsvg, but the quality of css rendering has improved a lot in recent versions of librsvg.
Recent versions of librsvg can also apply an external CSS file to the SVG. This has the same effect as an external CSS file that is embedded in header of an HTML file.
By using an external CSS, we can split the drawing structure from the styling. The new “css” parameter requires a somewhat recent version of librsvg so it may not work on older Linux systems. It is available on any MacOS or Windows, and the latest versions of Ubuntu, Debian, Fedora, etc.
The examples above write the rendered SVG file directly to a PNG file on disk. But you can also render the SVG to an in-memory bitmap buffer.
## raw [1:4, 1:600, 1:600] 00 00 00 00 ...
bitmap object is an array in R that contain the pixels of the image. You can read it for example using the
magick package for subsequent image editing, or to save it to a standard format.