app.R

#library(shiny)
#library(httr)
#library(jsonlite)
#library(shinythemes)
#library(ggplot2)
#library(yarrr)

require(shiny)
require(shinythemes)
require(httr)
require(jsonlite)
require(plyr)
require(ggplot2)
#require(yarrr)

source('R/webService.R')

# Call the recover function when an error occurs
options(error = recover)

server = function(input, output, session) {
  municipalitiesDataFrame = fetchMunicipalities()
  kpisDataFrame = fetchKpis()
  yearsAsVector = c(1980:2018)
  
  kpiListByMunicipalitySelection = function(){
    ## Get available KPIs
    x = input$municipalityDropDownListLeft
    municipalityDropDownListLeftId = as.numeric(as.matrix(municipalitiesDataFrame["values.id"])[match(
      as.character(input$municipalityDropDownListLeft),
      as.matrix(municipalitiesDataFrame["values.title"])
    )])
    a = !is.numeric(municipalityDropDownListLeftId)
      b = length(municipalityDropDownListLeftId) != 1
    if (!is.numeric(municipalityDropDownListLeftId) | length(municipalityDropDownListLeftId) != 1) return(c(NoData = 1))

    availableKpis = fetchByMunicipality(municipality = municipalityDropDownListLeftId, year = yearsAsVector)
    setKpi = unique(availableKpis["values.kpi"])
    return(setKpi)
  }
  
  # Municipality Dropdown Left
  output$municipalityDropDownListLeft = renderUI({
    #print(municipalitiesDataFrame)
    selectInput(
      inputId = "municipalityDropDownListLeft",
      choices = as.matrix(municipalitiesDataFrame["values.title"]),
      #kpiListByMunicipalitySelection()
      #choices = kpiListByMunicipalitySelection(),
      label = "Select Municipality:"
    )
  })
  
  # Municipality Dropdown Right
  output$municipalityDropDownListRight = renderUI({
    selectInput(
      inputId = "municipalityDropDownListRight",
      choices = as.matrix(municipalitiesDataFrame["values.title"]),
      label = "Select Municipality:"
    )
  })
  
  # Kpi Dropdown Left
  output$kpiDropDownListLeft = renderUI({
    selectInput(
      inputId = "kpiDropDownListLeft",
      choices = as.matrix(kpisDataFrame["member_id"]),
      label = "Select KPI:",
      selected = kpisDataFrame[kpisDataFrame$member_id == "N15030",1]#as.matrix(kpisDataFrame["member_id"])[650]
    )
    #selectInput(
    #  inputId = "kpiDropDownListLeft",
    #  choices = as.matrix(kpisDataFrame["member_id"]),
    #  label = "Select KPI:"
    #)
  })
  
  # Kpi Dropdown Right
  output$kpiDropDownListRight = renderUI({
    selectInput(
      inputId = "kpiDropDownListRight",
      choices = as.matrix(kpisDataFrame["member_id"]),
      label = "Select KPI:",
      selected = kpisDataFrame[kpisDataFrame$member_id == "N21802",1]#as.matrix(kpisDataFrame["member_id"])[650]
    )
  
  })
  #left panel filtered options based on city name
  observeEvent(input$municipalityDropDownListLeft, {
    #the Municipality name is chosen, and the ID is found
    # municipalityDropDownListLeftHolder = as.character(input$municipalityDropDownListLeft)
    # kpiDropDownListLeftHolder = as.character(input$kpiDropDownListLeft)
    # municipalityDropDownListLeftId = as.matrix(municipalitiesDataFrame["values.id"])[match(
    #   as.character(input$municipalityDropDownListLeft),
    #   as.matrix(municipalitiesDataFrame["values.title"])
    # )]
    # 
    # ## Get available KPIs
    # municipalityDropDownListLeftId = as.numeric(as.matrix(municipalitiesDataFrame["values.id"])[match(
    #   as.character(input$municipalityDropDownListLeft),
    #   as.matrix(municipalitiesDataFrame["values.title"])
    # )])
    # 
    # availableKpis = fetchByMunicipality(municipality = municipalityDropDownListLeftId, year = yearsAsVector)
    # setKpi = unique(availableKpis["values.kpi"])
    # #print(setKpi)
    # ### Continue here
    # #prepare webcall
    # #http://api.kolada.se/v2/data/kpi/<kpi>/municipality/<municipality>/year/<year>
    # 
    # 
    #   #the call is made to fetch all the available KPI and save it filtered as a list
    #   #(try to make it only once every time the app is started)
      

      #the Municipality name is chosen, and the ID is found
      municipalityDropDownListLeftHolder = as.character(input$municipalityDropDownListLeft)
      kpiDropDownListLeftHolder = as.character(input$kpiDropDownListLeft)
      municipalityDropDownListLeftId = as.matrix(municipalitiesDataFrame["values.id"])[match(
        as.character(input$municipalityDropDownListLeft),
        as.matrix(municipalitiesDataFrame["values.title"])
      )]
      
      #the call is made to fetch all the available KPI and save it filtered as a list
      #(try to make it only once every time the app is started)
      AllKpi = as.matrix(kpisDataFrame["member_id"])
      #kpi preparation in to a nice string with comas
      kpiString <- c("")
      for (i in 1:250) {
        kpiString <- paste(kpiString, AllKpi[i, ], ",", sep = "")
      }
      kpiString <- paste(kpiString, AllKpi[length(AllKpi), ], sep = "")
      
      #years preparation in to a nice string with comas
      yearsString <- c("")
      for (i in 1:(length(yearsAsVector) - 1)) {
        yearsString <- paste(yearsString, yearsAsVector[i], ",", sep = "")
      }
      yearsString <- paste(yearsString, yearsAsVector[length(yearsAsVector)], sep = "")
      
      
      
      #the call is made with the KPI list, the chosen municipality, and the year range as a list
      #print(fetchByKpi(as.list(AllKpi), as.integer(municipalityDropDownListLeftId), as.list(yearsAsVector)))
      
      
      mensajerino = paste("http://api.kolada.se/v2/data/kpi/",kpiString,"/municipality/",municipalityDropDownListLeftId,"/year/",yearsString, sep = "")
      #print(mensajerino)
      response = GET(mensajerino)
      #print(response)
      result = fromJSON(content(response, "text"), flatten = TRUE)
      uniqueKpi = result["values.kpi"]
      print(uniqueKpi)
      # #prepare webcall
      # #http://api.kolada.se/v2/data/kpi/<kpi>/municipality/<municipality>/year/<year>
    ##respuesta = GET(paste("http://api.kolada.se/v2/data/kpi/","" , sep =""))
    
    # kpis = kpiListByMunicipalitySelection()
    # print(kpis)
    # updateSelectInput(session, "kpiDropDownListLeft",
    #              choices = kpis) # remove selection
  })
  
  observeEvent(input$PlotButtonLeft, {
    # Left Panel
    municipalityDropDownListLeftHolder = as.character(input$municipalityDropDownListLeft)
    kpiDropDownListLeftHolder = as.character(input$kpiDropDownListLeft)
    #print(municipalityDropDownListLeftHolder)
    #print(kpiDropDownListLeftHolder)
    
    municipalityDropDownListLeftId = as.matrix(municipalitiesDataFrame["values.id"])[match(
      as.character(input$municipalityDropDownListLeft),
      as.matrix(municipalitiesDataFrame["values.title"])
    )]
    
    yearMin <- as.numeric(input$yearDropDownListLeft)[1]
    yearMax <- as.numeric(input$yearDropDownListLeft)[2]
    yearCnt <- as.numeric(input$yearDropDownListLeft)[1]
    kpiResultLeftVector <- c()
    while (yearCnt <= yearMax) {
      kpiResultLeft <-
        fetchByKpi(as.list(as.character(input$kpiDropDownListLeft)),
                   municipalityDropDownListLeftId,
                   as.list(yearCnt))
      if (nrow(kpiResultLeft) == 0) {
        kpiResultLeftVector <- c(kpiResultLeftVector, 0)
      }
      else{
        content = kpiResultLeft[1, "values.values"]
        # Some times despite having some data in the row, the value we are looking for is not found and a NA is returned
        if (is.na(content[[1]][3, "value"])) {
          kpiResultLeftVector <-
            c(kpiResultLeftVector, 0)
        }
        else{
          kpiResultLeftVector <-
            c(kpiResultLeftVector, content[[1]][3, "value"])
        }
      }
      yearCnt <- yearCnt + 1
    }
    if (sum(kpiResultLeftVector) == 0) {
      # print no data message
      output$Barplot_Left = renderPlot({
        par(bg = rgb(31.5, 38.6, 44.3, maxColorValue = 255))
        plot(
          1,
          1,
          col.main = "lightgray",
          col.lab = "lightgray",
          col.axis = "lightgray",
          fg = "lightgray"
        )
        text(1, 1, "The selected KPI has no data available", col = "red")
      })
    }
    else{
      # plot the datarina
      names(kpiResultLeftVector) <- c(yearMin:yearMax)
      output$Barplot_Left = renderPlot({
        par(bg = rgb(31.5, 38.6, 44.3, maxColorValue = 255))
        par(mar = c(5, 4, 4.2, 2), xpd = TRUE)
        barplot(
          kpiResultLeftVector,
          main = paste(
            as.character(municipalityDropDownListLeftHolder),
            as.character(kpiDropDownListLeftHolder),
            "KPI historical performance."
          ),
          xlab = "Year",
          ylab = "KPI performance",
          cex.lab = 1.2,
          col = c(
            "springgreen2", "mediumaquamarine"
          ),
          col.main = "lightgray",
          col.lab = "lightgray",
          col.axis = "lightgray",
          fg = "lightgray",
          border = "lightgray",
          las = 3
        )
        abline(
          h = mean(kpiResultLeftVector[kpiResultLeftVector != 0]),
          lty = 2,
          lwd = 2,
          col = 'mediumpurple3'
        )
        abline(
          glm(kpiResultLeftVector[kpiResultLeftVector != 0] ~ c(1:length(
            kpiResultLeftVector[kpiResultLeftVector != 0]
          ))),
          lty = 2,
          lwd = 2,
          col = 'yellow3'
        )
        # Add legend
        legend(
          "bottomright",
          #inset=c(0.05, -0.2),
          legend = c("Median", "Linear trend"),
          col = c('mediumpurple3', 'yellow3'),
          bg = rgb(31.5, 38.6, 44.3, maxColorValue = 255),
          box.col = "lightgray",
          text.col = "lightgray",
          lty = 2,
          lwd = 2
        )
      })
    }
  })
  
  
  observeEvent(input$PlotButtonRight, {
    # Right Panel
    municipalityDropDownListRightHolder = as.character(input$municipalityDropDownListRight)
    kpiDropDownListRightHolder = as.character(input$kpiDropDownListRight)
    
    municipalityDropDownListRightId = as.matrix(municipalitiesDataFrame["values.id"])[match(
      as.character(input$municipalityDropDownListRight),
      as.matrix(municipalitiesDataFrame["values.title"])
    )]
    
    yearMin <- as.numeric(input$yearDropDownListRight)[1]
    yearMax <- as.numeric(input$yearDropDownListRight)[2]
    yearCnt <- as.numeric(input$yearDropDownListRight)[1]
    
    kpiResultRightVector <- c()
    while (yearCnt <= yearMax) {
      kpiResultRight <-
        fetchByKpi(as.list(as.character(input$kpiDropDownListRight)),
                   municipalityDropDownListRightId,
                   as.list(yearCnt))
      if (nrow(kpiResultRight) == 0) {
        kpiResultRightVector <- c(kpiResultRightVector, 0)
      }
      else{
        content = kpiResultRight[1, "values.values"]
        # Some times despite having some data in the row, the value we are looking for is not found and a NA is returned
        if (is.na(content[[1]][3, "value"])) {
          kpiResultRightVector <-
            c(kpiResultRightVector, 0)
        }
        else{
          kpiResultRightVector <-
            c(kpiResultRightVector, content[[1]][3, "value"])
        }
      }
      yearCnt <- yearCnt + 1
    }
    if (sum(kpiResultRightVector) == 0) {
      # print no data message
      output$Barplot_Right = renderPlot({
        par(bg = rgb(31.5, 38.6, 44.3, maxColorValue = 255))
        plot(
          1,
          1,
          col.main = "lightgray",
          col.lab = "lightgray",
          col.axis = "lightgray",
          fg = "lightgray"
        )
        text(1, 1, "The selected KPI has no data available", col = "red")
      })
    }
    else{
      # plot the datarina
      names(kpiResultRightVector) <- c(yearMin:yearMax)
      output$Barplot_Right = renderPlot({
        par(bg = rgb(31.5, 38.6, 44.3, maxColorValue = 255))
        par(mar = c(5, 4, 4.2, 2), xpd = TRUE)
        barplot(
          kpiResultRightVector,
          main = paste(
            as.character(municipalityDropDownListRightHolder),
            as.character(kpiDropDownListRightHolder),
            "KPI historical performance."
          ),
          xlab = "Year",
          ylab = "KPI performance",
          cex.lab = 1.2,
          col = c(
            "springgreen2", "mediumaquamarine"
          ),
          col.main = "lightgray",
          col.lab = "lightgray",
          col.axis = "lightgray",
          fg = "lightgray",
          border = "lightgray",
          las = 3
        )
        abline(
          h = mean(kpiResultRightVector[kpiResultRightVector != 0]),
          lty = 2,
          lwd = 2,
          col = 'mediumpurple3'
        )
        abline(
          glm(kpiResultRightVector[kpiResultRightVector != 0] ~ c(
            1:length(kpiResultRightVector[kpiResultRightVector != 0])
          )),
          lty = 2,
          lwd = 2,
          col = 'yellow3'
        )
        # Add legend
        legend(
          "bottomright",
          #inset=c(0.05, -0.2),
          legend = c("Median", "Linear trend"),
          col = c('mediumpurple3', 'yellow3'),
          bg = rgb(31.5, 38.6, 44.3, maxColorValue = 255),
          box.col = "lightgray",
          text.col = "lightgray",
          lty = 2,
          lwd = 2
        )
      })
    }
  })
}

