title: Run shiny events at specified system times
body: <p>as a part of a shiny project I am working on, I need to play certain mp3 at specified times after the user presses an action button.</p>
  <p>below is a simplified example. The app displays the current system time (and updates it as a clock), displays when is the specified first time point is (target1) and how much time is left till that. same for time point 2. For simplicity, the difference between these time points is 30 seconds.</p>
  <p>In the last 2 pieces of code, I am trying to let shiny play a specified audio file when system time is equal to t1, then play another audio file when time reaches t2. <strong>But this part is not working.</strong></p>
  <p>do you have any suggestions?</p>
  <p>thank you</p>
  <h2>An update:</h2>
  <p>looks like the action button here is causing some unnecessary complexity so I changed from <code>observeEvent</code> to just <code>observe</code>
  what I need now is just to make the audio files play when the <code>t1</code> and <code>t2</code> points are reached.
  This is the updated code based on this modifications and on the suggestions below but it is still not working (ie, I do not hear the sound playing when the timer reaches <code>t1</code> or <code>t2</code>.</p>
  <p><strong>updated code:</strong></p>
  <pre><code>
  
  library(shiny)
  
  
  
  # Define UI for displaying current time ----
  ui &lt;- fluidPage(
    
    h2(textOutput(&quot;currentTime&quot;)),
    br(),
    br(),
    h2(textOutput(&quot;target1&quot;)),
    h2(textOutput(&quot;till1&quot;)), 
    br(),
    br(),
    h2(textOutput(&quot;target2&quot;)), 
    h2(textOutput(&quot;till2&quot;)), 
    uiOutput('my_audio1'), 
    uiOutput('my_audio2') 
  )
  
  
  
  server &lt;- function(input, output, session) {
    
    
    
    output$currentTime &lt;- renderText({
      invalidateLater(1000, session)
      paste(&quot;The current time is&quot;, Sys.time())
    })
    
    t1 &lt;- isolate({
      Sys.time() + 5
    })
    
    t2 &lt;- isolate({
      Sys.time() + 10
    })
    
    
    output$target1 &lt;- renderText({
      
      paste(&quot;Target:1&quot;, t1)
    })
    
    output$target2 &lt;- renderText({
      
      paste(&quot;Target:2&quot;, t2)
    })
    
    output$till1 &lt;- renderText({
      invalidateLater(1000, session)
      
      paste(&quot;Seconds till target 1:&quot;, t1-Sys.time())
    })
    
    output$till2 &lt;- renderText({
      invalidateLater(1000, session)
      
      paste(&quot;Seconds till target 2:&quot;, t2-Sys.time())
    })
    
    
    
    observe({
      if(Sys.time() &gt;= t1 &amp; Sys.time() &lt;= t2) {
        
        output$my_audio1 &lt;-renderUI(tags$audio(src = &quot;https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3&quot;, type = &quot;audio/mp3&quot;, autoplay = NA, controls = NA, style=&quot;display:none;&quot;))
        
      }
    })
    
    
    observe({
      if(Sys.time() &gt;= t2){
        
        output$my_audio2 &lt;-renderUI(tags$audio(src = &quot;https://www.soundhelix.com/examples/mp3/SoundHelix-Song-7.mp3&quot;, type = &quot;audio/mp3&quot;, autoplay = NA, controls = NA, style=&quot;display:none;&quot;))
        
      }
    })
    
    
  }
  
  # Create Shiny app ----
  shinyApp(ui, server)
  
  </code></pre>
  <p><strong>Previous code</strong></p>
  <pre><code>library(shiny)
   
  
  
  # Define UI for displaying current time ----
  ui &lt;- fluidPage(
    
    h2(textOutput(&quot;currentTime&quot;)),
    br(),
    br(),
    h2(textOutput(&quot;target1&quot;)),
    h2(textOutput(&quot;till1&quot;)), 
    br(),
    br(),
    h2(textOutput(&quot;target2&quot;)), 
    h2(textOutput(&quot;till2&quot;)), 
    
    actionButton(&quot;play&quot;, &quot;Start&quot;), 
    br(),
    br()
    
  )
  
  
   
  server &lt;- function(input, output, session) {
    
    
   
    output$currentTime &lt;- renderText({
      invalidateLater(1000, session)
      paste(&quot;The current time is&quot;, Sys.time())
    })
    
    t1 &lt;- isolate({
      Sys.time() + 30
    })
    
    t2 &lt;- isolate({
      Sys.time() + 60
    })
    
      
    output$target1 &lt;- renderText({
   
        paste(&quot;Target:1&quot;, t1)
      })
    
    output$target2 &lt;- renderText({
   
        paste(&quot;Target:2&quot;, t2)
      })
    
    output$till1 &lt;- renderText({
      invalidateLater(1000, session)
   
      paste(&quot;Seconds till target 1:&quot;, t1-Sys.time())
    })
     
    output$till2 &lt;- renderText({
      invalidateLater(1000, session)
   
      paste(&quot;Seconds till target 2:&quot;, t2-Sys.time())
    })
    
   
    
    observeEvent(input$play, {
      if(Sys.time() == t1){
        
        insertUI(selector = &quot;#play&quot;,
                 where = &quot;afterEnd&quot;,
                 ui = tags$audio(src = &quot;https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3&quot;, type = &quot;audio/mp3&quot;, autoplay = NA, controls = NA, style=&quot;display:none;&quot;)  
        ) 
      }
    })
    
    
    observeEvent(input$play, {
      if(Sys.time() == t2){
        
        insertUI(selector = &quot;#play&quot;,
                 where = &quot;afterEnd&quot;,
                 ui = tags$audio(src = &quot;https://www.soundhelix.com/examples/mp3/SoundHelix-Song-7.mp3&quot;, type = &quot;audio/mp3&quot;, autoplay = NA, controls = NA, style=&quot;display:none;&quot;)  
        ) 
      }
    })
    
    
  }
  
  # Create Shiny app ----
  shinyApp(ui, server)
  </code></pre>