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.

218 lines
7.3KB

  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", "CP", "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$CP) -> 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. if (is.null(input$map_bounds))
  74. return(communes[F,])
  75. bounds <- input$map_bounds
  76. latrng <- range(bounds$north, bounds$south)
  77. lngrng <- range(bounds$east, bounds$west)
  78. communes[lats >= latrng[1] & lats <= latrng[2] & lngs >= lngrng[1] & lngs <= lngrng[2],]$Codepos
  79. })
  80. # Filter GHM
  81. filteredData <- reactive({
  82. if (input$choice_ghm %>% is.null)
  83. {
  84. GHM %>%
  85. filter(Sexe %in% input$check_sexe,
  86. Age <= input$slider_age[2] & Age >= input$slider_age[1],
  87. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  88. CP %in% in_bounds())
  89. } else
  90. {
  91. GHM %>%
  92. filter(Sexe %in% input$check_sexe,
  93. Age <= input$slider_age[2] & Age >= input$slider_age[1],
  94. GHM %in% input$choice_ghm,
  95. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  96. CP %in% in_bounds())
  97. }
  98. })
  99. # GHM
  100. data_ghm <- reactive({
  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. CP %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. if (input$choice_ghm %>% is.null)
  124. {
  125. GHM %>%
  126. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  127. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  128. CP %in% in_bounds())
  129. } else
  130. {
  131. GHM %>%
  132. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  133. GHM %in% input$choice_ghm,
  134. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  135. CP %in% in_bounds())
  136. }
  137. })
  138. output$plot_sexe <- renderPlot({
  139. data_sexe() %>%
  140. ggplot() +
  141. aes(x = 1, fill = Sexe) +
  142. geom_bar() +
  143. coord_flip() +
  144. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  145. theme(axis.title = element_blank(),
  146. legend.position = "none",
  147. axis.text.y = element_blank(),
  148. axis.ticks.y = element_blank())
  149. })
  150. # Age
  151. data_age <- reactive({
  152. if (input$choice_ghm %>% is.null)
  153. {
  154. GHM %>%
  155. filter(Sexe %in% input$check_sexe,
  156. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  157. CP %in% in_bounds())
  158. } else
  159. {
  160. GHM %>%
  161. filter(Sexe %in% input$check_sexe,
  162. GHM %in% input$choice_ghm,
  163. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  164. CP %in% in_bounds())
  165. }
  166. })
  167. output$plot_age <- renderPlot({
  168. data_age() %>%
  169. ggplot() +
  170. aes(x = Age) +
  171. geom_histogram() +
  172. scale_x_continuous(limits = c(min_age, max_age)) +
  173. theme(axis.title = element_blank())
  174. })
  175. # Map
  176. # observe({
  177. # existing <- in_bounds()
  178. # leafletProxy("map", data = filteredData()) %>%
  179. # clearShapes() %>%
  180. # })
  181. # Use a separate observer to recreate the legend as needed.
  182. # observe({
  183. # Remove any existing legend, and only if the legend is
  184. # enabled, create a new one.
  185. # leafletProxy("map", data = quakes) %>%
  186. # clearControls()
  187. # if (input$legend) {
  188. # pal <- colorpal()
  189. # proxy %>% addLegend(position = "bottomright",
  190. # pal = pal, values = ~mag
  191. # )
  192. # }
  193. # })
  194. }
  195. shinyApp(ui = ui, server = server)