闪亮的应用程序:downloadHandler不生成文件

时间:2014-09-22 22:38:22

标签: r shiny

我无法弄清楚发生了什么 - 一切似乎都有效但我的应用程序没有生成文件 - 尽管看起来确实如此。 我在Windows上运行它,在RStudio 0.98.125上,我使用以下代码运行它: runApp() 下面是一个非常简单的可重复示例:

我的'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))
  ))

我的'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)
    }
  )

})

3 个答案:

答案 0 :(得分:51)

请注意,下载按钮在RStudio查看器中不起作用。您的 朋友可能正在使用RStudio查看器来查看应用程序。如果那样的话 如果是这种情况,请在外部网络浏览器中打开应用程序(有一个 “运行应用程序”按钮右侧的下拉列表:在窗口中运行, 在Viewer Pane中运行,运行外部;选择最后一个)。

答案 1 :(得分:7)

提供的示例适用于在我的测试中下载CSV(如果它来自webbrowser,即使用RStudio中的Run app确实导致同样的问题)

请注意,如果您从下载按钮而不是下载的内容中继续获得类似“download.html”的内容,则必须确保downloadButton(“myIdHere”,...)中的ID与输出$ myIdHere = downloadHandler匹配( “output.csv”,...)

另请注意,如果您使用闪亮模块(您可能知道您是否使用此模块),那么您想使用downloadButton(ns(“myIdHere”),...)然后您仍然输出$ myIdHere

答案 2 :(得分:0)

添加Colin D所说的内容。可能只是你的idNAME太长了。

我刚在我的应用程序上测试了这个工作原理:

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

并且这不起作用:

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

所以,简明扼要吧!

相关问题