【问题标题】:R Shiny import multiple PDFs and view them one by one using actionButtonR Shiny 导入多个 PDF 并使用 actionButton 逐一查看
【发布时间】:2021-07-15 19:30:52
【问题描述】:

我有以下应用程序,可以在导入后查看一个 pdf。但是我希望能够导入多个 PDF(这已经可以完成),然后单击 Next PDF actionButton 以查看下一个 PDF。一直到最后一个导入的PDF,怎么办?

如果以下代码无法查看一个 pdf,请确保您在 app.R 的同一目录中有一个 www 文件夹。

library(shiny)

ui <- shinyUI(fluidPage(
  
  titlePanel("Testing File upload"),
  
  sidebarLayout(
    sidebarPanel(
      fileInput('file_input', 'upload file ( . pdf format only)',
                accept = c('.pdf'),multiple = T),
      actionButton("next_pdf", "Next 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:1200px; width:100%", src="0.pdf")
    })
    
  })
  
  
  
  
})

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r pdf shiny


    【解决方案1】:

    我已经找到了解决方案!我对变量x使用reactiveVal,每次点击actionButton,x会增加1。在此基础上,我可以通过指定datapath[]来逐个查看PDF。它对于拥有多个 pdf 文件的人非常有用。

    library(shiny)
    
    ui <- shinyUI(fluidPage(
      
      titlePanel("Testing File upload"),
      
      sidebarLayout(
        sidebarPanel(
          fileInput('file_input', 'upload file ( . pdf format only)',
                    accept = c('.pdf'),multiple = T),
          tableOutput("files"),
          actionButton("next_pdf", "Next PDF"),
          textOutput("testing")
        ),
        
        mainPanel(
          uiOutput("pdfview")
        )
      )
    ))
    
    server <- shinyServer(function(input, output) {
      
      x = reactiveVal(1)
      
      output$files <- renderTable({input$file_input})
      
      observeEvent(input$file_input,{
        
        file.copy(input$file_input$datapath[1],"www", overwrite = T)
        
        output$pdfview <- renderUI({
          tags$iframe(style="height:1200px; width:100%", src="0.pdf")
        })
        
      })
      
      observeEvent(input$next_pdf,{
          x(x()+1) 
        
        file.rename(input$file_input$datapath[x()], "0.pdf")
        file.copy("0.pdf","www", overwrite = T)
        
        output$pdfview <- renderUI({
          tags$iframe(style="height:1200px; width:100%", src="0.pdf")
        })
        
        output$testing = renderText(x())
      })
      
      
      
    })
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2018-05-03
      • 2020-10-22
      • 2021-04-05
      • 1970-01-01
      • 2018-06-11
      • 2018-05-18
      • 2021-11-11
      • 2020-07-29
      相关资源
      最近更新 更多