0

I have some svg images that I want to export as png files, using r. Is there a function that can do that? Until now I only found function that export r plots to png.

But how could I export e.g. this example as a png?

<svg height="100" width="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg> 
rdatasculptor
  • 8,112
  • 14
  • 56
  • 81
  • Any specific reason to use R? There are several free online or standalone tools out there to do this. – Molx Dec 06 '15 at 20:01
  • I use rmarkdown to knit pdf's. Somewhere in the proces I generate through the html widget diagrammeR svg images that I want to be embedded in the pdf files. Somehow rmarkdown doesn't put the svg images in the document. – rdatasculptor Dec 06 '15 at 20:15
  • The newest `knitr` on Github will automatically try to convert `htmlwidgets` to static graphs. See https://github.com/yihui/knitr/blob/master/NEWS.md for more information. – timelyportfolio Apr 19 '16 at 12:37

3 Answers3

6

rsvg https://github.com/jeroenooms/rsvg is designed to specifically to do this, and I'll say it does it very quickly.

timelyportfolio
  • 6,479
  • 30
  • 33
3

The convertGraph R package can convert graphical file formats within R and does not even require RStudio. So any SVG file can be exported to PNG, PDF, JPEG, GIF, ... and you also have control over the resolution of the exported image.

In addition, it can convert other file formats to one another (e.g. PNG to PDF, PNG to JPEG, GIF to PNG, etc...). The package is hosted on CRAN as well as GitHub.

2

The package convertGraph may be the solution here. It claims to convert SVG to other image formats (PNG, JPG, etc.). It requires phantomjs for the conversion.

Ben
  • 41,615
  • 18
  • 132
  • 227