R Shiny: display elapsed time while function is running

早过忘川 提交于 2021-02-11 15:40:30

问题


Taking the same idea of this post: R shiny: display "loading..." message while function is running

I'd like actually not only display a "loading" message, but also display the elapsed time of the run. And at the end, that the final running time remains displayed.

I adapt a MWE from the R-bloggers https://www.r-bloggers.com/long-running-tasks-with-shiny-challenges-and-solutions/

library(shiny)

ui <- fluidPage(

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
        actionButton('run', 'Run')
      ),

      # Show a plot of the generated distribution
      mainPanel(
         tableOutput("result")
      )
   )
)

server <- function(input, output) {
  N <- 10

  result_val <- reactiveVal()
  observeEvent(input$run,{
    result_val(NULL)
    for(i in 1:N){
       # Long Running Task
       Sys.sleep(1)

       # Update progress
       incProgress(1/N)
     }
    result_val(quantile(rnorm(1000)))
  })
   output$result <- renderTable({
     result_val()
   })
}

shinyApp(ui = ui, server = server)

According to the first link, a way to make appear a message/time/something is to use conditionalPanel or load the shinyjs library and use function like onclick, toggle...

I can see what happens in that case when I click the "Run" button, the message appears. But what happens when the code has runned?

This is the first point. Then, I don't want a simple message, I want a "timer" that display the time of the running.

There is this post about the total time How to time reactive function in Shiny app in r and also the use of invalidateLater from https://shiny.rstudio.com/gallery/timer.html But the last one display the current time, not the running time.

I am lost about how to combine all that to get what I want. With the MWE, once I click on the "Run" button, a display message box appears, with like for example "elapsed time is: " with the actual running time.

I hope I am clear in my request. Thank you for your help.

来源:https://stackoverflow.com/questions/61460611/r-shiny-display-elapsed-time-while-function-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!