R_FF92 R_FF92 - 2 months ago 16
R Question

Countdown Timer in R shiny?

I want display the current time in my shiny App. Therefore, I can use

Sys.time()


function(input, output, session) {
output$currentTime <- renderText({
invalidateLater(1000, session)
paste("The current time is", Sys.time())
})
}


I am wondering if it is also possible to code a countdown timer depending on the current time for e.g. an upcoming event?

Answer

The following code should do (let's assume the event is only 4 mins ahead):

EventTime <- Sys.time() + 4*60
output$eventTimeRemaining <- renderText({
    invalidateLater(1000, session)
    paste("The time remaining for the Event:", 
           round(difftime(EventTime, Sys.time(), units='secs')), 'secs')
  })

with the following output:

The time remaining for the Event: 226 secs