R shiny dynamically render audio [close] - r

I am working on a project, trying to render audio dynamically. That is, I can click a button and play a selected local audio.
I have read this post and tried zedii's trick. The problem remains still.
Well I build a test app as shown below.
library(shiny)
# test set ----
ui <- fluidPage(
textInput('my_music','path:',value="questionF"),
actionButton("ok", "Okay"),
uiOutput('my_audio')
# tags$audio(src = "questionF.mp3", type = "audio/mp3")
)
get_audio_tag <- function(filename) {
tags$audio(src = filename,
type = "audio/mp3",
controls = "controls")
}
server <- function(input, output, session){
# Render the audio player
observeEvent(input$ok, {
wav_name = input$my_music
# output$my_audio <-renderUI(get_audio_tag("questionF.mp3"))
output$my_audio <-renderUI(get_audio_tag(wav_name))
})
}
shinyApp(ui = ui, server = server)
When I click the button, the first song turns out okay. But the following ones seemed pretty hard to load, for my computer would freeze with the memory used by Rstudio rising.
Any thoughts will be appreciated.
updates:
I tried on different browsers. These codes would failed on Chrome but works fine on Microsoft edges.
Looks like a caching problem.
So now my question is how can I make the codes work on every platform using shiny/R?
My codes works on most browser other than Chrome. I think it's more like a Chrome's problem.

Related

How Can I Set a Youtube Video to Autoplay Using R Shiny?

I'm using this code which will accept a Youtube URL and play the video in the RStudio viewer pane, rather than in a popout browser. It does exactly what I need it to do except that I would like it to autoplay when the function has run. I've seen a few other examples ( Like here and here ) where folks insert an allow = autoplay or something similar in the UI's fluidPage command but I haven't gotten it to work for me. I have additionally tried adding ?autoplay=1 to the URL, as described here, which works generally in any browser but seems to be ignored by R. Here's an example to replicate with:
videoplay <- function(url = "https://www.youtube.com/embed/wpHQnCaAJv8")
{
library(shiny)
xy <- c(784,479)
url <- gsub("watch\\?v=","embed/",url)
ui <- fluidPage(
HTML(paste0('<iframe width="',xy[1],'" height="',xy[2], '" src="', url,"?autoplay=1",'" frameborder="0"></iframe>'))
)
server <- function(input, output, session) {
}
runGadget(shinyApp(ui, server,options=c("launch.browser"=FALSE,"port"=1111)),port=1111,viewer = paneViewer())
}
Does anyone know how to make this video autoplay when the function is run? Appreciate any help!

R Shiny HTML object is rendered on every tabPanel()

I am trying to create a shiny app where users can upload their own data and get a visualization of the network dynamics in their data. I'm using the render.d3movie() function from the ndtv package to create an HTML object from the network with some user input parameters. I want to display this HTML object in one of my tabPanel()s but weirdly, it shows up on every panel instead. I tried with a different HTML file and this one works just fine. To reproduce this, you'll need to download the test and network animation html files and put them in the same directory as the example shiny app code below.
Example:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
navbarPage("ShinyExample",
tabPanel("TEST", HTML("FirstPage"), includeHTML("test.html")),
tabPanel("TEST2", HTML("SecondPage"), includeHTML("NetworkAnimation.html"))
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
# Stuff to render and save the network Animation happens here
}
# Run the application
shinyApp(ui = ui, server = server)
Expected Behavior:
test.html and NetworkAnimation.html should only be rendered within their respective tabPanel()
Observed Behavior:
test.html is only rendered in it's respective tabPanel() but NetworkAnimation.html is rendered on both tabPanel() s
The network-widget is appended to the body in the NetworkAnimation.html-Javascript:
target = d3.select('body').append('div').style({width: '100%', height: '100%'}).node();.
If you want it to be on Tab 2 only, you can include a div with id and change the JavaScript line, so that it gets appended to your div.
My App looks like this:
ui <- fluidPage(
## Include shinyjs
useShinyjs(),
## Set ID of navbarPage
navbarPage("ShinyExample", id = "navid",
tabPanel("TEST", includeHTML("test.html")),
tabPanel("TEST2",
## Include new DIV here & Set initial height
div(id="appendhere", style="height: 1090px;"),
includeHTML("NetworkAnimation.html"))
)
)
server <- function(input, output) {
observe({
req(input$navid == "TEST2")
runjs('$("#appendhere").resize()')
})
}
# Run the application
shinyApp(ui = ui, server = server)
and the JS is changed to
target = d3.select('#appendhere').append('div').style({width: '100%', height: '100%'}).node();
I also had to include shinyjs, to run some JavaScript when TAB2 is active. The width/height of the svg is calculated when it is rendered and therefore initially 0 (or actually -60).
If you remove the runjs line, you will see that the network is not visible.
By changing the browser size, the network gets redrawn and width/height are updated. Therefore we call $("#appendhere").resize() when TAB2 is active.
There is still the following error in the browser console, but everything seems to work fine.
Uncaught TypeError: a is undefined

Why does my download button activate twice in shiny app?

