Gib999
Gib999

Reputation: 93

Trigger code in a Shiny app after a specific time each day

I have an app that currently is set up to run when the first user accesses it each morning. That works very well, however, we occasionally have a very early riser access it before the data is ready, and then it must be updated manually.

What I would like to do is alter the below code to run when the first user access the app after a specific time, say 5 AM CST. I can't wrap my head around how to use today() along with a specific time. The way it works now is very simple, we just like at the file info for the CSV & if it's not been updated today we run the code; I just need to add a specific hour in the morning so that it only works after a certain time.

FileInfoClaim = file.info("claimallocator.csv") 
FileInfoClaim$mtime <- anydate(FileInfoClaim$mtime) 
claimrenderer <- ifelse(FileInfoClaim$mtime==today(), "yes", "no")

Upvotes: 1

Views: 266

Answers (1)

Ash
Ash

Reputation: 1513

Working with dates and times can be tricky. The below minimal example should do what you're after:

library("shiny")

ui <- fluidPage(

)

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

    #Check if its after 5am (runs once at app startup)
    if(Sys.time() > ISOdatetime(format(Sys.time(),'%Y'), format(Sys.time(),'%m'), format(Sys.time(),'%d'), 5, 0, 0)) {

        showNotification("Its after 5am")

    } else {

        showNotification("Its before 5am")

    }

}

shinyApp(ui = ui, server = server)

Everything is done in the local timezone. This could cause issues if you span multiple timezones, for example if you deploy your app remotely.

If you haven't already I'd suggest looking at lubridate, a popular package which makes dealing with dates and times much easier.

Upvotes: 2

Related Questions