R Shiny 中视频的交互式渲染
Interactive rendering for a video in R Shiny
对于来自用户的每个输入,我想根据不同的 src 渲染不同的视频,
我已经测试了很多方法,但没有一种方法满足我的需求,我依赖于从 CSV 文件中的特定单元格中提取 src 路径
请你帮我弄清楚如何做到这一点,以下行是需要调整以解决问题的主要部分:
tags$iframe(width="560", height="315", src=uiOutput('link1'))
library(shiny)
Targeted_sector<- c('Wastewater Treatment','Agriculture Schemes','Dams' )
Targeted_year<- c('2021','2022','2023' )
Wastewater_Treatment<- read.csv("data/Wastewater_Treatment.csv", header=TRUE,
check.names=FALSE)
Agriculture_Schemes<- read.csv("data/Agriculture_Schemes.csv", header=TRUE,
check.names=FALSE)
Dams<- read.csv("data/Dams.csv", header=TRUE, check.names=FALSE)
ui <-
fluidPage(
sidebarPanel(
selectInput('sector',
'Targeted Sector:',
Targeted_sector),
selectInput('year1',
'Targeted Year:',
Targeted_year)
)
)
mainPanel(
tags$iframe(width="560", height="315", src=uiOutput('link1'), frameborder="0",
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture",
allowfullscreen=NA)
# here I want src to change dynamically according to the input$sector
)
server <- function(input, output) {
SelectSector <-reactive({
if (input$sector == 'Wastewater Treatment')
{
return(Wastewater_Treatment)
}
else if (input$sector == 'Agriculture Schemes')
{
return(Agriculture_Schemes)
}
else
{
return(Dams)
}
})
SelectYear <- reactive({
if (input$year1 %in% SelectSector()$years)
{
SelectSector() %>%
filter(years == input$year1) %>%
pull(link) # This contain the targeted link for example"https://www.youtube.com/watchv=L7KSftmYce0" but it did not work
}
})
output$link1 <-
renderUI({
tagList(SelectYear())
})
}
shinyApp(ui = ui, server = server)
如果视频在 YouTube 上,您可以只使用他们的标准嵌入式 iframe。仅使用视频 ID,因为您要使用不同的 URL (https://www.youtube.com/embed/ instead of https://www.youtube.com/watch)。用选定的 ID 创建一个字符串,让 shiny 告诉它是 HTML 代码而不是文本来呈现 UI:
library(shiny)
library(dplyr)
videos <- data.frame(
name = c("A", "B"),
# youtube video ids (part of URL)
id = c("LXb3EKWsInQ", "xcJtL7QggTI")
)
ui <- fluidPage(
selectInput("name", "Name of Video to watch", c("A", "B")),
uiOutput("video")
)
server <- function(input, output, session) {
output$video <- renderUI({
id <- videos |>
filter(name == input$name) |>
pull(id) |>
first()
paste0(
"<iframe width='560' height='315' src='https://www.youtube.com/embed/",
id,
"' title='YouTube video player'",
"frameborder='0' allow='accelerometer; autoplay; clipboard-write;",
"encrypted-media;gyroscope; picture-in-picture' allowfullscreen></iframe>"
) |> HTML()
})
}
shinyApp(ui, server)
对于来自用户的每个输入,我想根据不同的 src 渲染不同的视频, 我已经测试了很多方法,但没有一种方法满足我的需求,我依赖于从 CSV 文件中的特定单元格中提取 src 路径
请你帮我弄清楚如何做到这一点,以下行是需要调整以解决问题的主要部分:
tags$iframe(width="560", height="315", src=uiOutput('link1'))
library(shiny)
Targeted_sector<- c('Wastewater Treatment','Agriculture Schemes','Dams' )
Targeted_year<- c('2021','2022','2023' )
Wastewater_Treatment<- read.csv("data/Wastewater_Treatment.csv", header=TRUE,
check.names=FALSE)
Agriculture_Schemes<- read.csv("data/Agriculture_Schemes.csv", header=TRUE,
check.names=FALSE)
Dams<- read.csv("data/Dams.csv", header=TRUE, check.names=FALSE)
ui <-
fluidPage(
sidebarPanel(
selectInput('sector',
'Targeted Sector:',
Targeted_sector),
selectInput('year1',
'Targeted Year:',
Targeted_year)
)
)
mainPanel(
tags$iframe(width="560", height="315", src=uiOutput('link1'), frameborder="0",
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture",
allowfullscreen=NA)
# here I want src to change dynamically according to the input$sector
)
server <- function(input, output) {
SelectSector <-reactive({
if (input$sector == 'Wastewater Treatment')
{
return(Wastewater_Treatment)
}
else if (input$sector == 'Agriculture Schemes')
{
return(Agriculture_Schemes)
}
else
{
return(Dams)
}
})
SelectYear <- reactive({
if (input$year1 %in% SelectSector()$years)
{
SelectSector() %>%
filter(years == input$year1) %>%
pull(link) # This contain the targeted link for example"https://www.youtube.com/watchv=L7KSftmYce0" but it did not work
}
})
output$link1 <-
renderUI({
tagList(SelectYear())
})
}
shinyApp(ui = ui, server = server)
如果视频在 YouTube 上,您可以只使用他们的标准嵌入式 iframe。仅使用视频 ID,因为您要使用不同的 URL (https://www.youtube.com/embed/ instead of https://www.youtube.com/watch)。用选定的 ID 创建一个字符串,让 shiny 告诉它是 HTML 代码而不是文本来呈现 UI:
library(shiny)
library(dplyr)
videos <- data.frame(
name = c("A", "B"),
# youtube video ids (part of URL)
id = c("LXb3EKWsInQ", "xcJtL7QggTI")
)
ui <- fluidPage(
selectInput("name", "Name of Video to watch", c("A", "B")),
uiOutput("video")
)
server <- function(input, output, session) {
output$video <- renderUI({
id <- videos |>
filter(name == input$name) |>
pull(id) |>
first()
paste0(
"<iframe width='560' height='315' src='https://www.youtube.com/embed/",
id,
"' title='YouTube video player'",
"frameborder='0' allow='accelerometer; autoplay; clipboard-write;",
"encrypted-media;gyroscope; picture-in-picture' allowfullscreen></iframe>"
) |> HTML()
})
}
shinyApp(ui, server)