如何将视频文件上传到使用 RStudio 中的闪亮包创建的网站

How to upload a video file to website created using shiny package in RStudio

我正在学习如何使用 Rstudio 中的 Shiny 包将视频上传到网络上。以下是 ui.R

的代码
library(shiny)
shinyUI(fluidPage(
headerPanel("Shiny App Example with Video and Image"),

 sidebarLayout(
 sidebarPanel(
  fileInput("file","Upload the file", multiple = T), 
  h6("Default max. file size is 5MB"),
  hr(),
  h5("Select the read.table parameters below"),
  checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
  checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
  br(),
  radioButtons(inputId = 'sep', label = 'Separator', choices =     c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ','),
  h6(" Powered by:"),
  tags$img(src='RStudio-Ball.png', height=50, width=50)

  ),

mainPanel(
  uiOutput("tb")
  )

)
))

下一个代码是我的 server.R

library(shiny)

shinyServer(function(input,output){

data <- reactive({
file1 <- input$file
if(is.null(file1)){return()} 
read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

})

output$filedf <- renderTable({
if(is.null(data())){return ()}
input$file

})

output$sum <- renderTable({
if(is.null(data())){return ()}
summary(data())

 })

output$table <- renderTable({
if(is.null(data())){return ()}
data()
})

output$tb <- renderUI({
if(is.null(data()))
  h6("intro video", br(), tags$video(src='reactive.mp4', type="video/mp4", width="350px", height="350px", controls="controls"))
else
  tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
 })
})

我可以启动网站了。从那里使用 "Upload" 按钮,我可以导航到我的 "reactive.mp4" 文件所在的文件夹。但它在主面板中提供了以下错误消息:"empty beginning of file"

在 Rstudio 控制台中,错误如下:

Error in read.table(file = file1$datapath, sep = input$sep, header = input$header,  : 
empty beginning of file
Warning in run(timeoutMs) : line 1 appears to contain embedded nulls
Warning in run(timeoutMs) :
incomplete final line found by readTableHeader on 'C:\Users\Lab-   User\AppData\Local\Temp\RtmpMxaMe9/8fe8ab38479dea3acaf755a8/0'

我也试过更改视频文件的编码。不幸的是,它不播放视频。

解决方案:

下面那位给出的解决方案我没有试过。因为我不想在 Youtube 上上传视频,然后将其托管在网络上。

所以有一次,用户点击了Rstudio控制台上的"Launch App"按钮。它将提示用户访问本地网站。当我使用浏览器打开网站时(按钮名称是 "Open in Browser",可以在左上角找到)。它会自动显示使用浏览器的网站。有时在 windows 中,当你从 Rstudio 本地 运行 时,当你在浏览器中 运行 时,行为是不同的。

回到我的 'quick' 修复解决方案是使用 iframe,您可以将视频上传到 youtube,然后使用 iframe 显示它,如下所示:

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)