---
title: "Preprocessing"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(psycho)
library(dplyr)
library(datasets)
```

```{r,echo=FALSE}
sidebarPanel(
  checkboxInput("ex","Uncheck for using your own file",value = TRUE),
  fileInput("file", "Upload the *.csv file with headers"),
  checkboxInput("std","STANDARDIZE",value = FALSE),
  checkboxInput("rmo","REMOVE MISSING OBSERVATIONS",value = FALSE),
  checkboxInput("shu","SHUFFLE THE OBSERVATIONS",value = FALSE),
  downloadButton("downloaddata", "DOWNLOAD DATASET"),
  uiOutput("vx"),
  selectInput("sel","Select All/SelectNone",choices = c("all","none"),selected = "all")
  
  
)
mainPanel(
   tabsetPanel(type = "tab",
              tabPanel("Summary", verbatimTextOutput("AD")),
              h6("", tags$img(src ="K.JPG", height= 400, width=400))
   )
)
output$AD<-renderPrint({
  
  if(input$ex == TRUE)
     {data("iris")
      data = iris}
     else
     {
  file1 = input$file
     if(is.null(file1)){return()}
     
     data =  read.table(file =  file1$datapath,sep =",",header = TRUE)
     if(is.null(data())){return()}
     }
     ds = data
     if(input$rmo == TRUE)
     {
      
       ds = ds[complete.cases(ds),]
       
     }
      if(input$std == TRUE)
     {
       ds = data.frame(standardize(ds))
       
      }
     ds = select(ds,input$variablex)
     
     if(input$shu ==  TRUE)
     {set.seed(1)
       gp =runif(nrow(ds))
       ds =  ds[order(gp),]
       
       }
    cat(sprintf("\nSnapshot of the dataset\n"))
   print(head(ds,6))
    cat(sprintf("\nSummary of the dataset\n"))
     print (summary(ds))
})

datasetInput1 <- reactive({
  
    if(input$ex == TRUE)
     {data("iris")
      data = iris}
     else
     {
  file1 = input$file
     if(is.null(file1)){return()}
     
     data =  read.table(file =  file1$datapath,sep =",",header = TRUE)
     if(is.null(data())){return()}
     }
     
     ds = data
     if(input$rmo == TRUE && input$std == TRUE)
     {
       ds = ds[complete.cases(ds),]
       ds = data.frame(standardize(ds))
       
     }
     else if(input$rmo == TRUE && input$std == FALSE)
     {
       ds = ds[complete.cases(ds),]
      
       
     }
     else if(input$rmo == FALSE && input$std == TRUE)
     {
       
       ds = data.frame(standardize(ds))
       
     }
     else if(input$rmo == FALSE && input$std == FALSE)
     {
       ds = data
       
     }
     
    ds = select(ds,input$variablex) 
    
    if(input$shu ==  TRUE)
     {set.seed(1)
       gp =runif(nrow(ds))
       ds =  ds[order(gp),]
       
    }
    else{
      
      ds = ds
    
    
    }
       
  })

output$downloaddata <- downloadHandler(
    filename = function() {
      filetitle = paste("dataset")
      paste(filetitle, ".csv", sep = "")
    },
    content = function(file) {
      
      write.csv(datasetInput1(), file, row.names = FALSE)
    }
  )

output$vx <- renderUI({
   

    if(input$ex == TRUE)
     {data("iris")
      data = iris}
     else
     {
  file1 = input$file
     if(is.null(file1)){return()}
     
     data =  read.table(file =  file1$datapath,sep =",",header = TRUE)
     if(is.null(data())){return()}
     }
    
     
    
    if(input$sel == "all") 
    {checkboxGroupInput("variablex","Select the variables",choices = colnames(data),selected = colnames(data) )}
     else {
       checkboxGroupInput("variablex","Select the variables",choices = colnames(data),selected = "")}


  })
