Shiny: export dynamic graph to image?

I use brilliant and I would like to create a download button that saves the current chart as an image. it works:

  output$downloadPlot <- downloadHandler(
    filename <- function() {
      paste(input$group,'-top6_plot', Sys.Date(),'.png',sep='') },
    content <- function(file) {
      png(file, width = 980, height = 400, units = "px", pointsize = 12,
          bg = "white", res = NA)

      plot(sin, -pi, 2*pi)

      dev.off()},
    contentType = 'image/png'
  )

But I use dygraphs for a dyanmic plot, and this creates an empty white image:

  output$downloadPlot <- downloadHandler(
    filename <- function() {
      paste(input$group,'-top6_plot', Sys.Date(),'.png',sep='') },
    content <- function(file) {
      png(file, width = 980, height = 400, units = "px", pointsize = 12,
          bg = "white", res = NA)

      ReshapedVariables<-variablesForPlot()

      if(input$timeframe == 1){
        Title ="Timeframe: 1 Month"
      } else if(input$timeframe==2){
        Title ="Timeframe: 3 Months"
      } else if(input$timeframe==3){
        Title ="Timeframe: 6 Months"
      } else if(input$timeframe==4){
        Title ="Timeframe: Year to date"
      } else if(input$timeframe==5){
        Title ="Timeframe: 3 Years"
      } else if(input$timeframe==6){
        Title ="Timeframe: All"
      } else {
        Title ="Timeframe: Year to date"
      }

      dygraph(ReshapedVariables, main=Title) %>%
        #dyLegend(width = 200, labelsSeparateLines = TRUE, labelsDiv="VariablePlotLegend", show="always")  %>%
        dyLegend(labelsSeparateLines = FALSE, labelsDiv="VariablePlotLegend", show="always") %>%
        dyOptions(strokeWidth=2, axisLineColor=GRAPH_BLUE, axisLabelColor=GRAPH_BLUE, gridLineWidth=0.1)


      dev.off()},
    contentType = 'image/png'
  )

But the plot code for dygraphs works in general ... because in a brilliant web application it displays the graph correctly.

+4
source share
1 answer

This question is quite old, but perhaps this answer may be useful to you or someone else. This question is directly related to this question: How to save the Flyer in the map R as a png or jpg file? . I had the same problem as mine, and here is how I solved it for my case:

"htmlwidgets" "webshot". ,

library(htmlwidgets)
library(webshot)

PhantomJS. ,

. ReshapedVariables , :

dyplot <- function(){
dygraph(ReshapedVariables(), main=Title) %>%
labelsDiv="VariablePlotLegend", show="always")  %>%
dyLegend(labelsSeparateLines = FALSE,  labelsDiv="VariablePlotLegend",show="always") %>%
dyOptions(strokeWidth=2, axisLineColor=GRAPH_BLUE, axisLabelColor=GRAPH_BLUE, gridLineWidth=0.1)}

:

output$downloadData <- downloadHandler(
filename = function () {paste(input$group,'-top6_plot', Sys.Date(),'.png',sep='') },
content = function(file) {
  saveWidget(dyplot(), "temp.html", selfcontained = FALSE)
  webshot("temp.html", file = file)
}
 )
0

All Articles