ui = shinyUI(fluidPage(theme = shinytheme("slate"), fluidRow(column(
  12,
  h1("Kola database", align = "center"),
  h3("KPI comparison of two municipalities", align = "center"),
  hr(),
  fluidRow(
    column(
      6,
      
      wellPanel(
        h4(" Select Parameters"),
        hr(),
        column(6,
               uiOutput("municipalityDropDownListLeft")),
        column(6,
               uiOutput("kpiDropDownListLeft")),
        sliderInput(
          "yearDropDownListLeft",
          label = "Year Range",
          min = 1980,
          max = 2017,
          value = c(2007, 20017),
          width = "100%"
        ),
        fluidRow(
          align = "center",
          actionButton(inputId = "PlotButtonLeft",
                       label = "Plot")
        )
      ),
      fluidRow(plotOutput("Barplot_Left"))
      
    ),
    column(
      6,
      wellPanel(
        h4(" Select Parameters"),
        hr(),
        column(6,
               uiOutput("municipalityDropDownListRight")),
        column(6,
               uiOutput("kpiDropDownListRight")),
        sliderInput(
          "yearDropDownListRight",
          label = "Year Range",
          min = 1980,
          max = 2017,
          value = c(2007, 2017),
          width = "100%"
        ),
        fluidRow(
          align = "center",
          actionButton(inputId = "PlotButtonRight",
                       label = "Plot")
        )
      ),
      fluidRow(plotOutput("Barplot_Right"))
    )
  )
))))

shinyApp(ui = ui, server = server)
rubenjmunoz/advanced-r-programming-5 documentation built on May 13, 2019, 5:35 p.m.