18

I can't figure out what's going on - everything seems to work but my app does not generate a file - although it looks like it does. I run it in Windows, on RStudio 0.98.125, and I run it using the line: runApp() Below is a very simple reproducible example:

my 'ui.R':

shinyUI(pageWithSidebar(

  headerPanel("My App"),

  sidebarPanel(
    numericInput('NumRuns','Number of runs',value=3,min=3,max=10,step=1),

    actionButton(inputId="goButton","Run!"),

    textInput("downloadData","Save My Data Frame:",value="Data Frame 1"),
    downloadButton('downloadData','Save my file!')

  ),

  mainPanel(
    tabPanel("Some Text",
             h4(textOutput("caption2")),
             tableOutput("mydf"),
             value=3))
  ))

my 'server.R':

shinyServer(function(input,output){

  # Creating files for download at the end

  myout = reactive({
    if(input$goButton==0) return(NULL)

      nrruns=input$NumRuns
      mylist=NULL
      for(i in 1:nrruns){
        mylist[[i]]<-data.frame(a=rnorm(10),b=runif(10))
        names(mylist)[i]<-paste("dataframe",i,sep="")
      }
      return(mylist)
  })

     output$mydf <- renderTable({
     if(input$goButton==0) return(NULL)
     input$goButton
     isolate(
       myout()$dataframe1
     )
   })

  output$downloadData <- downloadHandler(
    filename = function() { paste(input$downloadData, " ",Sys.Date(),".csv",sep="") },
    content = function(file) {
      write.csv(myout()$dataframe1,file,row.names=F)
    }
  )

})
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user3245256
  • 1,842
  • 4
  • 24
  • 51

3 Answers3

60

Note the download button does not work in the RStudio viewer. Your friend might be using the RStudio viewer to view the app. If that is the case, please open the app in the external web browser (there is a drop-down list on the right of the "Run App" button: Run in Window, Run in Viewer Pane, Run External; choose the last one).

Mustansar Fiaz
  • 769
  • 1
  • 5
  • 7
  • 2
    It took me way too long figuring this out, thanks m) – Jemus42 Oct 14 '16 at 13:22
  • 5
    This is still a thing (Jan 2017)! A file will download, but the result from filename=function() is not used by the RStudio viewer to save the file (at least not in Linux). – FvD Jan 27 '17 at 15:41
  • 1
    Now it's december 2018 and I ended up here after 2 hours of looking for what went wrong downloading my file! This answer should be at the top. – David Jorquera Dec 12 '18 at 15:46
  • 1
    October 2020, still a thing! – rvrvrv Oct 25 '20 at 10:32
13

The example provided works fine for downloading CSV in my tests (if it is from the webbrowser that is, using Run app within RStudio did cause same issue)

Note that if you keep getting something like "download.html" from your download button instead of the downloaded content, you must make sure the ID from downloadButton("myIdHere", ...) matches output$myIdHere = downloadHandler("output.csv", ...)

Also note that if you are using shiny modules (you probably would know if you are using this), then you want to use downloadButton(ns("myIdHere"),...) and then you still have output$myIdHere

Colin D
  • 2,822
  • 1
  • 31
  • 38
-4

Adding to what Colin D said. It can just be that your idNAME is too long.

I just tested on my app that THIS WORKS:

     output$download_mastergroup <- downloadHandler(...)   #server side
     downloadButton('download_mastergroup ', 'Download overview') #ui side

and that THIS DOES NOT WORK:

     output$download_mastergroup_overview <- downloadHandler(...)   #server side
     downloadButton('download_mastergroup_overview ', 'Download overview') #ui side

So, be concise and it will be allright!

DaveR
  • 1,696
  • 18
  • 24