R Shiny 导入多个 PDF 并使用 actionButton 一个一个地查看它们

R Shiny import multiple PDFs and view them one by one using actionButton

我有以下应用程序可以在导入后查看一个 pdf。但是我希望能够导入多个 PDF(这已经可以完成),然后单击 Next PDF 操作按钮查看下一个 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)

我想出了办法!我对变量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)