Embed instagram/youtube into Shiny R app
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>'))
}
})
and on ui side:
uiOutput("video")
Controlling an embedded YouTube video when observing another event in R Shiny
I found the answer to my own question if anyone needs it:
The javascript code was to be included in the server part only.
I included the onclick event in the UI:
library(shiny)
library(shinyjs)
library(shinydashboard)
ui <-
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs(),
fluidPage(uiOutput("video")),
img(id = 'image', src = 'http://www.zorro.com/wp-content/uploads/2014/04/005.jpg', onclick = 'myFunction()'),
)
)
output$video <- renderUI({
HTML('<div id="player"></div>
<script>
var tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player("player", {
height: "390",
width: "640",
videoId: "', input$modal_youtube_link, '"
playerVars: { "autoplay": 1, "modestbranding": 1 },
events: {}
});
}
var myFunction = function(){
player.playVideo();
</script>')})
shinyApp(ui, server)
embed iframe inside shiny app
library(shiny)
members <- data.frame(name=c("Name 1", "Name 2"), nr=c('BCRA1','FITM2'))
ui <- fluidPage(titlePanel("Getting Iframe"),
sidebarLayout(
sidebarPanel(
fluidRow(
column(6, selectInput("Member", label=h5("Choose a option"),choices=c('BCRA1','FITM2'))
))),
mainPanel(fluidRow(
htmlOutput("frame")
)
)
))
server <- function(input, output) {
observe({
query <- members[which(members$nr==input$Member),2]
test <<- paste0("http://news.scibite.com/scibites/news.html?q=GENE$",query)
})
output$frame <- renderUI({
input$Member
my_test <- tags$iframe(src=test, height=600, width=535)
print(my_test)
my_test
})
}
shinyApp(ui, server)
R Shiny - Passing Youtube links to iframe
You need to render the html from the server. This can be done using renderUI in the server and htmlOutput in the ui.
This simple app may help get you started:
shinyApp(
ui = fluidPage(titlePanel("Title"),
mainPanel(htmlOutput("video"))),
server = function(input, output, session) {
output$video <- renderUI({
tags$iframe(src = "https://www.youtube.com/embed/XQTAD9P5A1U")
})
}
)
Also, here it is with action buttons:
ui <- fluidPage(titlePanel("Title"),
sidebarLayout(
sidebarPanel(
actionButton("analyse1", "Link 1"),
actionButton("analyse2", "Link 2")
),
mainPanel(htmlOutput("video"))
))
server <- function(input, output, session) {
terms1 <- reactiveValues(link = NULL)
observeEvent(input$analyse1, {
terms1$link <- "https://www.youtube.com/embed/XQTAD9P5A1U"
})
observeEvent(input$analyse2, {
terms1$link <- "https://www.youtube.com/embed/em1u7iO9D1k"
})
output$video <- renderUI({
tags$iframe(src = terms1$link, width = 600, height = 400)
})
}
shinyApp(ui = ui, server = server)
Embed Video with Live Controls into Interactive Shiny Environment
Its quite easy to add a video. To do so please structure your app like so:
server.r
library(shiny)
shinyServer(function(input, output, session) {})
ui.r
shinyUI(
fluidPage(
tags$video(id="video2", type = "video/mp4",src = "SampleVideo_1280x720_1mb.mp4", controls = "controls")
)
)
www - this is where your video is, here I have .mp4
format video, downloaded from http://www.sample-videos.com/
How to upload a video file to website created using shiny package in RStudio
To comeback to my 'quick' fix solution is to use iframe
, you can upload the video onto youtube and then use the iframe to show it, something like this:
rm(list = ls())
library(shiny)
ui <- pageWithSidebar(
headerPanel("Welcome!"),
sidebarPanel(),
mainPanel( HTML('<iframe width="600" height="400" src="//www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>')
)
)
server <- function(input, output,session) {}
shinyApp(ui = ui, server = server)
Related Topics
How to Get The R Shiny Downloadhandler Filename to Work
Error with Scale_X_Labels in Ggplot2
Robust Standard Errors for Mixed-Effects Models in Lme4 Package of R
Line Spacing for Wrapped Text in Ggplot
How to Programmatically Create Binary Columns Based on a Categorical Variable in Data.Table
Label_Parsed of Facet_Grid in Ggplot2 Mixed with Spaces and Expressions
What Does The "More Columns Than Column Names" Error Mean
Change Plot Panel in Multipanel Plot in R
How to Convert a Data Frame of Integer64 Values to Be a Matrix
Under What Circumstances Does R Recycle
Run R Interactively from Rscript
Remove Blank Lines from Plot Geom_Tile Ggplot
Tiff Plot Generation and Compression: R VS. Gimp VS. Irfanview VS. Photoshop File Sizes
Terminating an Apply-Based Function Early (Similar to Break)