You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
6.9KB

  1. library(tidyverse)
  2. library(shiny)
  3. library(leaflet)
  4. library(RColorBrewer)
  5. library(sp)
  6. library(rgdal)
  7. library(plotly)
  8. library(stringr)
  9. library(lubridate)
  10. # Lecture fichier de GHM ----
  11. read_csv("ghm.csv", col_types = cols(IPP = col_character())) %>%
  12. `names<-`(c("IPP", "IEP", "Age", "Sexe", "Codepos", "GHM", "date_entree", "date_sortie")) %>%
  13. mutate(date_entree = date_entree %>% as.Date(format = "%Y/%m/%d"),
  14. date_sortie = date_sortie %>% as.Date(format = "%Y/%m/%d"),
  15. DS = as.numeric(date_sortie - date_entree),
  16. Sexe = Sexe %>% factor(labels = c("Homme", "Femme"))) -> GHM
  17. # Lecture labels racines de GHM et garder celles existantes ----
  18. read_csv("racinesGHM.csv") %>%
  19. mutate(codlib = str_c(racine, " - ", libelle)) -> racines
  20. racine <- racines$racine
  21. names(racine) <- racines$codlib
  22. racine <- racine[racine %in% GHM$GHM]
  23. rm(racines)
  24. # # Lecture codes postaux <-> codes insee et sauvegarde carte pour simplification avec mapshaper
  25. # read_csv2("insee.csv", col_types = cols(INSEE = col_character())) %>%
  26. # select(Codepos, insee = INSEE) %>%
  27. # mutate(insee = insee %>% str_pad(5, "left", "0")) -> insee
  28. # # Lecture carte et join avec codes postaux ----
  29. # readOGR("communes", "communes-20150101-100m") -> communes
  30. # communes@data %>%
  31. # left_join(insee) -> communes@data
  32. # rm(insee)
  33. # communes <- communes[!is.na(communes$Codepos),]
  34. # writeOGR(communes, "communes_byCP", "communes_byCP", driver = "ESRI Shapefile")
  35. # Lecture carte ----
  36. readOGR("communes_byCP", "communes_byCP") -> communes
  37. # Filtre Codes existants dans la base
  38. communes <- communes[communes$Codepos %in% GHM$Codepos,]
  39. communes@data$Codepos <- communes@data$Codepos %>% as.character
  40. # long/lat de chaque commune ----
  41. lngs <- communes@polygons %>% map(~slot(.x, "labpt")) %>% map_dbl(1)
  42. lats <- communes@polygons %>% map(~slot(.x, "labpt")) %>% map_dbl(2)
  43. min_age <- GHM$Age %>% min(na.rm = T)
  44. max_age <- GHM$Age %>% max(na.rm = T)
  45. min_date <- GHM$date_entree %>% min(na.rm =T)
  46. max_date <- GHM$date_sortie %>% max(na.rm =T)
  47. # UI ----
  48. ui <- bootstrapPage(
  49. tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  50. leafletOutput("map", width = "100%", height = "100%"),
  51. absolutePanel(top = 10, right = 10, width = "20%",
  52. # GHM
  53. plotOutput("plot_ghm", height = 200),
  54. selectizeInput("choice_ghm", "GHM", racine, multiple = T),
  55. # Age
  56. plotOutput("plot_age", height = 200),
  57. sliderInput("slider_age", "Age", min_age, max_age, value = c(min_age, max_age)),
  58. # Sexe
  59. plotOutput("plot_sexe", height = 200),
  60. checkboxGroupInput("check_sexe", "Sexe", c("Homme", "Femme"), c("Homme", "Femme"))
  61. ),
  62. absolutePanel(bottom = 10, left = "2.5%", width = "75%",
  63. sliderInput("slider_date", "Dates", width = "100%", min = min_date, max = max_date, value = c(max_date - years(1), max_date))
  64. )
  65. )
  66. # Server ----
  67. server <- function(input, output, session)
  68. {
  69. # Create map
  70. output$map <- renderLeaflet({
  71. leaflet(options = leafletOptions(minZoom = 6, maxZoom = 14)) %>%
  72. addProviderTiles(providers$CartoDB.Positron) %>%
  73. setView(lat = 48.35, lng = 6, zoom = 8)
  74. })
  75. # in GHM
  76. in_ghm <- reactive({
  77. if (is.null(input$choice_ghm))
  78. GHM
  79. else
  80. GHM %>% filter(GHM %in% input$choice_ghm)
  81. })
  82. # in time
  83. in_time <- reactive({
  84. req(input$slider_date, in_ghm())
  85. filter(in_ghm(), date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1])
  86. })
  87. # in bounds
  88. in_bounds <- reactive({
  89. req(input$map_bounds, in_time())
  90. bounds <- input$map_bounds
  91. latrng <- range(bounds$north, bounds$south)
  92. lngrng <- range(bounds$east, bounds$west)
  93. codes_pos <- communes[lats >= latrng[1] & lats <= latrng[2] & lngs >= lngrng[1] & lngs <= lngrng[2],]$Codepos
  94. filter(in_time(), Codepos %in% codes_pos)
  95. })
  96. # GHM
  97. data_ghm <- reactive({
  98. req(input$slider_age, input$check_sexe, input$slider_date, in_time())
  99. in_bounds() %>%
  100. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  101. Sexe %in% input$check_sexe)
  102. })
  103. output$plot_ghm <- renderPlot({
  104. data_ghm() %>%
  105. count(GHM, Sexe) %>%
  106. arrange(desc(n)) %>%
  107. .[1:20,] %>%
  108. ungroup() %>%
  109. mutate(GHM = GHM %>% factor(levels = GHM %>% unique %>% rev)) %>%
  110. ggplot() +
  111. aes(x = GHM, y = n, fill = Sexe) +
  112. geom_bar(stat = "identity") +
  113. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  114. coord_flip() +
  115. theme(axis.title = element_blank(),
  116. legend.position = "none")
  117. })
  118. # Sexe
  119. data_sexe <- reactive({
  120. req(input$slider_age, input$slider_date, in_time())
  121. in_bounds() %>%
  122. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1])
  123. })
  124. output$plot_sexe <- renderPlot({
  125. data_sexe() %>%
  126. ggplot() +
  127. aes(x = 1, fill = Sexe) +
  128. geom_bar() +
  129. coord_flip() +
  130. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  131. theme(axis.title = element_blank(),
  132. legend.position = "none",
  133. axis.text.y = element_blank(),
  134. axis.ticks.y = element_blank())
  135. })
  136. # Age
  137. data_age <- reactive({
  138. req(input$slider_date, input$check_sexe, in_time())
  139. in_bounds() %>%
  140. filter(Sexe %in% input$check_sexe)
  141. })
  142. output$plot_age <- renderPlot({
  143. data_age() %>%
  144. ggplot() +
  145. aes(x = Age, fill = Sexe) +
  146. geom_histogram(binwidth = 5) +
  147. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  148. scale_x_continuous(limits = c(min_age, max_age)) +
  149. theme(axis.title = element_blank(),
  150. legend.position = "none")
  151. })
  152. # filtered GHM
  153. filteredData <- reactive({
  154. req(input$check_sexe, input$slider_age, input$slider_date, in_time())
  155. in_time() %>%
  156. filter(Sexe %in% input$check_sexe,
  157. Age <= input$slider_age[2] & Age >= input$slider_age[1]) %>%
  158. count(Codepos)
  159. })
  160. # Map
  161. observe({
  162. req(filteredData())
  163. comm <- communes
  164. comm@data %>%
  165. left_join(filteredData()) -> comm@data
  166. comm <- comm[!is.na(comm$n),]
  167. pal <- colorNumeric(palette = topo.colors(100), domain = comm$n, na.color = NA, reverse = T)
  168. leafletProxy("map") %>%
  169. clearShapes() %>%
  170. clearControls()
  171. leafletProxy("map", data = comm) %>%
  172. addPolygons(weight = 1, color = "#222222", opacity = .1, fillOpacity = .3, fillColor = ~pal(n), label = ~as.character(n), highlightOptions = highlightOptions(color = "black", opacity = 1)) %>%
  173. addLegend(position = "topleft", pal = pal, values = ~n)
  174. })
  175. }
  176. # App ----
  177. shinyApp(ui = ui, server = server)