download heatmap as png from shiny app

Hi,
I'm trying to make an app where the user can download a heatmap as png.
I've been trying to achieve this with ggsave but it's not working.

An example:

library(shiny)
library(ggplot2)
library(gplots)
runApp(list(
  ui = fluidPage(downloadButton('foo'),
                 plotOutput("my_plot")
                 ),
  server = function(input, output) {
    my_plot <- reactive({
      x  <- as.matrix(mtcars)
      heatmap.2(x)    
    })
    output$my_plot <- renderPlot({
      my_plot()
    })
    output$foo = downloadHandler(
      filename = 'test.png',
      content = function(file) {
        device <- function(..., width, height) {
          grDevices::png(..., width = width, height = height,
                         res = 300, units = "in")
        }
        ggsave(file, plot = eval(my_plot()$call), device = device)
      })
  }
))

Anybody have an idea?

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.