Andres Mora
Andres Mora

Reputation: 1117

How to integrate code into a shiny app and upload files?

I'm new to shiny and I would like your advice on a requirement that I have at my office. I apologize in advance for not providing more information or code at the moment. I have currently coded a R script that does the following:

Im requiring a shiny web app that:

I might be asking a lot. I will appreciate if you just can give me some lights in how i can start working on this. I would like not to modify my current code, if possible (can i have shiny acquire the files, and call my code so it can process them?)

Upvotes: 0

Views: 467

Answers (1)

Ash
Ash

Reputation: 1513

Here is a minimal example showing uploading files, processing them, and downloading them.

For simplicity I've used 3 inputs and a single output.

If you want to notify a user that something has happened, you can use showNotification()

library(shiny)

ui <- fluidPage(
  
    #File Upload Boxes
    fileInput("myfileinput_1", label =  "Upload File 1", accept = ".csv"),
    fileInput("myfileinput_2", label =  "Upload File 2", accept = ".csv"),
    fileInput("myfileinput_3", label =  "Upload File 3", accept = ".csv"),
    
    #Button
    actionButton("mybutton", label =  "Process Uploaded Files"),
    
    #Table Showing Processed Data
    tableOutput("mytable"),
    
    #Download Buttons
    downloadButton("myfiledownload", label = "Download Processed File")
    
)

server <- function(input, output, session) {

    #A reactive dataframe to store our outputfile
    reactives <- reactiveValues(
      
      df_output = NULL
      
    )
    
    #Runs when button is pressed
    observeEvent(input$mybutton, {
        
        #Check that all 3 files are selected before loading
        if(!is.null(input$myfileinput_1) & !is.null(input$myfileinput_2) & !is.null(input$myfileinput_3)) {
        
            #Load input files
            df_input_1 <- read.csv(input$myfileinput_1$datapath)
            df_input_2 <- read.csv(input$myfileinput_2$datapath)
            df_input_3 <- read.csv(input$myfileinput_3$datapath)
            
            #Use input to create an output (we're just using a simple example)
            reactives$df_output <- data.frame(
                input = c("Input 1", "Input 2", "Input 3"),
                rows = c(nrow(df_input_1), nrow(df_input_2), nrow(df_input_3))
            )
            
            showNotification("Files Successfully Processed", type = "message")
            
        } else {
            
            showNotification("Ensure all three files are selected before loading", type  = "error")
            
        }

    })
    
    #Table Output
    output$mytable <- renderTable({
      
        reactives$df_output
      
    })
    
    #Download handler
    output$myfiledownload <- downloadHandler(
        
        filename = "mydata.csv",
        content = function(file) {write.csv(reactives$df_output, file, row.names = FALSE)}
        
    )
    
}

shinyApp(ui, server)

Upvotes: 1

Related Questions