Ok my title is a little bit confusing but let me explain.
I am using renderUI to get an audio tag, however, I want to start the audio at a lower volume because it is simply too loud.
The code below works fine, except because I added the delay it starts off at a higher volume and quickly goes to the lower volume but it is still very noticeable. Lowering the delay does not help, I've tried. If I remove the delay, the two observeEvents will run simultaneously and the volume will not be changed. If I move the js$runs tag (which lowers the volume) inside the first observeEvent, it won't work either. I think this is because renderUI does not actually render until the observeEvent has fully completed. Also, I don't think I can remove the renderUI because in my full app, I take a user input to play the audio.
Is there a way to initially start the audio tag at a lower volume? Or is there a way to run the renderUI immediately so that there will be no delay?
All help is appreciated, thanks.
library(shiny)
library(shinyjs)
jsCode <- 'shinyjs.runs = function setHalfVolume() {document.getElementById("myaudio").volume = 0.2;}'
get_audio <- function(){
tags$audio(id = "myaudio", controls = NA, autoplay = NA, tags$source(src="aud.mpeg"))
}
ui <- fluidPage(
useShinyjs(),
extendShinyjs(text = jsCode),
uiOutput("my_audio"),
actionButton("guessbutton", "Submit")
)
server <- function(input, output) {
observeEvent(input$guessbutton, {
output$my_audio <- renderUI(get_audio())
#js$runs()
})
observeEvent(input$guessbutton, {
delay(100, js$runs())
})
}
shinyApp(ui = ui, server = server)