3

Is there anyway to plot SVG to memory buffer instead of file?

I'm trying to embed R into my own app, so I can use R to generate svg charts. I would like to avoid generating file and then read svg file to my app.

I found this In R, how to plot into a memory buffer instead of a file?, but it's for png format.

It would be great if we can do something similar to svg and save svg content to a string variable

Thanks!

Community
  • 1
  • 1
jerry
  • 1,196
  • 2
  • 9
  • 21

1 Answers1

3

The gridSVG package might be useful:

library(ggplot2)
library(gridSVG)


ggplot(iris, aes(Species, Sepal.Length)) + geom_point()

SVGlist <- grid.export(name = NULL)
str(SVGlist, 1)
#List of 4
# $ svg     :Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 
# $ coords  :List of 18
# $ mappings:List of 5
# $ utils   : chr "// Note that this code is documented using JSDoc and guided by the following URLs:\n// http://code.google.com/p/jsdoc-toolkit/w"| _truncated__

SVGlist$svg
#the SVG code

However, this still needs to print to the graphics device.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • thanks a lot, @Roland! this is what i need, i'm planning to send the svg code to web browser. ggplot(iris, aes(Species, Sepal.Length)) + geom_point() this statement actually opened a window to display the plot, is there anyway to supress it, but still generate the svg? i want this to work on the server side. – jerry Jun 05 '15 at 12:07
  • 2
    the new `svglite` package https://github.com/hadley/svglite offers this functionality with `svgstring()`. – timelyportfolio Feb 19 '16 at 02:59