I want to keep reflashing until 10:05, after 10:05 I got the tplus0_dt and stop to processing invalidaterLater().
Before 10:00, tplus0_dt doesn't exist, so I need to keep reflashing until 10:05. After 10:05, it is no need to refalsh, and when the tplus0_dt becomes very lage the invalidaterLater() will effects the displaying of table1, the screen and plots go GRAY every 1 seconds so it looks like the page is dead while the updating occurs.
So how can I do to stop processing the invalidateLater() and keep showing the data after 10:05? Thanks for help! My example code were below.
require(shiny)
require(data.table)
app <- shinyApp(
server = function(input, output, session){
get_tplus0_data <- reactive({
time <- substr(as.character(Sys.time()), 12, 16)
invalidateLater(1000)
if(time >= "10:05"){
# tplus0_dt<-data.table(read.csv("/data/df_highest_after_signal.csv",header = T, sep = ",", stringsAsFactors = F))
tplus0_dt<- data.table(a = c(1, 2, 3, 4), b = c(3, 4, 5, 8))
return(tplus0_dt)
}
})
output$table1 <- renderTable({get_tplus0_data()})
},
ui = fluidPage( tableOutput("table1") )
)
runApp(app)
Although you'd never realize it from the Shiny documentation, invalidateLater() actually only returns to your reactive once. The reason it seems to return repeatedly is that on each trip the invalidateLater() function gets run again.
So the solution is to use a conditional around the function so that you don't keep repeatedly calling it:
if(runMeAgain) {
invalidateLater(n)
}
runMeAgain = TRUE # your reactive re-runs every n milliseconds
runMeAgain = FALSE # your reactive does not re-run
Also note that:
invalidateLater() is non-blocking (other code can run while you
wait)invalidateLater() doesn't stop the rest of the reactive from
running. If you want to stop the reactive at that point in the code, put a return() after invalidateLater()invalidateLater() gets isolated() inside an observeEvent() or eventReactive() and consequently doesn't work; you have to use observe() or reactive(). It might also work inside a render function, but I haven't ever had a reason to try that.In terms of the original question, the reactive should look like this:
get_tplus0_data <- reactive({
time <- substr(as.character(Sys.time()), 12, 16)
if(time >= "10:05"){
tplus0_dt<- data.table(a = c(1, 2, 3, 4), b = c(3, 4, 5, 8))
return(tplus0_dt)
} else {
invalidateLater(1000)
return()
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With