问题
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