Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny app: downloadHandler does not produce a file

Tags:

r

shiny

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)
    }
  )

})
like image 409
user3245256 Avatar asked Sep 22 '14 22:09

user3245256


People also ask

Can you save shiny app as HTML?

You need an R server in order to execute a shiny app. There is no way to convert it into "pure HTML" and run the interactivity via javascript. The reason for that is that shiny apps will have to execute R code at runtime and javascript does not know how to deal with that.

Where is the shiny app directory?

Shiny apps are contained in a single script called app. R . The script app. R lives in a directory (for example, newdir/ ) and the app can be run with runApp("newdir") .

How do you enter shiny data?

To add an input in a Shiny app, we need to place an input function *Input() in the ui object. Each input function requires several arguments. The first two are inputId , an id necessary to access the input value, and label which is the text that appears next to the input in the app.


1 Answers

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).

like image 172
Mustansar Fiaz Avatar answered Nov 05 '22 17:11

Mustansar Fiaz