0

I am trying to display a dynamic iframe to display youtube videos based on user's input for the URL.
My code does not generate error, but the video does not display, would you have any solution ?
Many thanks in advance!
Regards,
Thomas

My code:

ui.R

box(title = "settings", status = "warning", solidHeader = TRUE, width = 2, textInput("texturli", label = "enter valid Youtube URL", value = "https://www.youtube.com/watch?v=C7mXGMcpA0g"),
                    actionButton("geturl", label = "launch analysis")
                  ),
box(title = "video", status = "primary", solidHeader = TRUE, width = 3, 
tags$iframe(src = "https://www.youtube.com/watch?v=C7mXGMcpA0g"), # this works fine
htmlOutput("test")) # this does not work

server.R

url <- reactive({
    input$geturl
    isolate(paste0(input$texturli))
  })

  output$test <- renderUI({
    tags$iframe(src = url())
  })
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
cho7tom
  • 1,030
  • 2
  • 13
  • 30

2 Answers2

1

I found the solution:

ui.R

box(title = "settings", status = "warning", solidHeader = TRUE, width = 2,  
    textInput("texturli", label = "enter valid Youtube URL", value = https://www.youtube.com/watch?v=C7mXGMcpA0g"),
    actionButton("geturl", label = "launch analysis")),  

box(title = "video", status = "primary", solidHeader = TRUE, width = 3, 
htmlOutput("test"))

server.R

url <- reactive({
input$geturl
isolate(paste0("https://www.youtube.com/embed/",sub(".*v=(.*)","\\1",input$texturli)))})

output$yvideo <- renderUI({
tags$iframe(id = "app", src = url(), width = "100%")})

Regards,

Thomas

cho7tom
  • 1,030
  • 2
  • 13
  • 30
0

When I run the code, I get this error in my browser's console:

Refused to display 'https://www.youtube.com/watch?v=C7mXGMcpA0g' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'

Instead of using the link to the video, try using the Youtube embed link to fix this error (see this and this):

http://www.youtube.com/embed/C7mXGMcpA0g

In your dynamic iframe, you could for example do:

url <- reactive({
    input$geturl
    isolate(paste0("http://www.youtube.com/embed/",sub(".*v=(.*)","\\1",input$texturli)))
  })
Community
  • 1
  • 1
NicE
  • 21,165
  • 3
  • 51
  • 68
  • Thank you NicE. You are right, i have to use the embed link for sure. However it still does not work (despite no error while launching my shiny app). The shape for the iframe appears on my screen but there is no content. I may not fill the src parameters correctly in 'tags$iframe(src = url())' ... :-( – cho7tom Mar 11 '15 at 10:46