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.

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