问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/36208785/html-or-r-shiny-audio-caching