title: Cleaning a data frame inputed with fileInput and plotting it - R Shiny
body: <p><strong>Goal</strong>: Create a shiny app that can take in data from the user using the fileInput() function and then clean the data that is being brought in with the help of dplyr and a lot of other data manipulation techniques and then plot this data and display a table of the cleaned data and also have a download button to download the cleansed data.</p>
  
  <p><strong>What I have done</strong>: I have successfully created the app without the fileInput. I simply cleansed the data using a R script and then added the script before the user interface portion of the app and ran the app and it worked fine. However, this did not have user input capability.</p>
  
  <p>The code below is a simplified version of my ui and server side of the app that attempts to add user input functionality. </p>
  
  <pre><code>Dept &lt;- c(1,2,3)
  DepartmentName &lt;-c("abc","def","ghi")
  Dept_Names &lt;- cbind.data.frame(Dept,DepartmentName)
  
  ui &lt;- dashboardPage(   
    dashboardHeader(title="ABC"),
    dashboardSidebar(width = 150,
                     sidebarMenu(  
            menuItem("DataInput",tabName = "DataInput"),      
            menuItem("Dashboard", tabName = "Dashboard")
                     )),
    dashboardBody(
      tabItems(
        tabItem(tabName = "DataInput", 
                box(fileInput('file1', 'Choose CSV File',
                              accept=c('.csv')),
                    tags$hr(),
                    checkboxInput('header', 'Header', TRUE),
                    radioButtons('sep', 'Separator',
                                 c(Comma=',',
                                   Semicolon=';',
                                   Tab='\t'),
                                 ','),
                    actionButton("Load", "Load the File"))
        ),
        tabItem( tabName = "Dashboard",
                 fluidRow(column(9,box(status = "primary",plotOutput("plot1"))
                 ),
                 column(3,
                        box(title="Controls",
                            selectInput(inputId = "Dept", 
                                        label = "Select", 
                                        choices = C(1,2,3)), 
                            numericInput(inputId = "ClassNum", 
                                         label = "Enter the Class Number", value = 1, min=0, max=100000),
                            status = "warning", solidHeader = TRUE, width="100%"),
  
                        downloadButton('downloadData', 'Download')
  
                  )
                ), 
                 box(DT::dataTableOutput("table"), width = "100%,",status = "primary")
        )
      )
    )
  )
  
  server &lt;- function(input,output) { 
  
    data &lt;- reactive({
      if(input$Load == 0){return()}
      inFile &lt;- input$file1
      if (is.null(inFile)){return(NULL)}
  
      isolate({ 
        input$Load
        a &lt;- read.csv(inFile$datapath, header = input$header,sep = input$sep,stringsAsFactors =FALSE)
  
        options(stringsAsFactors = FALSE) 
        a &lt;- a[,1:5] 
        names(a) &lt;- c("Dept","Description","Price","ClassNum","Quantity") 
  
        a &lt;- a %&gt;%
          filter(Quantity &gt; 0) %&gt;%
          filter(!(Price==""))  
  
        for (i in 1:length(a)) {  
          for (j in 1:nrow(a)) {
            if (i==2) {next}
            a[j,i] &lt;- gsub(",", "", a[j,i])
          }  
        }  
        rm(i,j)  
  
        a &lt;- merge(a,Dept_Names, by="Dept")
  
        for (j in 1:nrow(a)) { 
          if (a$ClassNum[j]=="") {a[j,4] &lt;-0} else {a[j,4] &lt;- a$ClassNum[j]}
        } 
        rm(j)
  
        a$Dept &lt;- as.numeric(as.character(a$Dept))
        a$ClassNum &lt;- as.numeric(as.character(a$ClassNum))
        a$Price &lt;- as.numeric(as.character(a$Price))
        a$Quantity &lt;- as.numeric(as.character(a$Quantity))
  
        a &lt;- a %&gt;%
          mutate(Revenue=Price*Quantity,Key1=paste(Dept, "_", ClassNum, sep="")) 
  
        total_complete &lt;- a %&gt;%
          group_by(Dept, ClassNum) %&gt;%
          summarise(Revenue=sum(Revenue)) %&gt;%
          arrange(Dept, desc(Revenue)) %&gt;%
          mutate(Key1=paste(Dept, "_", ClassNum, sep="")) 
  
        ofn &lt;- paste0("ABC",Sys.time(),".csv")
        ofn &lt;- gsub(":","_",ofn)
  
      })
  
    })
  
  
    output$downloadData &lt;- downloadHandler(
      filename = data()$ofn,
      content = function(file) {
        write.csv(data()$total_complete, file, row.names = FALSE)
      }
    )
  
  
    output$table &lt;- DT::renderDataTable(DT::datatable({data1 &lt;- data()$total_complete[data()$total_complete[,1]==input$Dept ,]
    }))
  
    output$plot1 &lt;- renderPlot({
  
      ggplot(total_complete[total_complete$Department==input$Dept,], 
             aes(reorder(x=Key1,desc(Revenue)), y=Revenue, fill=Key1)) +
        geom_bar(stat="identity") +
        theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) 
    })
  
  
  
  } 
  
  
  
  shinyApp(ui, server)
  </code></pre>
  
  <p>The input data looks like this</p>
  
  <pre><code>Dept #  Description Retail  Class # Q
  10  MOP 11.99   100 1
  10  Broom   7.99    101 2
  10  soap    5.99    102 3
  10  key 7.99    103 4
  10  salmon  34.99   104 5
  10  steak   11.99   105 5
  10  wine    9.99    106 7
  </code></pre>
  
  <p>Thanks for all help. </p>