0

I am trying to download the graph I made in Shiny, I found a Stackoverflow post about this subject here. However, when I run the code from the answer, all seeems to work fine, exept that I can't open the graphs once they are "saved". I can't see them in the folder I saved them in and when I try to open them from my recent files, an error "file not found" pops up.

This is the code I'm using:

library(shiny)
library(ggplot2)
runApp(list(
#ui
  ui = fluidPage(downloadButton('downloadPlot')),

#server
server = function(input, output) {
   datasetInput <- reactive({
switch(input$dataset,
       "rock" = rock,
       "pressure" = pressure,
       "cars" = cars)
 })

plotInput <- reactive({
  df <- datasetInput()
  p <-ggplot(df, aes_string(x=names(df)[1], y=names(df)[2])) +
  geom_point()
 })
output$downloadPlot <- downloadHandler(
filename = function() { paste(input$dataset, '.png', sep='') },
content = function(file) {
    ggsave(file, plot = plotInput(), device = "png")
}
)
}
))
Prradep
  • 5,506
  • 5
  • 43
  • 84
Hav11
  • 409
  • 1
  • 5
  • 14
  • It works everything fine for me when I define on the client side a missing input in downloadHandler: `selectInput("dataset", "Choose a dataset:", choices = c("rock", "pressure", "cars"))` – Michal Majka Aug 17 '16 at 14:56
  • I changed `input$dataset` to `input$filename` which is a `textInput` in my UI, but it still doesn't work – Hav11 Aug 19 '16 at 07:38

2 Answers2

2

Alternatively, you can use plotly which supplies a download possibility without further configurations (download button is in the upper right corner of the graph):

library(shiny)
library(plotly)
runApp(list(
  #ui
  ui = fluidPage(selectInput("dataset", "Choose a dataset:", choices = c("rock", "pressure", "cars")),
                 plotlyOutput('plot')),

  #server
  server = function(input, output) {
    datasetInput <- reactive({
      switch(input$dataset,
             "rock" = rock,
             "pressure" = pressure,
             "cars" = cars)
    })

    output$plot <- renderPlotly({
      df <- datasetInput()
      ggplot(df, aes_string(x=names(df)[1], y=names(df)[2])) +
        geom_point()

      ggplotly()
    })

  }
))
shosaco
  • 5,915
  • 1
  • 30
  • 48
1

I tried to replicate your code with textInput and this works fine for me.

library(shiny)
library(ggplot2)
runApp(list(

#ui
 ui = fluidPage(downloadButton('downloadPlot'),
                textInput("filename", "Choose a dataset:")),

#server
 server = function(input, output) {
  datasetInput <- reactive({
   switch(input$filename,
          "rock" = rock,
          "pressure" = pressure,
          "cars" = cars)
  })

  plotInput <- reactive({
   df <- datasetInput()
   p <- ggplot(df, aes_string(x=names(df)[1], y=names(df)[2])) +
        geom_point()
  })
  output$downloadPlot <- downloadHandler(
   filename = function() { paste(input$filename, '.png', sep='') },
   content = function(file) {
     ggsave(file, plot = plotInput(), device = "png")
   }
  )
 }
))