在闪亮的应用程序中单击下载按钮后,将打开一个新页面来初始化下载。但是,我的下载处理程序需要一些时间来生成可下载文件,该文件显示在主闪亮页面上的进度栏中。有没有一种方法可以使用户停留在主页上或阻止下载页面打开或将下载页面推迟到生成文件为止?
非常感谢
马库斯
Vincent的解决方案使用了两个按钮,一个是用于计算的操作按钮,另一个是用于下载的下载按钮。该解决方案的另一个好处是进度条也包含在ShiningIncubator软件包中。
如果有人想做同样的事情,请对我的代码进行解释:
ui.R具有一个操作按钮和一个动态下载按钮:
actionButton("makePlots", "Calculate Results"),
uiOutput("download_button")
进度条的进度初始化:
mainPanel(
progressInit(),
uiOutput("mytabs")) # dynamic rendering of the tabs
server.R稍微复杂一些。因此,只有在有要下载的内容时才显示下载按钮,我将动态uiOutput与以下代码一起使用:
output$download_button <- renderUI({
if(download){
downloadButton("downloadPlots", "Download Results")
}
})
下载按钮仅在时显示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
在此阶段,函数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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句