Fabi_92
Fabi_92

Reputation: 581

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?

Upvotes: 7

Views: 2761

Answers (1)

Sandipan Dey
Sandipan Dey

Reputation: 23109

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

Upvotes: 8

Related Questions