Vincent的解决方案使用了两个按钮,一个用于计算的动作按钮和一个用于下载的下载按钮.此解决方案的另一个好处是进度条也包含在shinyIncubator包中.
我的代码的解释是因为其他人想要做同样的事情:
ui.R有一个动作按钮和一个动态下载按钮:
actionButton("makePlots", "Calculate Results"),
uiOutput("download_button")
Run Code Online (Sandbox Code Playgroud)
以及进度条的进度初始化:
  mainPanel(
    progressInit(),
      uiOutput("mytabs")) # dynamic rendering of the tabs
Run Code Online (Sandbox Code Playgroud)
server.R有点复杂.因此只有在有东西要下载时才显示下载按钮我使用动态uiOutput并使用以下代码:
    output$download_button <- renderUI({
          if(download){
              downloadButton("downloadPlots", "Download Results")
          }
     })
Run Code Online (Sandbox Code Playgroud)
下载按钮仅在显示时显示download==TRUE.在server.R的开头,变量被初始化:download<-FALSE
当每次点击动作按钮增加1时,我包括一个计数器(初始值0),在每次"使用"动作按钮后增加.原因是第一个if语句.
makePlots<-reactive({
    if(input$makePlots>counter){ # tests whether action button has been clicked
       dir.create("new_directory_for_output")
       withProgress(session, min=1, max=15, expr={ # setup progress bar
       for(i in 1:15){
         png(paste0("new_directory_for_output/plot",i,".png"))
         plot(i)
         dev.off()
         setProgress(message = 'Calculation in progress',
              detail = 'This may take a while...',
              value=i)
       } # end for
       }) # end progress bar
       counter<<-counter+1 # needs the <<- otherwise the value of counter
                           # is only changed within the function
       download<<-TRUE     # something to download     
     } # end if
}) # end function
Run Code Online (Sandbox Code Playgroud)
在这个阶段,函数makePlots()没有输出,也没有在任何地方调用,因此它什么都不做.因此,我将makePlots()放在每个选项卡的开头,这样无论用户在哪个选项卡上,一旦单击操作按钮,就会生成并保存绘图.
最后一块拼图是下载处理程序:
output$downloadPlots <- downloadHandler(
    filename = function() { my_filename.zip },
    content = function(file){
      fname <- paste(file,"zip",sep=".") 
      zip(fname,new_directory_for_output) # zip all files in the directory
      file.rename(fname,file)
      unlink(new_directory_for_output,recursive = TRUE) # delete temp directory
      download<<-FALSE # hide download button 
    }
    ) # end download handler
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           2534 次  |  
        
|   最近记录:  |