2
votes

My "download" button does not work as expectation. It opens a new app window every time I click on it. I am wondering why it functions in this way?

download function in server.R:

output$down_load <- downloadHandler(
    # specify the file name
    filename = function() {
          paste('cls_result_export', Sys.Data(),'.csv', sep='')
        },
    # Write the plot back
    content = function(file){
          write.csv(cls_output()$raw_data, file)
        }
)

download function in ui.R:

downloadButton(outputId = "down_load", label = "Download the CLS Raw Data")
2
@user5249203 Hmmm, it still opens new windows even i used "cls_result_export.csv" instead. ;( Also, the cls_output() is an reactive object which returns multiple objects. It should be ok to get "raw_data" object from "cls_output()" with "cls_output()$raw_data".Mark Li
So what is your expectation? I think that window is supposed to open up so as to give the user the chance to rename it. Although it does seem to behave differently in the browser and in the R-Studio viewer, it does not open a window in the Chrome browser for me. If you don't want a window, you could just use an action button to save it.Mike Wise
@MikeWise Thanks for the suggestion of using "action button"! I will give a try. My current "download" button just open a new window for a new session of the same app and there is no download performed in this process.. I don't mind if it just opens a new window for download.. :(Mark Li
The different behaviors in Chrome and in the RStuio viewer makes me think the function is buggy and ill-specified (as the docs do not reveal what it should actually do).Mike Wise
@MikeWise Yea, I see. Got it. In my case, since I have to use the result from an reactive object, I will just use action button with write.csv() instead. Thanks for your help!:)Mark Li

2 Answers

1
votes

Try using an actionButton wired to an observe clause like this:

library(shiny)

ui <- fluidPage(  actionButton("dodo", "Download" ) )

   server <-  function(input, output)
   {
     observe({

       if (input$dodo>0){
          fname <- paste0('cls_result_export', Sys.Date(),'.csv')
          write.csv(mtcars,fname)
       }
     })
}
shinyApp(ui = ui, server = server)
1
votes

Another possible way to try to fix this issue, is to include this line in your server.R script:

outputOptions(output, 'down_load', suspendWhenHidden=FALSE)