0

After several issues trying to implement a simple downloadButton of shiny app I just tried to implement an examplo that I found.

source: https://www.youtube.com/watch?v=Y5arqZ9Bp0A

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("ngear", "Select the gear number", c("1"="cyl","2"="am","3"="gear"))
    ),
    mainPanel(
      plotOutput("plot"),
      downloadButton("report","download"))
  )
)

server <- function(input, output) {
   mtreact <- reactive({
     mtcars[,c("mpg",input$ngear)]
   })

   output$plot <- renderPlot({
      with(mtreact(),boxplot(mpg~mtreact()[,2]))
   })

   output$report <- downloadHandler(
     filename = function(){
       paste("plot","png",sep=".")
     },
     content = function(){
       png(file)
       with(mtreact(),boxplot(mpg~mtreact()[,2]))
       dev.off
     }
   )
}

# Run the application 
shinyApp(ui = ui, server = server)

When I run this code the shiny app run properly. However, when I click on the download button a window is open to save a file named "report" with no extension and no it does not contain a plot as expected.

This is the first time that I try this functionality. Then, anyone see any mistake on the code?

phalteman
  • 3,442
  • 1
  • 29
  • 46
  • 2
    Well, it should be `dev.off()`, not just `dev.off`. Where is `file` defined for the `png(file)`. Maybe you meant to also have `content = function(file){}`? Also, are you running this in a real web browser or with the Rstuido built in browser? I don't think the latter supports file names for downloads (but I might be wrong about that). See: https://stackoverflow.com/questions/14810409/save-plots-made-in-a-shiny-app – MrFlick Jan 10 '19 at 20:46

2 Answers2

0

Your downloadHandler should accept a filename on the content

   output$report <- downloadHandler(
     filename = function(){
       paste("plot","png",sep=".")
     },
     content = function(file){
                        ^^^^
       png(file)
       with(mtreact(),boxplot(mpg~mtreact()[,2]))
       dev.off
     }
   )

Secondly, the filename function returns a suggestion that is used for the download dialogbox. This however is not executed properly in RStudio's app viewer (the one that appears when you in RStudio clicks 'Run App'). Try running the app in a proper browser like Chrome to check that it responds correctly.

MrGumble
  • 5,631
  • 1
  • 18
  • 33
0

As MrGumble and MrFlick have pointed out the code included two mistakes: function(file) and dev.of**()**

The following code generates the report as expected.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("ngear", "Select the gear number", c("1"="cyl","2"="am","3"="gear"))
    ),
    mainPanel(
      plotOutput("plot"),
      downloadButton("report","download"))
  )
)

server <- function(input, output) {
   mtreact <- reactive({
     mtcars[,c("mpg",input$ngear)]
   })

   output$plot <- renderPlot({
      with(mtreact(),boxplot(mpg~mtreact()[,2]))
   })

   output$report <- downloadHandler(
     filename = function(){
       paste("plot","png",sep=".")
     },
     content = function(file){
       png(file)
       with(mtreact(),boxplot(mpg~mtreact()[,2]))
       dev.off()
     }
   )
}

Thanks to MrFlick and MrGumble for the help.