问题
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?
回答1:
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.
回答2:
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:
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)
来源:https://stackoverflow.com/questions/36850114/uploading-many-files-in-shiny