So I have a shiny app (https://github.com/tadeu95/BAGs) (http://tadeu-apps.shinyapps.io/bags) and I have several download buttons. Recently what has started to happen is that when I click the download button it activates two times and downloads the same file twice. The thing is that the app worked for almost a year without any problem.
I've tried on chrome, edge and mozzila and happens every time. I don't know what happened because I didn't touch the part of the code where the downloads are implemented.
One thing I've just discovered is that if instead of clicking the download button with the left button of the mouse, I click the right button and choose "open link in a new tab", it downloads the file correctly only once.
This is a short reproducible app, I advise that you give as input "ursidae" as it downloads the file pretty quickly:
library(bold)
library(readr)
library(shiny)
library(shinyWidgets)
grades2<-function(groups){
taxon9<-bold_seqspec(taxon=groups, format = "tsv")
}
ui <- fluidPage(textInputAddon(inputId="taxa2",addon=icon("search"),width="500px",label=tags$h5(tags$strong("Enter the name of the taxonomic group or groups separated by commas, without spaces:")),placeholder="Example: Carnivora,Ursidae,Artiodactyla,Soricomorpha"),downloadBttn("downloadData_2",size="sm","Download"))
server <- function(input, output) {
taxaInput_2 <- reactive({grades2(unlist(strsplit(input$taxa2, ",")))})
output$downloadData_2 <- downloadHandler(
filename = function() {
paste(input$taxa2,sep_out=",", ".tsv")
},
content = function(file) {
shiny::withProgress(
message=paste0("Downloading and annotating library for ",input$taxa2,sep_out=","), detail='This may take several minutes',
value=10,
{
shiny::incProgress(10/10)
write_tsv(taxaInput_2(), file)
}
)
}
)
}
shinyApp(ui=ui,server=server)
If anyone has any idea what the reason might be, I will be very thankful.

r shiny tags$video not showing video

I want to play an mp4 video in a shiny output. It plays fine on my computer when I double click on the file name. I copied the file to the www folder below the main app folder. I have tried variations on the following code.
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(debug = TRUE),
mainPanel(
"video test",
uiOutput('ozoneVideo'), id = "mainPanel")
)
server <- function(input, output, session) {
output$ozoneVideo <- renderUI({
h6("ozone video", tags$video(src = "MesaCountyOzone2017_07_10.mp4", type = "video/mp4", width = "1080px", height = "480px", controls = "controls", autoplay = NA))
})
}
shinyApp(ui = ui, server = server)
The web page that is generated shows the text elements (video test, ozone video) and horizontal bar with a video play button and the word "Error" with no explanation. No error appears in the javascript console.
When I add www/ to the filename, the javascript console has an error message "Failed to load resource: the server responded with a status of 404 (Not Found) and a url of http://127.0.0.1:4817/www/MesaCountyOzone2017_07_10.mp4
Any help clarifying this greatly appreciated!
Update: July 11, 2017.
I am now pretty sure this is a problem with Safari. I run the app with run external chosen. The web page that opens up in Safari has an error message in the video control section. If I copy the url and past to a Chrome web page, the video plays fine.
So my question is now what do I do to make this play in Safari???

Shiny open multiple browser tabs

In my Shiny app I want to open several URL's with a short delay between opening.
Here is some example code that works just fine when I run the app in my RStudio.
library(shiny)
URLs <- c("http://www.google.com", "http://www.stackoverflow.com")
ui <- fluidPage(
actionButton(
"click",
"Click here to open several browser tabs"
)
)
server <- function(input, output){
observeEvent(input$click, {
for (i in URLs){
browseURL(i)
Sys.sleep(1) #Short delay of 1 second
}
})
}
shinyApp(ui, server)
However, when I run this app on shinyapps.io, browseURL() doesn't work (as mentioned here).
Does anyone know how to open multiple browser tabs with a short delay between opening them, so that it also works when the app is deployed on shinyapps.io?
Would it be possible with R code or is JavaScript necessary?
This is a pretty old question, but answering in case others stumble upon while searching.
As mentioned in the reference you linked, I think you need to use some JS to accomplish this task. Below is an example of using the shinyjs package to define a shiny compatible browseURL function. Once we have the function defined we add a few lines to the ui and then call it in the server as js$browseURL().
Note that a pop-up blocker might block the effects of opening multiple tabs. Check your blocker settings if things don't seem to work.
library(shiny)
library(shinyjs)
# define js function for opening urls in new tab/window
js_code <- "
shinyjs.browseURL = function(url) {
window.open(url,'_blank');
}
"
URLs <- c("http://www.google.com", "http://www.stackoverflow.com")
ui <- fluidPage(
# set up shiny js to be able to call our browseURL function
useShinyjs(),
extendShinyjs(text = js_code, functions = 'browseURL'),
actionButton(
"click",
"Click here to open several browser tabs"
)
)
server <- function(input, output){
observeEvent(input$click, {
for (i in URLs){
js$browseURL(i)
Sys.sleep(1) #Short delay of 1 second
}
})
}
shinyApp(ui, server)

Resources