有没有一种方法,以prevent从R中闪亮打开下载页面? [英] Is there a way to prevent the download page from opening in R Shiny?

查看:263
本文介绍了有没有一种方法,以prevent从R中闪亮打开下载页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦下载按钮被点击了一个闪亮的应用程序,一个新的页面打开初始化下载。然而,我的下载处理程序需要花费一些时间,以产生在主发亮页上的进度条中所示的下载的文件。有没有一种方法来打开或推迟下载页面保持用户主页下载页面上或prevent,直到该文件已被生产出来的?

Once the download button has been clicked in a shiny app, a new page opens initialising the download. However, my download handler takes some time to produce the downloadable file which is shown in a progress bar on the main shiny page. Is there a way to keep the user on the main page or to prevent the download page from opening or postponing the download page till the file has been produced?

非常感谢

马库斯

推荐答案

使用两个按钮,用于计算一个动作按钮并下载一个下载按钮,文森特的解决方案是一个我跟去了。一个额外的奖金,以这种解决方案的进度条还配备了shinyIncubator包。

Vincent's solution using two buttons, an action button for the calculation and a download button for the download is the one I went with. An additional bonus to this solution is the progress bar that also comes in the shinyIncubator package.

我的code的柜面别人的解释想要做同样的事情:

An explanation of my code incase someone else wants to do the same thing:

该ui.R有一个操作按钮和一个动态下载按钮:

The ui.R has an action button and a dynamic download button:

actionButton("makePlots", "Calculate Results"),
uiOutput("download_button")

和进度初始化进度条:

  mainPanel(
    progressInit(),
      uiOutput("mytabs")) # dynamic rendering of the tabs

该server.R是有点复杂。这样当有东西下载我用动态uiOutput具有以下code仅显示下载按钮:

The server.R is a little more complex. So that the download button is only shown when there is something to download I used the dynamic uiOutput with the following code:

    output$download_button <- renderUI({
          if(download){
              downloadButton("downloadPlots", "Download Results")
          }
     })

下载按钮时,才会显示在下载== TRUE 。在server.R开始的变量被初始化:下载&LT; -false

The download button is only shown when download==TRUE. At the start of server.R the variable is initialised: download<-FALSE

正如每次1操作按钮增加被点击我包括一个计数器(初始值为0),经过各操作按钮的使用增加。之所以这样,是第一个if语句。

As the action button increases by 1 everytime it is clicked I included a counter (initial value 0) that increases after each "use" of the action button. Reason for this is the first if statement.

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()在每个标签的开头,这样无论哪个选项卡用户,一旦动作按钮被点击该地块是由并保存。

At this stage the function makePlots() doesn't have an output and isn't called anywhere so it does nothing. I therefore placed makePlots() at the beginning of each tab so that no matter which tab the user is on, once the action button has been clicked the plots are made and saved.

德拼图的最后一块是下载处理程序:

The final piece of teh puzzle is the download handler:

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

这篇关于有没有一种方法,以prevent从R中闪亮打开下载页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