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

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!

Related

How to play local video in shiny app?(Windows)

I tried the follow code:
shinyUI(
fluidPage(
tags$video(id="video2", type = "video/mp4",src = "sample.mp4", controls = "controls")
)
)
shinyServer(function(input, output, session){})
But, it returns a blank page,
How should I do to play the video?
For anyone late to this question - it won't play in RStudio's 'Run App' environment - launch your app to the browser and it seems to work fine.

R shiny dynamically render audio [close]

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.

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)

Write R code in a shiny app?

Is it possible to write R Code within the shiny app while it's running? I've built a shiny app, but would like to give users the option to write their own R code in the shiny app. I want to put the console in shiny along with the viewer and a simplified environment pane. I've never seen anything like this, so before I spend a lot of time doing this I'm wondering if it's even possible. Has anyone seen something like this?
Thanks to Cory I found rfiddle, which is exactly what I'm looking for. However I can't seem to get it to work. I used the iframe that r-fiddles website says to use to embed rfiddle, but I keep getting this error message:
Error in withReactiveDomain(shinysession, { :
No handler registered for for type .clientdata_output_<iframe width="300" height="600" src="http://r-fiddle.org/#/embed/eYsWfghB/1" allowfullscreen="allowfullscreen" frameborder="0"></iframe>_hidden
Code:
library(shiny)
ui = shinyUI(
fluidPage(
htmlOutput(tags$iframe(width=300, height=600,
src='http://r-fiddle.org/#/embed/eYsWfghB/1',
allowfullscreen='allowfullscreen', frameborder='0'))
)
)
server = shinyServer(function(input, output, session) {
})
shiny::shinyApp(ui,server)

R Shiny downloadButton error in Internet Explorer 8

Running the following minimal case:
library(shiny)
runApp(list(
ui = bootstrapPage(downloadButton("myDownload")),
server = function(input, output) {
output$myDownload <- downloadHandler(
filename = function() "mtcars.txt",
content = function(file) write.table(mtcars, file),
contentType = ".txt"
)
}
))
which looks like:
yields the below error in Internet Explorer 8.0.7601 when I click the download button:
Anyone know why this is happening?
Cannot recreate the problem, since I do not have IE 8. But I have remembered this thread, which talks about incompatibilty of shiny with Internet explorer lower than 10:
shiny-discuss/websockets
So I would suggest you upgrade your browser (your code works on my Internet Explorer 10, for example.)
does your code works with other browser like firefox or chrome? Past i have faced similar problem in download data thing. in downloadHandaler where data is getting generated i have assigned it as a global variable. after that is started working, i guess.

Resources