# ###### UI ##########
# 
# library(shiny)
# library(readxl)
# library(flexdashboard)
# 
# library(leaflet)
# library(spData)
# library(dplyr)
# 
# nyc_mortality <- read.csv("cleaned_nyc_data.csv")
# 
# a_l <- read_xlsx("Alcaldes_Light.xlsx")
# 
# comunas_select <- a_l$Comuna
# comunas_select <- unique(comunas_select)
# 
# listas_select <- a_l$Lista
# listas_select  <- unique(listas_select)
# 
# partidos_select <- a_l$Partido
# partidos_select  <- unique(partidos_select)
# 
# # Define UI for application that draws a histogram
# shinyUI(fluidPage(
# 
#     # Application title
#     titlePanel("Elecciones Chilenas Alcaldes Light"),
#     
#     br(),
#     hr(),
#     
#     titlePanel("Listas por comuna (filtro simple):"),
#  
#     
#     # Sidebar with a 3 inputs 
#     sidebarLayout(
#         sidebarPanel(
#             selectInput(inputId = "comuna",
#                         label = "Seleccione comuna (Filtro simple):",
#                         choices = c(comunas_select)),
#                         # 
#                         # choices = c("2080",
#                         #             "2007",
#                         #             "2008",
#                         #             "2009",
#                         #             "2010",
#                         #             "2011",
#                         #             "2012",
#                         #             "2013",
#                         #             "2014")),
#                         # 
#             # radioButtons(inputId = "sex",
#             #              label = "Sex:",
#             #              choices = c(
#             #                  "Female" = "F",
#             #                  "Male" = "M"
#             #              )),
#             # radioButtons(inputId = "race",
#             #              label = "Partidos:",
#             #              choices = unique(nyc_mortality$race_ethnicity))
#             
#             # selectInput(inputId = "lista",
#             #             label = "Seleccione lista:",
#             #             choices = c(listas_select)),
#         ),
# 
#         # Show plot and table
#         mainPanel(
# 
#             plotOutput("deathPlot"),
#             
#             DT::dataTableOutput("deathTable")
#         )
#     ),
#    
#     br(),
#     br(),
#     titlePanel("Porcentaje de listas en la comuna sobre el total nacional de Listas:"),
#     gaugeOutput("gauge"),
#   
#     br(),
#     titlePanel("Porcentaje de partidos en la comuna sobre el total nacional de Partidos:"),
#     gaugeOutput("gauge2"),
#     br(),
#     
#     br(),
#     titlePanel("Porcentaje de candidatos en la comuna sobre el total nacional de Candidatos:"),
#     gaugeOutput("gauge3"),
#     
#     br(),
#     
#     ##########
#     
#     # Sidebar with a 3 inputs 
#     sidebarLayout(
#         sidebarPanel(
#             selectInput(inputId = "lista",
#                         label = "Seleccione lista (filtro anidado):",
#                         choices = c(listas_select)),
#             
#             
#             selectInput(inputId = "partido",
#                         label = "Seleccione partido (filtro anidado):",
#                         choices = c(partidos_select)),
#         ),
# 
#         # Show plot and table
#         mainPanel(
#            
#             
#           #  plotOutput("deathPlot2"),
#             
#             DT::dataTableOutput("deathTable2"),
# 
#         )
#     )
# ))
# 
# 
# ##### SERVER #######
# 
# library(dplyr)
# library(ggplot2)
# library(readr)
# library(shiny)
# library(readxl)
# library(flexdashboard)
# 
# library(leaflet)
# library(spData)
# library(dplyr)
# 
# nyc_mortality <- read.csv("cleaned_nyc_data.csv")
# 
# a_l <- read_xlsx("Alcaldes_Light.xlsx")
# 
# # Define server logic required to draw a histogram
# shinyServer(function(input, output) {
# 
#     selections = reactive({
#         req(input$comuna)
#         req(input$lista)
#         
#         ddd <- nrow(unique(input$lista))
#         
#         # req(input$race)
#         # filter(a_l, Comuna == input$comuna) %>% filter(Lista %in% input$lista)
#         filter(a_l, Comuna == input$comuna) 
#    
#     })
#     
#     selections2 = reactive({
#         req(input$comuna)
#         req(input$lista)
#         # req(input$race)
#         filter(a_l, Comuna == input$comuna) %>% filter(Lista %in% input$lista) %>%
#             filter(Partido %in% input$partido)
#         #filter(a_l, Comuna == input$comuna) 
#     })
#     
#     
#     output$deathPlot = renderPlot({
#         
#         ggplot(data = selections(), aes(x =  Lista)) +
#             geom_bar(stat = 'count', color = 'steelblue', fill = 'green') +
#             labs(
#                 title = "Listas por comuna",
#                 x = "Comuna",
#                 y = "Numero de listas"
#             ) + theme(axis.text.x = element_text(angle = 15, hjust=1))
#     })
# 
# 
#     ######
#     
#     output$gauge = renderGauge({
#         
#         cualquiercosa3 <- selections()[,c("Lista")]
#         cualquiercosa3 <- nrow(unique(cualquiercosa3))
#         porcentaje_de_lista_sobre_el_total <- cualquiercosa3*100/14
#         
#         gauge(porcentaje_de_lista_sobre_el_total, 
#               min = 0, 
#               max = 100, 
#               sectors = gaugeSectors(  danger= c(66, 100), 
#                                      warning = c(33, 66),
#                                     success = c(0, 33)))
#     })
#     
#     #######
#     
#     
#     output$gauge2 = renderGauge({
#         
#         cualquiercosa4 <- selections()[,c("Partido")]
#         cualquiercosa4 <- nrow(unique(cualquiercosa4))
#         porcentaje_de_lista_sobre_el_total <- cualquiercosa4*100/24
#         
#         gauge(porcentaje_de_lista_sobre_el_total, 
#               min = 0, 
#               max = 100, 
#               sectors = gaugeSectors(  danger= c(66, 100), 
#                                        warning = c(33, 66),
#                                        success = c(0, 33)))
#     })
#     
# 
#     output$gauge3 = renderGauge({
#         
#         cualquiercosa5 <- selections()[,c("Candidato")]
#         cualquiercosa5 <- nrow(unique(cualquiercosa5))
#         porcentaje_de_lista_sobre_el_total <- cualquiercosa5*100/1447
#         
#         gauge(porcentaje_de_lista_sobre_el_total,
#               min = 0,
#               max = 100,
#               sectors = gaugeSectors( danger= c(66, 100),
#                                       warning = c(33, 66),
#                                       success = c(0, 33)))
#     })
#     
#     #######
# 
#     output$deathTable = 
#         DT::renderDataTable({
#             DT::datatable(selections()[,c("Comuna", "Lista", "Candidato", "Partido", "Región", "Facebook")],
#                           colnames = c("Comuna", "Lista", "Candidato", "Partido", "Región", "Facebook"),
#                           options = list(order = list( pageLength=50, scrollX='100px')),
#                           rownames = FALSE,
#                           
#             )
#         })
# 
#     
#     output$deathTable2 = 
#         DT::renderDataTable({
#             DT::datatable(selections2()[,c("Comuna", "Lista", "Candidato", "Partido", "Región", "Facebook")],
#                           colnames = c("Comuna", "Lista", "Candidato","Partido", "Región", "Facebook"),
#                           options = list(order = list(2, 'des')),
#                           rownames = FALSE,
#             )
#         })
# 
# })