Reputation: 304
I want to reset the reactive timer to zero and start counting to 10000 again.
If you press the reset button within 10s, "timer fires" should never print.
I thought this might work, but no.
require('shiny')
if (interactive()) {
ui <- fluidPage(
actionButton("reset_button", "Reset")
)
server <- function(input, output) {
autoInvalidate <- reactiveTimer(10000)
observe({
autoInvalidate()
print ("timer fires")
})
observeEvent(input$reset_button,
{
autoInvalidate <- reactiveTimer(10000)
print("reset")
}
)
}
shinyApp(ui, server)
}
Upvotes: 2
Views: 1112
Reputation: 29417
This should do:
library(shiny)
ui <- fluidPage(
actionButton("reset_button", "Reset"),
textOutput("text")
)
server <- function(input, output, session) {
v <- reactiveValues(timer = Sys.time()+5)
observe({
invalidateLater(100)
if(v$timer <= Sys.time()){
v$timer <- Sys.time()+5
print("timer fires")
}
})
observeEvent(input$reset_button,{
v$timer <- Sys.time()+5
print("reset")
})
}
shinyApp(ui, server)
Upvotes: 4
Reputation: 9809
I am not 100% sure I understand your desired behaviour. If you press "reset" should the timer start from 0 again, or should "timer fires" never be printed. Because with reactiveTimer
or invalidateLater
your code will re-execute every x milliseconds.
I came up with this little example. If you want "timer fires" never to appear when "reset" is pressed, you have to include the loopit()
part. If you just want to reset the timer, then delete the lines with loopit()
.
require('shiny')
if (interactive()) {
ui <- fluidPage(
actionButton("reset_button", "Reset")
)
start = Sys.time()
n=10000
server <- function(input, output) {
loopit <- reactiveVal(TRUE)
observe({
invalidateLater(n)
if (loopit()) {
print(paste("start: ", start))
if (Sys.time() > start+(n/1000)) {
print ("timer fires")
}
}
})
observeEvent(input$reset_button, {
start <<- Sys.time()
loopit(FALSE)
print("reset")
})
}
shinyApp(ui, server)
}
Upvotes: 1