Agaz Wani
Agaz Wani

Reputation: 5694

How to load a folder of csv files in shiny

I have a folder of CSV files and i want to upload and access them as a list of files in shiny. I tried the following code to upload the files.

server: output$sourced <- renderDataTable({

        inFile <- input$file1

        if (is.null(inFile))
          return(NULL)

        df <- list.files(inFile$datapath)  #, header=input$header, sep=input$sep, quote=input$quote)

    }) 

ui.r:  fileInput("file1", "Choose CSV files from directory", multiple = "TRUE",
                accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),

Error for folder upload:

invalid 'description' argument

For one file its working fine if i use df <- read.csv(inFile$datapath) to load a file. But m not able to upload a folder. Help appreciated.

Upvotes: 4

Views: 3923

Answers (1)

redmode
redmode

Reputation: 4951

Assuming CSV files have the same structure and you want to merge them, minimal working example can be:

ui.R

library(shiny)

shinyUI(
  fluidPage(
    fileInput("file1",
              "Choose CSV files from directory",
              multiple = TRUE,
              accept=c('text/csv', 
                       'text/comma-separated-values,text/plain', 
                       '.csv')),
    dataTableOutput("sourced")
))

server.R

library(shiny)
library(dplyr)

shinyServer(function(input, output) {

  output$sourced <- renderDataTable({

    inFile <- input$file1
    if (is.null(inFile)) {
      return(NULL)
    } else {
      inFile %>%
        rowwise() %>%
        do({
          read.csv(.$datapath)
        })
    }
  }) 

})

Upvotes: 3

Related Questions