# Goal 1 - shortest piece of shiny code to do each of the following, don't move to next step until previous step is done and 
#          okayed by Jeff.  Include comments for what is happening, and with links to where you got information.
#        - (a) Select a csv file (get that working first)
#        - (b) read.csv, and display summary and head of the file (get that working)
#        - (c) boxplot
#        - (d) UI to select which columns we want - we need to talk about that after (a)-(c) are done

# Also - hackerrank problems, store your solutions in your permanent CS account

#method of uploading files, summarizing, and boxplotting
#https://www.youtube.com/watch?v=HPZSunrSo5M&t=59s
#Youtube tutorial videos R Shiny App Tutorial # 15 (a) by Abhinav Agrawal
#Also, https://shiny.rstudio.com/tutorial/#written-tutorials


# make it so can upload larger files, size is in bytes
options(shiny.maxRequestSize=30*1024**2) 

library(shiny)
library(ggplot2)

ui <- fluidPage(
  shinyUI(fluidPage(
    titlePanel("Select Data File"),
    sidebarLayout(
      sidebarPanel(
        fileInput("file","Select csv file", multiple = FALSE), 
        helpText("Note - max file size is 30MB"), 
        checkboxInput(inputId = 'header', label = 'Header', value = TRUE),
        radioButtons(inputId = 'sep', label = 'Separator', 
                     choices = c(Comma=',', Tab='\t'), selected = ','),
        textInput(inputId="skipLines", "Lines to skip", value="0"),
        checkboxInput(inputId = 'log2', label = 'Apply log2', value = FALSE),
        selectInput(inputId = "columns_to_boxplot", label="Columns to boxplot",
                    multiple=TRUE, choices="", size=5, selectize=FALSE),
        submitButton(text = "Apply Changes")
      ),
      
      mainPanel(
        tabsetPanel(
          tabPanel("Dataset (head)", tableOutput("table")),
          tabPanel("Summary", verbatimTextOutput("summary")),
          tabPanel("Boxplot", plotOutput("boxplot"))
        )
      )
    )
  ))
)

server <- function(input, output, session) {
  data <- reactive({
    if(is.null(input$file)){
      NULL
    }
    else {
      t <- read.table(file=input$file$datapath, sep=input$sep, header = input$header,
                 skip=input$skipLines)
      if (input$log2) {
        num_cols <- unlist(lapply(t, is.numeric))
        t[,num_cols] <- log2(t[,num_cols]+1)
      }
      updateSelectInput(session, inputId = "columns_to_boxplot", label="Columns to boxplot", 
                   choices=colnames(t), selected=colnames(t))
      t
    }
  })
  
#  columns_to_boxplot <- reactive({
#    input$columns
#  })
  
  # creates input$file as a data frame that stores the contents of the file I.E. name and temporary location
  output$file <- renderTable({
    if(! is.null(input$file)){
      input$file 
    }
  })
  
  
  
  #Creates the dataset from read.table
  #read.table reads in a file in a table format and creates the dataframe
  output$table <- renderTable({ 
    if(! is.null(data())){
      head(data())
    }
  })

  #Summary function, applies summary to read.table.
  output$summary <- renderPrint({
    if(! is.null(data())){
      summary(data())
    }
  })
  
  #Boxplot function
  output$boxplot <- renderPlot({
    if(! is.null(data())){
      d <- data()[,input$columns_to_boxplot]
      print(input$columns_to_boxplot)
      boxplot(d)
    }
  })
  
}


# Create Shiny app ----
shinyApp(ui, server)