在do.call()终止之前完成了闪亮的进度条

时间:2017-12-05 15:12:32

标签: r progress-bar shiny do.call

我正在尝试实现一个使用一组参数调用函数的闪亮应用程序。这样的函数返回一个用renderPlot()渲染的图。生成进度条以保持用户更新。

我对使用do.call调用此函数及其参数特别感兴趣,因为我的最终目标是将其包装为闪亮的模块。但是,在绘图获得生成机会之前,进度条已“完成”。只有在进度条消失后,才会渲染绘图。我认为这是因为我试图使用do.call()来处理绘图功能。我在这里创建了一个问题的简化版本:

ui <- fixedPage(
  wellPanel(
    plotOutput("plot")
    , fluidPage(actionButton("Btn", "plot", class = "btn-primary"))
  )
)

server <- function(input, output, session) {

  observeEvent(input$Btn, {

    message("button triggered")

    withProgress(message = "Progress bar"
                   , detail ='Rendering plot...'
                   , value = 0,{

                     # pausa
                     Sys.sleep(1)               
                     # updade progress bar
                     setProgress(0.5)

                     output$plot <- renderPlot({
                       # plot to render
                       do.call(plot, list(1:10, 11:20))  
                       # pause
                       do.call(Sys.sleep, list(2))
                     })

                     # update progress to complete status
                     setProgress(1)
                   })



    showNotification("DONE!", duration = 5, closeButton =  TRUE, type = "warning")

  })
}

shinyApp(ui, server)

我希望在生成和可视化图之后完成进度条。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我添加了两个选项,一个是shinycssloaders,如果您不知道,另一个是修复现有代码以同步情节和条形码。

library(shinycssloaders)

ui <- fixedPage(
  wellPanel(
    #withSpinner(plotOutput("plot")) #uncomment if you need to use the pkg
    plotOutput("plot")
    , fluidPage(actionButton("Btn", "plot", class = "btn-primary"))
  )
)

server <- function(input, output, session) {

  observeEvent(input$Btn, {

    message("button triggered")

    withProgress(message = "Progress bar"
                 , detail ='Rendering plot...'
                 , value = 0,{

                   # pausa
                   Sys.sleep(1)               
                   # updade progress bar
                   setProgress(0.5)



                   # update progress to complete status
                   setProgress(1)
                 })

    output$plot <- renderPlot({
      # plot to render
      do.call(plot, list(1:10, 11:20))  
      # pause
      #do.call(Sys.sleep, list(2))
    })


    showNotification("DONE!", duration = 5, closeButton =  TRUE, type = "warning")

  })
}

shinyApp(ui, server)