3

After figuring out how to make a Shiny server play a wav file, I created a shiny server that dynamically creates wav files based on reactive input. However, the first wav file gets cached, and despite changes to the file or even renaming the file, only the first wav is played until a full page refresh.

How could I play a changed wav file after it is changed through an HTML-based Shiny app?

I understand that this question has been asked (and solved) for HTML developers in a few places, using javascript or Jquery/php or a server-side solution, but I haven't figured out how to make any of these work with Shiny.

Community
  • 1
  • 1
Zediiiii
  • 750
  • 1
  • 7
  • 21
  • 1
    Why put this on hold? Refreshing an HTML based audio tag is general, but generating the code in an R Shiny app is pretty specific, and isn't posted or elaborated anywhere else. In fact, playing audio in the first place using R shiny is sparsely documented. I don't have a working example precisely because the issue is so specific. – Zediiiii Mar 28 '16 at 01:38
  • Could you tell me how did you managed to save wav files in a shiny server? I'm trying to do something similar and I used the same approach as described [here](http://stackoverflow.com/questions/33594007/writting-wav-file-in-shiny-app-with-seewave-and-tuner). But somehow this is not running. – zielinskipp Jun 11 '16 at 23:18
  • @Piotr Zieliński http://stackoverflow.com/questions/36205419/r-shiny-audio-playback doesn't get you there, then PM me and I'll send you some of my code. – Zediiiii Jun 23 '16 at 18:21
  • I've already overcome the problem. But thanks anyway. – zielinskipp Jun 23 '16 at 19:21

1 Answers1

2

Solved by BSU prof. Champion. At the top of the script we have some name handling functions for keeping the wav names unique, and then a function that gets the appropriate audio tag:

    get_audio_tag<-function(filename){tags$audio(src = filename,
                                  type ="audio/wav", controls = NA)}
    wave_name<-function(n,p1,f1,p2,f2,p3,f3,l){
    paste0( paste("sound","num",n, "w1",p1,f1,"w2", p2,f2,"w3",p3,f3,"lev",
    gsub("\\.","_",l), sep="_"), ".wav")
    }

The trick is that the variable wavname updates dynamically inside one of the output functions, so that every time a reactive input is touched, the wav file changes names. Then the get_audio_tag function runs inside the output function as well so that the update occurs. The code the output function looks like:

wname<-wave_name(   input$radio,
                    input$frequency1,input$form1, 
                    input$frequency2,input$form2,
                    input$frequency3,input$form3,
                    input$level)

output$audiotag<-renderUI(get_audio_tag("tempwav.wav")) #starting wave file    
output$audiotag<-renderUI(get_audio_tag(wavname))

The starting tempwav is necessary because the audio tags are loaded in the HTML before the reactive elements send their first output.

Zediiiii
  • 750
  • 1
  • 7
  • 21