# load required libraries
library(shiny)
library(plyr)
library(ggplot2)
library(googleVis)
## 
## Welcome to googleVis version 0.5.10
## 
## Please read the Google API Terms of Use
## before you start using the package:
## https://developers.google.com/terms/
## 
## Note, the plot method of googleVis will by default use
## the standard browser to display its output.
## 
## See the googleVis package vignettes for more details,
## or visit http://github.com/mages/googleVis.
## 
## To suppress this message use:
## suppressPackageStartupMessages(library(googleVis))
library(reshape2)


shinyServer(function(input, output) {
  
  Data <- reactive({
              
          inFile <- input$file1
          
          if (is.null(inFile))
            return(NULL)
          
         df.raw <- read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
        
         # calculate term and pupil averages - No Changes in code but these are Day Averages ... 
          t1Av <- colMeans(df.raw[3:5])[1]
          t2Av <- colMeans(df.raw[3:5])[2]
          t3Av <- colMeans(df.raw[3:5])[3]
          df.raw$Av <- round(rowMeans(df.raw[3:5]),1)
          
          # reshape the data.frame for further analysis
          df.melt <- melt(df.raw, id.vars=c("P","V"))
          colnames(df.melt) <- c("ProductName","VendorName","Day","DaysPrice")
          
          # get average Amazon n Flipkart prices...
          Amazon <-round(tapply(X = df.melt$DaysPrice, INDEX = list(df.melt$VendorName), FUN = mean)["A"],1)
          Flipkart <-round(tapply(X = df.melt$DaysPrice, INDEX = list(df.melt$VendorName), FUN = mean)["F"],1)
         
          # create a list of data for use in rendering
          info <- list(df.raw=df.raw,df.melt=df.melt,t1Av=t1Av,t2Av=t2Av,t3Av=t3Av,Amazon=Amazon,Flipkart=Flipkart)
          return(info)
  })
   
  # allows pageability and number of rows setting
  myOptions <- reactive({  
    list(
      page=ifelse(input$pageable==TRUE,'enable','disable'),
      pageSize=input$pagesize
    ) 
   } )
  
  output$raw <- renderGvis({
   if (is.null(input$file1)) { return() }
  
     gvisTable(Data()$df.raw,options=myOptions())         
   })

  output$density <- renderPlot({
    if (is.null(input$file1)) { return() }
  print(ggplot(Data()$df.melt, aes(x=DaysPrice, fill=VendorName)) + geom_density(alpha=.3))
  
  })
  
  output$VendorNameDensity <- renderPlot({
    if (is.null(input$file1)) { return() }
    df.VendorName<- subset(Data()$df.melt,Day!="Av")
    str(df.VendorName)
    print(ggplot(df.VendorName, aes(x=DaysPrice, fill=VendorName)) + geom_density(alpha=.3))
        
  })
  
  output$VendorNameDiff <- renderPrint({
    if (is.null(input$file1)) { return() }
    df.VendorName<- subset(Data()$df.melt,Day!="Av")
    aov.by.VendorName <- aov(DaysPrice ~ VendorName, data=df.VendorName)
    summary(aov.by.VendorName) 
  })
  
  output$caption1 <- renderText( {
    if (is.null(input$file1)) { return() }
    
    " ANOVA Results"
  })
  
  output$caption2 <- renderText( {
    if (is.null(input$file1)) { return() }
    paste0("Average Product Price / Cost DAY-1:",Data()$t1Av,
           
           " DAY-2:",Data()$t2Av,
           
           "DAY-3:",Data()$t3Av)
  })
  #  
  output$caption3 <- renderText( {
    if (is.null(input$file1)) { return() }
    paste0("Analysis of Variance by Vendor - Vendor 1 AVG Cost:",Data()$Flipkart, "Vendor 2 AVG Cost:",Data()$Amazon)
  })
 #
  output$notes2 <- renderUI( {
    if (is.null(input$file1)) { return() }
    HTML("The above graph shows the variation in Cost of product by Vendor.")
    
  })
  
  output$notes3 <- renderUI( {
    if (is.null(input$file1)) { return() }
    HTML("The Analysis of Variance indicates whether there is a statistically significant
         difference between Vendors.")
    
  })
  
  })