That indeed demonstrates the functionality I'm after thank you @nirgrahamuk! However, I'm looking to get the timer object into an independent module, a standalone file I can import into others and use it there to trigger refreshes. Can you help me to modularise timer?
I tried myself and again the app ticks but it doesn't allow me to modify the timer object. I think I'm not correctly passing the options to the server function of the timer.
The reason I need it as a separate module is I want to then import it to various other files with other parts of my app and trigger refreshes on those all together.
I'm still learning so thank you for the help. Here is what I have modularised so far from the code you gave me.
main.R
box::use(shiny)
ui <- shiny$fluidPage(
shiny$numericInput(
"ms",
"millisec",
value=1000,
min=100,
max=2000,
step=100
),
shiny$checkboxInput("chk", "on/off", value = TRUE),
shiny$verbatimTextOutput("display")
)
# to see more decimal points
op <- options(digits.secs = 6)
box::use(./suggested_timer)
server <- function(input, output, session) {
timer_options <- shiny$reactive({
list(
timer_ms = input$ms,
timer_active = input$chk
)
})
last_timer_value <- suggested_timer$server(timer_options)
output$display <- shiny$renderText({
capture.output(shiny$req(last_timer_value()))
})
}
shiny$shinyApp(ui, server)
suggested_timer.R
box::use(shiny)
#' @export
server <- function(timer_options) {
options <- shiny$isolate(timer_options())
mytimer <- shiny$reactive({
if(!shiny$isTruthy(options$timer_active)) {
return(NULL)
} else{
shiny$invalidateLater(millis = shiny$req(options$timer_ms))
}
Sys.time()
})
last_timer_value <- shiny$reactiveVal(NULL)
shiny$observeEvent(mytimer(), {
if(!identical(last_timer_value, mytimer())) {
if(shiny$isTruthy(mytimer())) {
last_timer_value(mytimer())
}
}
})
return(last_timer_value)
}