首页 > 解决方案 > r闪亮的pdf显示为空白

问题描述

我正在尝试在闪亮的应用程序中显示导入的 pdf,下面是可重现的代码,但 pdf 查看器在导入后是空白的,也没有显示任何错误。不知道如何使它工作。

library(tidyverse)
library(shiny)
library(pdftools)

ui <- fluidPage(
  titlePanel("Demo"),
  fileInput("file_import", "Upload Files", multiple = T, accept = ".pdf"),
  tableOutput("files"),
  uiOutput("pdfview")
)

server <- function(input, output, session) {
  output$files <- renderTable({input$file_import})
  
  observeEvent(input$file_import,{
    
    output$pdfview <- renderUI({
      
        tags$iframe(style = 'height: 680px; width: 960px;scrolling=yes',
                    src = input$file_import$datapath)
      
    })
    
  })
}

shinyApp(ui, server)

标签: rpdfiframeshiny

解决方案


以下工作正常。

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)
    
    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)

请确保您有一个www文件夹并重新启动 RStudio。它应该在浏览器中本地工作。然后尝试部署并测试它 - 在确保www您保存app.R.


推荐阅读