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.

228 lines
7.8KB

  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. # addPolygons(weight = 1, color = "#222222", opacity = .1, fillOpacity = .1, fillColor = "#EEEEEE") %>%
  69. setView(lat = 48.35, lng = 6, zoom = 8)
  70. })
  71. # in bounds
  72. in_bounds <- reactive({
  73. req(input$map_bounds)
  74. bounds <- input$map_bounds
  75. latrng <- range(bounds$north, bounds$south)
  76. lngrng <- range(bounds$east, bounds$west)
  77. communes[lats >= latrng[1] & lats <= latrng[2] & lngs >= lngrng[1] & lngs <= lngrng[2],]$Codepos
  78. })
  79. # GHM
  80. data_ghm <- reactive({
  81. req(input$slider_age, input$check_sexe, input$slider_date, in_bounds())
  82. GHM %>%
  83. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  84. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  85. Codepos %in% in_bounds(),
  86. Sexe %in% input$check_sexe)
  87. })
  88. output$plot_ghm <- renderPlot({
  89. data_ghm() %>%
  90. count(GHM, Sexe) %>%
  91. semi_join(GHM %>% count(GHM) %>% top_n(10) %>% arrange(n), by = "GHM") %>%
  92. ungroup() %>%
  93. mutate(GHM = GHM %>% factor(levels = GHM %>% unique)) %>%
  94. ggplot() +
  95. aes(x = GHM, y = n, fill = Sexe) +
  96. geom_bar(stat = "identity") +
  97. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  98. coord_flip() +
  99. theme(axis.title = element_blank(),
  100. legend.position = "none")
  101. })
  102. # Sexe
  103. data_sexe <- reactive({
  104. req(input$slider_age, input$slider_date, in_bounds())
  105. if (input$choice_ghm %>% is.null)
  106. {
  107. GHM %>%
  108. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  109. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  110. Codepos %in% in_bounds())
  111. } else
  112. {
  113. GHM %>%
  114. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  115. GHM %in% input$choice_ghm,
  116. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  117. Codepos %in% in_bounds())
  118. }
  119. })
  120. output$plot_sexe <- renderPlot({
  121. data_sexe() %>%
  122. ggplot() +
  123. aes(x = 1, fill = Sexe) +
  124. geom_bar() +
  125. coord_flip() +
  126. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  127. theme(axis.title = element_blank(),
  128. legend.position = "none",
  129. axis.text.y = element_blank(),
  130. axis.ticks.y = element_blank())
  131. })
  132. # Age
  133. data_age <- reactive({
  134. req(input$slider_date, input$check_sexe, in_bounds())
  135. if (input$choice_ghm %>% is.null)
  136. {
  137. GHM %>%
  138. filter(Sexe %in% input$check_sexe,
  139. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  140. Codepos %in% in_bounds())
  141. } else
  142. {
  143. GHM %>%
  144. filter(Sexe %in% input$check_sexe,
  145. GHM %in% input$choice_ghm,
  146. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  147. Codepos %in% in_bounds())
  148. }
  149. })
  150. output$plot_age <- renderPlot({
  151. data_age() %>%
  152. ggplot() +
  153. aes(x = Age, fill = Sexe) +
  154. geom_histogram(binwidth = 5) +
  155. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  156. scale_x_continuous(limits = c(min_age, max_age)) +
  157. theme(axis.title = element_blank(),
  158. legend.position = "none")
  159. })
  160. # filtered GHM
  161. filteredData <- reactive({
  162. req(input$check_sexe, input$slider_age, input$slider_date) #, in_bounds())
  163. if(input$choice_ghm %>% is.null)
  164. {
  165. GHM %>%
  166. filter(Sexe %in% input$check_sexe,
  167. Age <= input$slider_age[2] & Age >= input$slider_age[1],
  168. # Codepos %in% in_bounds(),
  169. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1]) %>%
  170. count(Codepos)
  171. } else
  172. {
  173. GHM %>%
  174. filter(Sexe %in% input$check_sexe,
  175. Age <= input$slider_age[2] & Age >= input$slider_age[1],
  176. GHM %in% input$choice_ghm,
  177. # Codepos %in% in_bounds(),
  178. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1]) %>%
  179. count(Codepos)
  180. }
  181. })
  182. # Map
  183. observe({
  184. req(filteredData())
  185. comm <- communes
  186. comm@data %>%
  187. left_join(filteredData()) -> comm@data
  188. comm <- comm[!is.na(comm$n),]
  189. pal <- colorNumeric(palette = topo.colors(100), domain = comm$n, na.color = NA, reverse = T)
  190. leafletProxy("map", data = comm) %>%
  191. clearShapes() %>%
  192. clearControls() %>%
  193. addPolygons(weight = 1, color = "#222222", opacity = .1, fillOpacity = .3, fillColor = ~pal(n)) %>%
  194. addLegend(position = "topleft", pal = pal, values = ~n)
  195. })
  196. }
  197. shinyApp(ui = ui, server = server)