jerry
jerry

Reputation: 1261

In R, how to plot in SVG format and output to memory buffer instead of file

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!

Upvotes: 3

Views: 1943

Answers (1)

Roland
Roland

Reputation: 132706

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.

Upvotes: 3

Related Questions