2021-04-29

Using shinyjs delay to delay running a code by a calculate amount of milliseconds

I am trying the code below using delay from shinyjs package. What I am expecting is to show the modal AND play the audio after a predefined time from running the app (5 seconds in this example). I only can see the modal but audio is not playing. any guidance on what might be wrong?

thank you



library(shiny)
 
library(shinyjs)
 
ui <- fluidPage(
  useShinyjs(),
  h2(textOutput("currentTime")),
  br(),
  br(),
  h2(textOutput("target1")),
  h2(textOutput("till1")), 
  uiOutput('my_audio1')
 
)



server <- function(input, output, session) {
  
  
  
  output$currentTime <- renderText({
    invalidateLater(1000, session)
    paste("The current time is", format(Sys.time(), "%H:%M"))
  })
  
  t1 <- isolate({
    Sys.time() + 5
    
  })
 
  
  output$target1 <- renderText({
    
    paste("Target", t1)
  })
 
  
  output$till1 <- renderText({
    invalidateLater(1000, session)
    
    paste("Seconds till target:", round (t1-Sys.time()))
  })
 
  
  sys1 = isolate({
    Sys.time()
  })
  
  observe({
    print(sys1)
    print(t1)
    print(t1-sys1)
    print(as.numeric(t1-sys1, units = "secs")*1000)
  })
  
  
    
  observe ({
    delay (as.numeric(t1-sys1, units = "secs")*1000, 
           showModal(modalDialog(
             title = "",
             "Audio should play now"))  
    )
  })
  
 
  
  observe({
    delay (as.numeric(t1-sys1, units = "secs")*1000, 
  output$my_audio1 <-renderUI({
               tags$audio(src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3", 
                          type = "audio/mp3", autoplay = NA, controls = NA, style="display:none;")
    })
  )
  })
  
  
 
}

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




from Recent Questions - Stack Overflow https://ift.tt/2R936Gl
https://ift.tt/eA8V8J

No comments:

Post a Comment