dev/applis_sdes/app_indic.R

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(leaflet)
library(sf)
library(data.table)
library(dplyr)
#library(rgdal)
library(leaflet.extras)


#####LECTURE DES DONNEES
CP_Layer <- st_read("data/codes_postaux_region_wgs84.shp",stringsAsFactors=FALSE)
DPT_Layer <- st_read("data/dep_wgs84.shp",stringsAsFactors=FALSE)

load("data/SUBST_TOT.Rdata")

CP_Layer <- left_join(CP_Layer,INDIC_CP ,by= c("ID" = "CP"))
DPT_Layer <- left_join(DPT_Layer,INDIC_DPT ,by= c("DEP" = "DPT"))

###Preparation des zone de liste
#Surface du ratio
Surf_Ref <- c("Surface Agricole Utile" = "SAU", "Surface Agricole Utile - Surface Toujours en herbe" = "SAU_STH")
Periode <- c("2013" = "2013","2014" = "2014","2015" = "2015","2016" = "2016","2017" = "2017")

##########INTERFACE UTILISATEUR
ui <- fluidPage(

  div(class="outer",
  
      tags$head(
        # Include our custom CSS
        includeCSS("styles.css")),    
        ######CARTE          
        leafletOutput("mymap", width="100%", height="100%"),
      
        ######PANEAU DE CONTROLE      
        absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
                draggable = TRUE, top = "auto", left = 0, right = 20, bottom = 0,
                width = 330, height = "auto",
                h3("Explorer les donnees"),
                selectInput("Surf_Ref", "Surface de reference", Surf_Ref),
                selectInput("Periode", "Annee", Periode)
              )
  )              
)



#########SERVEUR
server <- function(input, output, session) {
  
  #########CARTE AU CHARGEMENT
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles(urlTemplate='http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png',options = tileOptions(maxZoom = 12)) %>%
      fitBounds(lng1 = -5.14,lat1 = 41.36, lng2 = 9.55, lat2 = 51.09) %>% addSearchOSM()
    }) 
  
  #########CARTE AU CHANGEMENT DE VALEUR SELECTIONNEES 
  observe({
    Var <- paste0('QTE_SUBS_',input$Surf_Ref,'_',input$Periode)
    if (input$Surf_Ref=="SAU") {Title_map <- "Quantite de substances <br>achetees rapportee a <br>la surface agricole utile<br>(en kg/ha)" }
    else {Title_map <- "Quantite de substances <br>achetees rapportee a <br>la surface agricole utile,<br>hors surfaces toujours<br>en herbe (en kg/ha)" }
    
    
    bins <- c(0, 1,2.5, 5, Inf)
    
    colorDataCP <- CP_Layer[[Var]]
    palCP <- colorBin("YlOrRd", domain = colorDataCP, bins = bins)    
    
    colorDataDPT <- DPT_Layer[[Var]]
    palDPT <- colorBin("YlOrRd", domain = colorDataDPT, bins = bins)   
    
    
    
  
      leafletProxy("mymap", data = DPT_Layer) %>%
        clearShapes() %>%
        clearControls() %>% 
        addPolygons(data=DPT_Layer,
                                                                                         fillColor = ~palDPT(colorDataDPT),
                                                                                         weight = 1,
                                                                                         color = "white",
                                                                                         opacity = 1,
                                                                                         fillOpacity = 0.7,
                                                                                         popup = paste0("<strong>",DPT_Layer$DEP,"</strong><br/>", 
                                                                                                        DPT_Layer[[Var]]) , group = "DPT")%>% 
        addPolygons(data=CP_Layer,
                    fillColor = ~palCP(colorDataCP),
                    weight = 1,
                    color = "white",
                    opacity = 1,
                    fillOpacity = 0.7,
                    popup = paste0("<strong>",CP_Layer$ID,"</strong><br/>", 
                                   CP_Layer[[Var]]," kg/ha") , group = "CP")%>%
        addLayersControl(baseGroups = c("DPT", "CP" ) )%>%
        addLegend(data=DPT_Layer,pal = palDPT, values = colorDataDPT, opacity = 0.7, title = Title_map)
      
  
  }) 
  
  
}


# Run the application 
shinyApp(ui = ui, server = server)
CedricMondy/bnvd documentation built on June 25, 2019, 5:57 p.m.