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.

215 lines
7.0KB

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