【问题标题】:Embed instagram/youtube into Shiny R app将 instagram/youtube 嵌入到 Shiny R 应用程序中
【发布时间】:2017-09-30 03:06:45
【问题描述】:

我想通过点击图表来播放 Instagram 或 Youtube 视频(例如显示异常值可能是什么等)

到目前为止,明确告诉 Shiny 视频是什么作品:

require(shiny)
require(ggplot2)

# data
df <- data.frame(ID=c(1,2),x=c(33,7),y=c(50,16),name=c("Vid1","Vid2"),link=c("https://www.youtube.com/embed/Gyrfsrd4zK0","https://anotherlink.com"), stringsAsFactors=FALSE)

# video is explicitly embedded with the youtube link (i.e. not dynamic)
ui <- basicPage(
  plotOutput("plot", click = "plot_click"),
  verbatimTextOutput("selection"),
  conditionalPanel("plot_click!=null",
                   h4(textOutput("nametext")),
                   HTML('<iframe width="200" height="100" src="https://www.youtube.com/embed/Gyrfsrd4zK0" frameborder="0" allowfullscreen></iframe>'))
)

server <- function(input, output,session) {

  output$plot <- renderPlot({
    ggplot(data=df,aes(x=x,y=y))+
      geom_point()+
      scale_x_continuous(limits = c(0, 68))+
      scale_y_continuous(limits = c(0, 52.5))
  })

  output$selection <- renderPrint({
    nearPoints(df, input$plot_click)
  })

  info <- reactive({

    t <- as.data.frame(nearPoints(df, input$plot_click))
    s <- t[1,4]
    u <- t[1,5]
    list(s=s,u=u)

  })

  output$nametext <- renderText({if(!is.na(info()$s)){info()$s}})
  output$urltext <- renderText({if(!is.na(info()$u)){info()$u}})
}

runApp(shinyApp(ui, server), launch.browser = TRUE)

我希望视频只在点击时出现(并且对于不同的点会有所不同),但是我不知道如何更改条件面板以适应这种情况。我尝试在服务器内使用 renderImage 以及 GoogleGroups answer,但没有得到任何乐趣。

【问题讨论】:

标签: r video youtube shiny embed


【解决方案1】:

在服务器端你可以使用:

 library(memisc) 
 output$video <- renderUI({
    click <- input$plot_click
    if(!is.null(click)){
      link = cases(
        "Gyrfsrd4zK0" = click$x > 40,
        "b518URWajNQ" = click$x > 20,
        "I5Z9WtTBZ_w "= click$x > 0
      )
      HTML(paste0('<iframe width="200" height="100" src="https://www.youtube.com/embed/', link ,'" frameborder="0" allowfullscreen></iframe>'))
    }
  })

在用户界面方面:

uiOutput("video")

【讨论】:

  • 谢谢,这对于给定的 youtube 视频效果很好,但我不能让它在 Instagram 上工作,我会继续摆弄
  • 欢迎您。当然只是发布一个新问题,我可以看看,..我猜应该以类似的方式可行。
  • ok,ta,基本上 Instagram 嵌入代码是一个 HTML 块引用,只有在没有其他参数的情况下直接粘贴到 HTML() 中时它似乎才有效(但它不会显示在上面的代码中)跨度>
  • 嗯,它不限于使用 iframe 进行,所以我认为它很好。提供一个 instagram 的小例子对我来说也很好,然后我进行编辑。
【解决方案2】:

这是 Instagram 的替代方案,在 iframe 内:

请原谅剪辑!

# make some data

df <- data.frame(ID=c(1,2),x=c(33,7),y=c(50,16),name=c("Vid1","Vid2"),link=c("https://www.instagram.com/p/BTke9pwjEvu","AnotherWeblink"), stringsAsFactors=FALSE)

# remove original instagram link
df$link <- gsub("https://www.instagram.com/p/","",df$link)

ui <- basicPage(
  plotOutput("plot", click = "plot_click"),
  verbatimTextOutput("selection"),
  conditionalPanel("plot_click!=null",
                   h4(textOutput("nametext")),
                   uiOutput("frame"))
)

server <- function(input, output,session) {

  output$plot <- renderPlot({
    ggplot(data=df,aes(x=x,y=y))+
      geom_point()+
      scale_x_continuous(limits = c(0, 68))+
      scale_y_continuous(limits = c(0, 52.5))
  })

  output$selection <- renderPrint({
    nearPoints(df, input$plot_click)
  })

  info <- reactive({

    t <- as.data.frame(nearPoints(df, input$plot_click))
    s <- t[1,4]
    u <- t[1,5]
    list(s=s,u=u)

  })

  output$nametext <- renderText({if(!is.na(info()$s)){info()$s}})

  output$frame <- renderUI({
    click <- input$plot_click
    if(!is.null(click)){
      link = info()$u
      HTML(paste0('<iframe width="400" height="270" src="http://instagram.com/p/', link,"/embed",'" frameborder="0" allowfullscreen></iframe>'))
      }
  })

}

runApp(shinyApp(ui, server), launch.browser = TRUE)

我觉得还可以吧

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-16
    • 2018-06-07
    • 2022-01-02
    • 2011-08-13
    • 2016-01-15
    • 2011-11-23
    • 1970-01-01
    相关资源
    最近更新 更多