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.

175 lines
6.0KB

  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. # Lecture fichier de GHM ----
  10. read_csv("ghm.csv", col_types = cols(IPP = col_character())) %>%
  11. `names<-`(c("IPP", "IEP", "Age", "Sexe", "CP", "GHM", "date_entree", "date_sortie")) %>%
  12. mutate(date_entree = date_entree %>% as.Date(format = "%Y/%m/%d"),
  13. date_sortie = date_sortie %>% as.Date(format = "%Y/%m/%d"),
  14. DS = as.numeric(date_sortie - date_entree),
  15. Sexe = Sexe %>% factor(labels = c("Homme", "Femme"))) -> GHM
  16. # Lecture labels racines de GHM et garder celles existantes ----
  17. read_csv("racinesGHM.csv") %>%
  18. mutate(codlib = str_c(racine, " - ", libelle)) -> racines
  19. racine <- racines$racine
  20. names(racine) <- racines$codlib
  21. racine <- racine[racine %in% GHM$GHM]
  22. rm(racines)
  23. # Lecture codes postaux <-> codes insee et garder ceux existants ----
  24. read_csv2("insee.csv") %>%
  25. select(Codepos, insee = INSEE) %>%
  26. filter(Codepos %in% GHM$CP) %>%
  27. mutate(insee = insee %>% as.character)-> insee
  28. # Lecture carte et join avec codes postaux ----
  29. readOGR("communes", "communes-20150101-100m") -> communes
  30. communes@data %>%
  31. left_join(insee) -> communes@data
  32. communes <- communes[!is.na(communes$Codepos),]
  33. rm(insee)
  34. # long/lat de chaque commune ----
  35. lngs <- communes@polygons %>% map(~slot(.x, "labpt")) %>% map_dbl(1)
  36. lats <- communes@polygons %>% map(~slot(.x, "labpt")) %>% map_dbl(2)
  37. min_age <- GHM$Age %>% min(na.rm = T)
  38. max_age <- GHM$Age %>% max(na.rm = T)
  39. min_date <- GHM$date_entree %>% min(na.rm =T)
  40. max_date <- GHM$date_sortie %>% max(na.rm =T)
  41. # UI ----
  42. ui <- bootstrapPage(
  43. tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  44. leafletOutput("map", width = "100%", height = "100%"),
  45. absolutePanel(top = 10, right = 10, width = "20%",
  46. # GHM
  47. plotOutput("plot_ghm", height = 200),
  48. selectizeInput("choice_ghm", "GHM", racine, multiple = T),
  49. # Age
  50. plotOutput("plot_age", height = 200),
  51. sliderInput("slider_age", "Age", min_age, max_age, value = c(min_age, max_age)),
  52. # Sexe
  53. plotOutput("plot_sexe", height = 200),
  54. checkboxGroupInput("check_sexe", "Sexe", c("Homme", "Femme"), c("Homme", "Femme"))
  55. ),
  56. absolutePanel(bottom = 10, left = "2.5%", width = "75%",
  57. sliderInput("slider_date", "Dates", width = "100%", min = min_date, max = max_date, value = c(max_date - years(1), max_date), animate = animationOptions(interval = 0))
  58. )
  59. )
  60. # Server ----
  61. server <- function(input, output, session)
  62. {
  63. # Create map
  64. output$map <- renderLeaflet({
  65. leaflet(communes, options = leafletOptions(minZoom = 6, maxZoom = 14)) %>%
  66. addProviderTiles(providers$CartoDB.Positron) %>%
  67. #addPolygons(weight = 1, color = "#222222", opacity = .1, fillOpacity = .1, fillColor = "#EEEEEE") %>%
  68. setView(lat = 48.35, lng = 6, zoom = 8)
  69. })
  70. # in bounds
  71. in_bounds <- reactive({
  72. if (is.null(input$map_bounds))
  73. return(communes[F,])
  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],]
  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()$Codepos)
  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()$Codepos)
  96. }
  97. })
  98. # GHM
  99. data_ghm <- reactive({
  100. GHM %>%
  101. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  102. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  103. CP %in% in_bounds()$Codepos,
  104. Sexe %in% input$check_sexe)
  105. })
  106. output$plot_ghm <- renderPlot({
  107. data_ghm() %>%
  108. count(GHM) %>%
  109. top_n(10) %>%
  110. arrange(n) %>%
  111. mutate(GHM = GHM %>% factor(levels = GHM)) %>%
  112. ggplot() +
  113. aes(x = GHM, y = n) +
  114. geom_bar(stat = "identity") +
  115. coord_flip() +
  116. theme(axis.title = element_blank())
  117. })
  118. # Sexe
  119. data_sexe <- reactive({
  120. if (input$choice_ghm %>% is.null)
  121. {
  122. GHM %>%
  123. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  124. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  125. CP %in% in_bounds()$Codepos)
  126. } else
  127. {
  128. GHM %>%
  129. filter(Age <= input$slider_age[2] & Age >= input$slider_age[1],
  130. GHM %in% input$choice_ghm,
  131. date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  132. CP %in% in_bounds()$Codepos)
  133. }
  134. })
  135. output$plot_sexe <- renderPlot({
  136. data_sexe() %>%
  137. ggplot() +
  138. aes(x = 1, fill = Sexe) +
  139. geom_bar() +
  140. coord_flip() +
  141. scale_fill_manual(values = c("Homme" = "lightblue", "Femme" = "pink")) +
  142. theme(axis.title = element_blank(),
  143. legend.position = "none",
  144. axis.text.y = element_blank(),
  145. axis.ticks.y = element_blank())
  146. })
  147. # pal <- colorpal()
  148. # proxy %>% addLegend(position = "bottomright",
  149. # pal = pal, values = ~mag
  150. # )
  151. # }
  152. # })
  153. }
  154. shinyApp(ui = ui, server = server)