File size: 1,476 Bytes
002eac9
2f4e3dc
002eac9
2f4e3dc
 
 
 
 
 
 
 
 
 
 
002eac9
2f4e3dc
 
 
 
002eac9
 
 
2f4e3dc
 
 
 
 
 
 
 
 
 
 
 
 
 
002eac9
2f4e3dc
 
 
 
 
 
 
 
 
 
 
 
 
002eac9
2f4e3dc
 
 
002eac9
 
 
2f4e3dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
library(shiny)
library(data.table)
library(dplyr)
library(shinycssloaders)
options(shiny.maxRequestSize = 100000 * 1024^2)
ui <- fluidPage(
  titlePanel("CSV File Combiner"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file_input", "Choose CSV Files", multiple = TRUE, accept = c("text/csv", ".csv")),
      actionButton("combine_button", "Combine Files"),
      br(),
      br(),
      downloadButton("download_data", "Download Combined CSV")
    ),
    mainPanel(
      withSpinner(tableOutput("preview_table"))
    )
  )
)

server <- function(input, output, session) {
  combined_data <- reactiveVal(NULL)
  
  observeEvent(input$combine_button, {
    req(input$file_input)
    
    withProgress(message = 'Combining files', value = 0, {
      combined <- data.frame()
      
      for (i in 1:nrow(input$file_input)) {
        file <- input$file_input$datapath[i]
        data <- fread(file)
        combined <- rbind(combined, data)
        
        incProgress(1/nrow(input$file_input), detail = paste("Processing file", i))
      }
      
      combined_data(combined)
    })
  })
  
  output$preview_table <- renderTable({
    req(combined_data())
    head(combined_data(), 10)
  })
  
  output$download_data <- downloadHandler(
    filename = function() {
      paste("combined_data_", Sys.Date(), ".csv", sep = "")
    },
    content = function(file) {
      write.csv(combined_data(), file, row.names = FALSE)
    }
  )
}

shinyApp(ui = ui, server = server)