|
- library(tidyverse)
- library(shiny)
- library(leaflet)
- library(RColorBrewer)
- library(sp)
- library(rgdal)
- library(plotly)
- library(stringr)
-
- # Lecture fichier de GHM ----
- read_csv("ghm.csv", col_types = cols(IPP = col_character())) %>%
- `names<-`(c("IPP", "IEP", "Age", "Sexe", "CP", "GHM", "date_entree", "date_sortie")) %>%
- mutate(date_entree = date_entree %>% as.Date(format = "%Y/%m/%d"),
- date_sortie = date_sortie %>% as.Date(format = "%Y/%m/%d"),
- DS = as.numeric(date_sortie - date_entree),
- Sexe = Sexe %>% factor(labels = c("Homme", "Femme"))) -> GHM
-
- # Lecture labels racines de GHM et garder celles existantes ----
- read_csv("racinesGHM.csv") %>%
- mutate(codlib = str_c(racine, " - ", libelle)) -> racines
- racine <- racines$racine
- names(racine) <- racines$codlib
-
- racine <- racine[racine %in% GHM$GHM]
- rm(racines)
-
- # Lecture codes postaux <-> codes insee et garder ceux existants ----
- read_csv2("insee.csv") %>%
- select(Codepos, insee = INSEE) %>%
- filter(Codepos %in% GHM$CP) %>%
- mutate(insee = insee %>% as.character)-> insee
-
- # Lecture carte et join avec codes postaux ----
- readOGR("communes", "communes-20150101-100m") -> communes
-
- communes@data %>%
- left_join(insee) -> communes@data
-
- communes <- communes[!is.na(communes$Codepos),]
-
- rm(insee)
-
- # long/lat de chaque commune ----
-
- lngs <- communes@polygons %>% map(~slot(.x, "labpt")) %>% map_dbl(1)
- lats <- communes@polygons %>% map(~slot(.x, "labpt")) %>% map_dbl(2)
- min_age <- GHM$Age %>% min(na.rm = T)
- max_age <- GHM$Age %>% max(na.rm = T)
- min_date <- GHM$date_entree %>% min(na.rm =T)
- max_date <- GHM$date_sortie %>% max(na.rm =T)
-
- # sliderInput("slider_age", "Age", min(GHM$Age), max(GHM$Age), value = c(min(GHM$Age), max(GHM$Age))),
- # Sexe
- # plotlyOutput("plot_sexe"),
- checkboxGroupInput("check_sexe", "Sexe", c("Homme", "Femme"), c("Homme", "Femme"))
- ),
- absolutePanel(bottom = 10, left = "5%", width = "90%",
- sliderInput("slider_date", "Dates", width = "100%", min = as.Date("2005-01-01"), max = as.Date("2017-01-01"), value = c(as.Date("2016-01-01"), as.Date("2017-01-01")), animate = animationOptions(interval = 0))
- )
- )
-
- server <- function(input, output, session)
- {
- # Create map
- output$map <- renderLeaflet({
- leaflet(communes, options = leafletOptions(minZoom = 7, maxZoom = 15)) %>%
- addProviderTiles(providers$CartoDB.Positron) %>%
- addPolygons(weight = 1, color = "#222222", opacity = .1, fillOpacity = .1, fillColor = "#EEEEEE") %>%
- setView(lat = 48.35, lng = 6, zoom = 9)
- })
- # Filter in bounds
- in_bounds <- reactive({
- if (is.null(input$map_bounds))
- return(communes[F,])
- bounds <- input$map_bounds
- latrng <- range(bounds$north, bounds$south)
- lngrng <- range(bounds$east, bounds$west)
- communes[lats >= latrng[1] & lats <= latrng[2] & lngs >= lngrng[1] & lngs <= lngrng[2],]
- })
- # Filter GHM
- # filteredData <- reactive({
- # GHM %>%
- # filter(Sexe %in% input$check_sexe,
- # Age <= input$slider_age[2] & Age >= input$slider_age[1],
- # GHM %in% input$choice_ghm,
- # date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
- # CP %in% in_bounds$insee)
- # })
- # Plots
- # observe({
- # output$plot_sexe <- renderPlotly({
- # filteredData() %>%
- # (
- # ggplot() +
- # aes(x = Sexe) +
- # geom_bar(position = "stacked")
- # ) %>% ggplotly
- # })
- # })
- # observe({
- # output$plot_age <- renderPlotly({
- # filteredData() %>%
- # (
- # ggplot() +
- # aes(x = Age, y = ..density..) +
- # geom_histogram() +
- # geom_density()
- # ) %>% ggplotly
- # })
- # })
- # observe({
- # leafletProxy("map", data = in_bounds()) %>%
- # clearShapes() %>%
- # })
- # Use a separate observer to recreate the legend as needed.
- # observe({
- # Remove any existing legend, and only if the legend is
- # enabled, create a new one.
- # leafletProxy("map", data = quakes) %>%
- # clearControls()
- # if (input$legend) {
- # pal <- colorpal()
- # proxy %>% addLegend(position = "bottomright",
- # pal = pal, values = ~mag
- # )
- # }
- # })
- }
-
- shinyApp(ui = ui, server = server)
|