R Shiny Sounds Upon Selection

Mmm, I see. I think one thing you may want to try is to set the ignoreInit = FALSE in your observeEvent() call (I am guessing you are using that observer to trigger your sounds). That should supress any inputs from being "observed" on app start-up.

Also, I've been playing a bit more with music-playing implementation with shiny and found a better way to do it than my previous post. The code I posted before creates a new Howler object with every runjs() call inside the shiny observer.

Just for posterity, I leave here a working example that creates a sound object upon app start-up, and then call a method from that object inside an observeEvent() (here, it is the .play method). There surely is room for improvement, but I think this is a better starting point.

library(shiny)
library(shinyjs)

ui =  fixedPage(
  useShinyjs(),
  includeScript("www/howler.js"),

  tags$script("function load_music(){
                var music = new Howl({src:['http://www.sonidosmp3gratis.com/sounds/mario-coin.mp3'],
                                 autoplay: false});
                return music;}"), # return the howler object
  br(),
  actionButton(inputId = "playButton", 
               label = "",
               width = "200px",
               icon = icon("play")),
  p("Click the button to get COINS :).")
)

server = function(input, output) {
  shinyjs::runjs("my_music = load_music()") # load music upon start-up
  observeEvent(
    input$playButton,({
      shinyjs::runjs("my_music.play();") # use a method of the howler object
    } )
  )
}

shinyApp(ui = ui, server = server)


This post was published by an Appsilon team member. Our company can help you get the most out of RShiny and Posit/RStudio products.

Check our open positions here.

Appsilon: Building impactful RShiny Dashboards and providing R coding services.
Appsilon_GIFsmall_whitebg