how do I upload multiple files with fileInput() ?

5,203 views
Skip to first unread message

zak gezon

unread,
Apr 3, 2013, 5:23:58 PM4/3/13
to shiny-...@googlegroups.com

I want to write a program that merges two datasets.  It is pretty straight forward, but I am having trouble figuring out how exactly to upload multiple files using fileInput(), and I haven't seen any example code so far.  

Here is what I have so far (which doesn't work).  Any advice would be appreciated! 

ui.R
shinyUI(pageWithSidebar(
  
  # title
  headerPanel("Update bee data with species IDs"),
  
  sidebarPanel(
    fileInput('file1', 'First upload the raw bee data, then upload the species info',
    accept=c('text/csv', 'text/comma-separated-values,text/plain'), multiple=T),
    tags$hr(),
    downloadButton('downloadData', 'Download merged data'),
    tags$hr()
  ),
  
  mainPanel(
    #tableOutput('table')
  )
))

server.R
shinyServer(function(input,output) {
  data <- reactive({
    if (is.null(input$file1)) {
      return(NULL)
    }
    rawData <- read.csv(input$file1$datapath)  # read in raw data
    speciesInfo <- read.csv(input$file2$datapath)  # read in species info
    
    merged <- merge(x=rawData,y=speciesInfo,by='Bee.ID.',all.x=T,all.y=F)
    
    merged <- (merged[,c(2:9,1,10:12,20:26)])
    
    names(merged)<-names(rawData)
    merged
  })
  
  output$table <- renderTable({ 
    data()                            
  })
  
   output$downloadData <- downloadHandler(
    filename = function() { "bee data with species info.csv" },
    content = function(file) {
      write.csv(data(), file)
    }
  )
})

Joe Cheng

unread,
Apr 3, 2013, 5:48:21 PM4/3/13
to shiny-...@googlegroups.com
If you want them to upload two separate files in order, then probably the easiest/best way to do that is to include two separate fileInput fields. This will let them change the file if they get it wrong.

In general though, when you upload multiple files through fileInput, then input$file1 (in your case) will be a dataframe where each row is a different file. So to retrieve the first file's datapath it would be input$file1[[1, 'datapath']] and for the second, input$file1[[2, 'datapath']].


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jamie M. Kass

unread,
Feb 16, 2015, 2:30:00 AM2/16/15
to shiny-...@googlegroups.com
Not sure what's wrong, but I cannot seem to select multiple files even when selecting multiple =TRUE for fileInput(). I am on Mac OS Yosemite and running the app on the latest RStudio. Has anyone else experienced this problem recently?

Verena Haunschmid

unread,
Mar 4, 2015, 8:47:52 AM3/4/15
to shiny-...@googlegroups.com
I have the exact same problem with the same setup as you. It works on my Windows Virtual Machine, no idea how to solve this :/

Manbir Mohindru

unread,
Mar 4, 2015, 10:35:50 PM3/4/15
to shiny-...@googlegroups.com
I somewhat remember a similar problem i faced with fileinput(). I instead used choose.files() which allowed to accomplish the multiple file select. The other reason i used choose.files() was to expose the full file source path. 

Yihui Xie

unread,
Mar 5, 2015, 8:32:36 AM3/5/15
to Jamie M. Kass, shiny-discuss
Can you open the app in your web browser and try again? It might be a
problem of the RStudio viewer.

Regards,
Yihui

Yihui Xie

unread,
Mar 5, 2015, 8:35:00 AM3/5/15
to Manbir Mohindru, shiny-discuss
But note that choose.files() is Windows only, and works only when the
app is viewed locally. It will not work when the app is viewed on
other platforms, or on the server.

Regards,
Yihui

Verena Haunschmid

unread,
Mar 5, 2015, 8:58:38 AM3/5/15
to shiny-...@googlegroups.com, mohi...@gmail.com
I now use choose.files() because I can live with the limitations and it actually better suits my needs, because I don't want the files to be uploaded immediately :)

Yihui Xie

unread,
Mar 5, 2015, 9:01:24 AM3/5/15
to Verena Haunschmid, shiny-discuss, mohi...@gmail.com
That makes sense, of course :)

Regards,
Yihui
> --
> You received this message because you are subscribed to the Google Groups
> "Shiny - Web Framework for R" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to shiny-discus...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/shiny-discuss/3a99e040-6955-4d34-85da-b37b92595b57%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Manbir Mohindru

unread,
Mar 5, 2015, 9:09:12 AM3/5/15
to shiny-...@googlegroups.com
So if choose.files () does not work from outside local machin . Is fileInput () the only answer ?

Yihui Xie

unread,
Mar 5, 2015, 9:18:44 AM3/5/15
to Manbir Mohindru, shiny-discuss
I think the answer is yes.

Regards,
Yihui

Verena Haunschmid

unread,
Mar 5, 2015, 9:31:19 AM3/5/15
to shiny-...@googlegroups.com
I also saw this when I was looking for a solution: http://cran.r-project.org/web/packages/shinyFiles/shinyFiles.pdf
But I don't exactly know what it does, you could check it out :)

Manbir Mohindru

unread,
Mar 5, 2015, 1:30:59 PM3/5/15
to Verena Haunschmid, shiny-...@googlegroups.com

Thanks. I saw it too. As the description reads, it's for navigating the server side file system and not client side unless of course the app is running locally.

So in regards to the whole topic of managing fileInput (), I need it to do 2 things:
1) not load the file right away - not tried it but think can work with server side observe ()

2) give me the full path of the source file. Not the temp location that is exposed via $ datapath but the original location of the file

I need to pass the full path to another program hence need to do this.

--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/M2gRfaIP3Dc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e0990c87-0db8-48f6-bbcb-7fc41df27f05%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jamie Kass

unread,
Jul 2, 2015, 10:49:21 PM7/2/15
to shiny-...@googlegroups.com, ndimhy...@gmail.com
Yihui,

Yes, you were right. Thanks for the advice. Running my app in my browser allowed me to multi-select. Wonder why this doesn't work in RStudio though.

Boris Nguema

unread,
Nov 4, 2016, 11:25:11 AM11/4/16
to Shiny - Web Framework for R
Hi, i've used the multiple file selection... Is there any way to iterate through the selection on the server's side? Something like foreach "input$fileInput do something...". Obviously the server doesn't know yet how many files are loaded so i can't just do it with "foreach(i=1:10) %do% something". 
Reply all
Reply to author
Forward
0 new messages