cho7tom
cho7tom

Reputation: 1070

Shiny | iframe | reactive

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())
  })

Upvotes: 0

Views: 2324

Answers (2)

cho7tom
cho7tom

Reputation: 1070

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

Upvotes: 1

NicE
NicE

Reputation: 21425

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)))
  })

Upvotes: 0

Related Questions