【问题标题】:upload and view a pdf in R shiny在 R Shiny 中上传和查看 pdf
【发布时间】:2018-05-03 21:01:19
【问题描述】:

我在 R 中有一个简单的闪亮应用程序,用于从用户那里读取 PDF 文件并显示它。我似乎无法让它工作。在 www 目录中的闪亮服务器上,我看到一个名为“myreport.pdf”的 1 KB 文件,它只有第一个字符

library(shiny)

ui <- shinyUI(fluidPage(

  titlePanel("Testing File upload"),

  sidebarLayout(
    sidebarPanel(
      fileInput('file_input', 'upload file ( . pdf format only)', accept = c('.pdf'))
    ),

    mainPanel(
      uiOutput("pdfview")
    )
  )
))

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

  observe({
    req(input$file_input)
    test_file <- readBin(input$file_input$datapath, what="character") 
    writeBin(test_file, "www/myreport.pdf")
  })


  output$pdfview <- renderUI({
    tags$iframe(style="height:600px; width:100%", src="myreport.pdf")
  })

})

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r pdf shiny


    【解决方案1】:

    我认为问题在于二进制读写。相反,尝试使用 file.copy 复制文件似乎可行。此外,我在observeEvent 中使用了iframe,以便每次在同一会话中上传 pdf 时更新 iframe。

    更新代码:

    library(shiny)
    
    ui <- shinyUI(fluidPage(
    
      titlePanel("Testing File upload"),
    
      sidebarLayout(
        sidebarPanel(
          fileInput('file_input', 'upload file ( . pdf format only)', accept = c('.pdf'))
        ),
    
        mainPanel(
          uiOutput("pdfview")
        )
      )
    ))
    
    server <- shinyServer(function(input, output) {
    
      observe({
        req(input$file_input)
    
         #test_file <- readBin(input$file_input$datapath, what="raw") 
    
         #writeBin(test_file, "myreport.pdf")
    
         #cat(input$file_input$datapath)
    
         file.copy(input$file_input$datapath,"www", overwrite = T)
    
    
    
      output$pdfview <- renderUI({
        tags$iframe(style="height:600px; width:100%", src="0.pdf")
      })
    
      })
    
    })
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 我已尝试运行您的代码并上传 pdf,但在 pdfview 中它只是显示未找到,有什么想法吗?谢谢!
    • 我将 src = "0.pdf" 更改为 "input$file_input$datapath" 但它显示空白信息
    • 您需要在运行上述块之前创建文件夹www并将file.copy(input$file_input$datapath, "www", overwrite = T)替换为file.copy(input$file_input$datapath, "www/0.pdf", overwrite = T)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2019-06-10
    • 2019-10-16
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    相关资源
    最近更新 更多