Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading many files in Shiny

I am developing an app that helps to organize and visualize many PDF documents by topic/theme. I can upload and read a single PDF but I have difficulty in reading multiple PDF documents.

For single PDF document:

ui.R

  ---
  fileInput('file1', 'Choose PDF File', accept=c('.pdf'))

 ---

server.R

   --------

   library(pdftools)

   -------


 mypdf<-reactive({

   inFile <- input$file1

   if (is.null(inFile)){
  return(NULL)
  }else{
  pdf_text(inFile$datapath)

   }

  })

To upload multiple PDF files, I have to use multiple = TRUE in the ui.R portion of the code, but how can I read in all the uploaded files?

like image 929
Fisseha Berhane Avatar asked Dec 18 '22 17:12

Fisseha Berhane


2 Answers

I realise this question is older, but i was looking for the same answer and build a minimal app to test the functionality. The question is fully covered by the other answer, but as always reproducible code helps us all to save time, so i decided to share my minimal testing app.

Reproducible app:

# creating sample files to upload
write.csv2(
  x = "diff same", 
  file = "test.csv"
)

write.csv2(
  x = "diffhere same", 
  file = "test2.csv"
)


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput(
        inputId = "files", 
        label = "Choose CSV File", 
        multiple = TRUE,
        accept = c("text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      )
    ),
    mainPanel(
      tableOutput("contents")
    )
  )
)

server <- function(input, output) {
  output$contents <- renderTable({
    req(input$files)
    upload = list()

    for(nr in 1:length(input$files[, 1])){
      upload[[nr]] <- read.csv(
        file = input$files[[nr, 'datapath']]
      )
    }

    return(upload)
  })
}

shinyApp(ui, server)
like image 55
Tonio Liebrand Avatar answered Jan 02 '23 17:01

Tonio Liebrand


The uploaded files can be read in a for loop like this

for(i in 1:length(input$files[,1])){
  lst[[i]] <- read.csv(input$files[[i, 'datapath']])
}

This is an example for CSV files but you can do the same for pdf files.

like image 35
Xiongbing Jin Avatar answered Jan 02 '23 18:01

Xiongbing Jin