
svglite is a graphics device that produces clean svg output, suitablefor use on the web, or hand editing. Compared to the built-insvg(), svglite produces smaller files, and leaves text asis, making it easier to edit the result after creation. It also supportsmultiple nice features such as embedding of web fonts.
svglite is available on CRAN usinginstall.packages("svglite"). You can install thedevelopment version from github with:
# install.packages("pak")pak::pak("r-lib/svglite")The grDevices package bundled with R already comes with an SVG device(using the eponymoussvg() call). The development ofsvglite is motivated by the following considerations:
svglite() is considerably faster thansvg(). If you are rendering SVGs dynamically to serve overthe web this can be quite important:
library(svglite)x<-runif(1e3)y<-runif(1e3)tmp1<-tempfile()tmp2<-tempfile()svglite_test<-function() {svglite(tmp1)plot(x, y)dev.off()}svg_test<-function() {svg(tmp2,onefile =TRUE)plot(x, y)dev.off()}bench::mark(svglite_test(),svg_test(),min_iterations =250,check =FALSE)#> # A tibble: 2 × 6#> expression min median `itr/sec` mem_alloc `gc/sec`#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>#> 1 svglite_test() 2.86ms 2.97ms 325. 709KB 5.28#> 2 svg_test() 6.11ms 6.26ms 159. 224KB 0.638Another point with high relevance when serving SVGs over the web isthe size.svglite() produces much smaller files
# svglitefs::file_size(tmp1)#> 75K# svgfs::file_size(tmp2)#> 327KIn both cases, compressing to make.svgz (gzipped svg)is worthwhile. svglite supports compressed output directly which will betriggered if the provided path has a".svgz" (or".svg.gz") extension.
tmp3<-tempfile(fileext =".svgz")svglite(tmp3)plot(x, y)invisible(dev.off())# svglite - svgzfs::file_size(tmp3)#> 9.45KOne of the main reasons for the size difference between the size ofthe output ofsvglite() andsvg() is the factthatsvglite() encodes text as styled<text> elements, whereassvg() convertsthe glyphs to polygons and renders these. The latter approach means thatthe output ofsvg() does not require the font to be presenton the system that displays the SVG but makes it more or less impossibleto edit the text after the fact. svglite focuses on providing maximaleditability of the output, so that you can open up the result in avector drawing program such as Inkscape or Illustrator and polish theoutput if you so choose.
svglite uses systemfonts for font discovery which means that allinstalled fonts on your system is available to use. The systemfontsfoundation means that fonts registered withregister_font()orregister_variant() will also be available, as will fontsadded withadd_fonts() andrequire_font(). Ifany of these contains non-standard weights or OpenType features(e.g. ligatures or tabular numerics) this will be correctly encoded inthe style block. systemfonts also allows you to embed webfont@imports in your file to ensure that the file looks asexpected even on systems without the used font installed. Usingsystemfonts::fonts_as_import() you can generate theseimports automatically, optionally embedding the font data directly inthe file.
This section is only relevant for building svglite from scratch,as opposed to installing from a pre-built package on CRAN.
Building svglite requires the system dependency libpng. As svglitedoesn’t have any build-time configuration, your R configuration mustpoint to libpng’sinclude andlib folders.
For instance on macOS, install libpng with:
brew install libpngAnd make sure your~/.R/Makevars knows about Homebrew’sinclude andlib folders where libpng shouldnow be installed. On arm64 hardware, this would be:
CPPFLAGS+= -I/opt/homebrew/includeLDFLAGS+= -L/opt/homebrew/libPlease note that the svglite project is released with aContributor Codeof Conduct. By contributing to this project, you agree to abide byits terms.