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.

209 lines
6.7KB

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