【问题标题】:Shiny | iframe | reactive闪亮 |框架 |反应式
【发布时间】:2015-05-13 00:18:10
【问题描述】:

我正在尝试显示一个动态 iframe,以根据用户输入的 URL 显示 youtube 视频。
我的代码没有报错,但是视频不显示,请问有什么解决办法吗?
非常感谢!
问候,
托马斯

我的代码:

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

服务器.R

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

  output$test <- renderUI({
    tags$iframe(src = url())
  })

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    当我运行代码时,我在浏览器的控制台中收到此错误:

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

    不要使用视频链接,而是尝试使用 Youtube 嵌入链接来修复此错误(请参阅 thisthis):

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

    在您的动态 iframe 中,您可以执行以下操作:

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

    【讨论】:

    • 谢谢你。你是对的,我必须使用嵌入链接。但是它仍然不起作用(尽管启动我闪亮的应用程序时没有错误)。 iframe 的形状出现在我的屏幕上,但没有内容。我可能无法在 'tags$iframe(src = url())' 中正确填写 src 参数... :-(
    【解决方案2】:

    我找到了解决办法:

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

    服务器.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%")})
    

    问候,

    托马斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2016-02-10
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多