Shiny svg ggplots?

855 views
Skip to first unread message

Andrew Clancy

unread,
Jul 24, 2015, 12:38:40 PM7/24/15
to Shiny - Web Framework for R
Hi all, 

Can shiny output ggplots as svg easily, rather than png? 
I'm using the interactive ggplot features, so svgPanZoom doesnt help (but is cool, and could also be useful). 



Andrew Clancy

unread,
Jul 24, 2015, 12:41:00 PM7/24/15
to Shiny - Web Framework for R, ni...@achren.org
Planning to use ggvis/rCharts etc as well, I just need to keep all avenues open, as there'll always be ggplots generated by our R users. 

Amber James

unread,
Jul 27, 2015, 2:42:30 AM7/27/15
to Shiny - Web Framework for R, ni...@achren.org
This isn't really a shiny question but more of a ggplot question. 
Here is a link to a stack overflow page pertaining to this :Link

The only issue you might have is with "headless server" error when saving. I ran into those too when I first started on shiny. 
Copying the code from the link above with adjustments
require("ggplot2")
shinyServer
(function(input, output, session) {
values <- reactiveValues(
    plot = NULL
)
values$plot
<-
 qplot(clarity, data=diamonds, fill=cut, geom="bar")
output$plot
<- renderPlot(values$plot)
observeEvent(input$savePlot, {
    image
=values$plot
    ggsave
(file="test.svg", plot=image, width=10, height=8)
})
}

You need to pass the reactive values to the saving function and not the output plot. You then need to ensure that you define the width and height because it will not render anything if you don't specify. 

Hope this helps a bit. 

Amber

Andrew Clancy

unread,
Jul 27, 2015, 5:30:05 AM7/27/15
to Shiny - Web Framework for R, amber....@gmail.com
Thanks Amber - this isnt quite what I'm looking for - I'm fine with saving ggplot2 as svg/pdf, however I'm keen for a 1-step way to render as svg rather than png specifically in shinyR - ie. it would be a hack to save (say to the www folder) then render an image tag etc. 

In an ideal world, this would simply be an option in plotOutput or renderPlot - eg. a single attribute change in something like this http://shiny.rstudio.com/gallery/telephones-by-region.html
- A
Reply all
Reply to author
Forward
0 new messages