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.

226 lines
7.6KB

  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-100m") -> 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%", min = min_date, max = max_date, value = c(max_date - years(1), max_date), animate = animationOptions(interval = 0))
  59. )
  60. )
  61. # Server ----
  62. server <- function(input, output, session)
  63. {
  64. # Create map
  65. output$map <- renderLeaflet({
  66. leaflet(communes, 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. # Filter GHM
  80. filteredData <- reactive({
  81. if (input$choice_ghm %>% is.null)
  82. {
  83. GHM %>%
  84. filter(Sexe %in% input$check_sexe,
  85. Age <= input$slider_age[2] & Age >= input$slider_age[1],
  86. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  87. CP %in% in_bounds())
  88. } else
  89. {
  90. GHM %>%
  91. filter(Sexe %in% input$check_sexe,
  92. Age <= input$slider_age[2] & Age >= input$slider_age[1],
  93. GHM %in% input$choice_ghm,
  94. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  95. CP %in% in_bounds())
  96. }
  97. })
  98. # GHM
  99. data_ghm <- reactive({
  100. req(input$slider_age, input$check_sexe, input$slider_date, in_bounds())
  101. GHM %>%
  102. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  103. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  104. Codepos %in% in_bounds(),
  105. Sexe %in% input$check_sexe)
  106. })
  107. output$plot_ghm <- renderPlot({
  108. data_ghm() %>%
  109. count(GHM, Sexe) %>%
  110. semi_join(GHM %>% count(GHM) %>% top_n(10) %>% arrange(n), by = "GHM") %>%
  111. ungroup() %>%
  112. mutate(GHM = GHM %>% factor(levels = GHM %>% unique)) %>%
  113. ggplot() +
  114. aes(x = GHM, y = n, fill = Sexe) +
  115. geom_bar(stat = "identity") +
  116. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  117. coord_flip() +
  118. theme(axis.title = element_blank(),
  119. legend.position = "none")
  120. })
  121. # Sexe
  122. data_sexe <- reactive({
  123. req(input$slider_age, input$slider_date, in_bounds())
  124. if (input$choice_ghm %>% is.null)
  125. {
  126. GHM %>%
  127. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  128. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  129. Codepos %in% in_bounds())
  130. } else
  131. {
  132. GHM %>%
  133. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  134. GHM %in% input$choice_ghm,
  135. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  136. Codepos %in% in_bounds())
  137. }
  138. })
  139. output$plot_sexe <- renderPlot({
  140. data_sexe() %>%
  141. ggplot() +
  142. aes(x = 1, fill = Sexe) +
  143. geom_bar() +
  144. coord_flip() +
  145. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  146. theme(axis.title = element_blank(),
  147. legend.position = "none",
  148. axis.text.y = element_blank(),
  149. axis.ticks.y = element_blank())
  150. })
  151. # Age
  152. data_age <- reactive({
  153. req(input$slider_date, input$check_sexe, in_bounds())
  154. if (input$choice_ghm %>% is.null)
  155. {
  156. GHM %>%
  157. filter(Sexe %in% input$check_sexe,
  158. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  159. Codepos %in% in_bounds())
  160. } else
  161. {
  162. GHM %>%
  163. filter(Sexe %in% input$check_sexe,
  164. GHM %in% input$choice_ghm,
  165. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  166. Codepos %in% in_bounds())
  167. }
  168. })
  169. output$plot_age <- renderPlot({
  170. data_age() %>%
  171. ggplot() +
  172. aes(x = Age, fill = Sexe) +
  173. geom_histogram() +
  174. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  175. scale_x_continuous(limits = c(min_age, max_age)) +
  176. theme(axis.title = element_blank(),
  177. legend.position = "none")
  178. })
  179. # Map
  180. # observe({
  181. # existing <- in_bounds()
  182. # leafletProxy("map", data = filteredData()) %>%
  183. # clearShapes() %>%
  184. # })
  185. # Use a separate observer to recreate the legend as needed.
  186. # observe({
  187. # Remove any existing legend, and only if the legend is
  188. # enabled, create a new one.
  189. # leafletProxy("map", data = quakes) %>%
  190. # clearControls()
  191. # if (input$legend) {
  192. # pal <- colorpal()
  193. # proxy %>% addLegend(position = "bottomright",
  194. # pal = pal, values = ~mag
  195. # )
  196. # }
  197. # })
  198. }
  199. shinyApp(ui = ui, server = server)