将 instagram/youtube 嵌入到 Shiny R 应用程序中 [英] Embed instagram/youtube into Shiny R app
本文介绍了将 instagram/youtube 嵌入到 Shiny R 应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过点击图表来播放 Instagram 或 Youtube 视频(例如显示异常值等)
I would like to play an instagram or Youtube video upon a click of a graph (e.g. showing what an outlier could be etc)
到目前为止,明确告诉 Shiny 视频是什么:
So far, telling Shiny explicitly what the video is works:
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)
我希望视频只在点击时出现(并且不同点会有所不同)但是我不知道如何更改conditionalPanel 以适应此情况.我尝试在服务器内部使用 renderImage,还有这个 GoogleGroups answer 但没有快乐.
I would like the video only to appear on click (and be different for different points) however I don't know how to change the conditionalPanel to suit this. I tried with renderImage inside server, and also this GoogleGroups answer but got no joy.
推荐答案
在服务器端你可以使用:
On server side you can use:
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")
这篇关于将 instagram/youtube 嵌入到 Shiny R 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文