Run shiny function at pre specified times

Hello
I am working on a small app that runs certain events at specified system times.
the user opens that app, adds some info then presses an action button, this will trigger the app to monitor system and (for examples) please certain audio file at a predefined time.

In the example below, I created 2 time points that are 30 and 60 seconds form the time the user starts that app (this is just to test the concept, in real life, I will predefine the time points or import from an API).

I tried different methods below that I collected from other questions but none of them seem to work. was hoping I can find some guidance. Even if you can just direct me what functions to use/try, this will be extremely helpful as I feel I exhausted all available online resources without finding an asnwer.

library(shiny)
 


# Define UI for displaying current time ----
ui <- fluidPage(
  
  h2(textOutput("currentTime")),
  
  h2(textOutput("target1")),
  h2(textOutput("till1")), 
  br(),
  
  h2(textOutput("target2")), 
  h2(textOutput("till2")), 
  
  actionButton("play", "Start"), 
  
  br(), 
  uiOutput('my_audio')
  
)





# Define server logic to show current time, update every second ----
server <- function(input, output, session) {
  
  
  #v <- reactiveValues(timer = Sys.time()) #current time 
  
 
  output$currentTime <- renderText({
    invalidateLater(1000, session)
    paste("The current time is", Sys.time())
  })
  
  t1 <- isolate({
    Sys.time() + 30
  })
  
  t2 <- isolate({
    Sys.time() + 60
  })
  
    
  output$target1 <- renderText({
      #invalidateLater(1000, session)
      #
      paste("Target:1", t1)
    })
  
  output$target2 <- renderText({
      #invalidateLater(1000, session)
      #
      paste("Target:2", t2)
    })
  
  output$till1 <- renderText({
    invalidateLater(1000, session)
    #
    paste("Seconds till target 1:", t1-Sys.time())
  })
   
  output$till2 <- renderText({
    invalidateLater(1000, session)
    #
    paste("Seconds till target 2:", t2-Sys.time())
  })
  
 
  
  systi <- reactiveValues(timer = Sys.time())
  
  
  
  #another method to render audio UI:
  
  observeEvent(systi$timer == t1, {
     
      output$my_audio <-renderUI(tags$audio(src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3", type = "audio/mp3", autoplay = NA, controls = NA, style="display:none;"))
    
    
  })
  
  
  
  observe({
    invalidateLater(100)
    if(t1 == Sys.time()){
       
      output$my_audio <-renderUI(tags$audio(src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3", type = "audio/mp3", autoplay = NA, controls = NA, style="display:none;"))
      
    }
  })
  
  
  
  
 
  observeEvent(input$play, {
    if(systi$timer == t1){
      
      insertUI(selector = "#play",
               where = "afterEnd",
               ui = tags$audio(src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3", type = "audio/mp3", autoplay = NA, controls = NA, style="display:none;")  
      ) 
    }
  })
  
  
  observeEvent(input$play, {
    if(systi$timer == t2){
      
      insertUI(selector = "#play",
               where = "afterEnd",
               ui = tags$audio(src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-7.mp3", type = "audio/mp3", autoplay = NA, controls = NA, style="display:none;")  
      ) 
    }
  })
  
  
  
  
}

# Create Shiny app ----
shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.