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.

131 lines
4.4KB

  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. # sliderInput("slider_age", "Age", min(GHM$Age), max(GHM$Age), value = c(min(GHM$Age), max(GHM$Age))),
  42. # Sexe
  43. # plotlyOutput("plot_sexe"),
  44. checkboxGroupInput("check_sexe", "Sexe", c("Homme", "Femme"), c("Homme", "Femme"))
  45. ),
  46. absolutePanel(bottom = 10, left = "5%", width = "90%",
  47. sliderInput("slider_date", "Dates", width = "100%", min = as.Date("2005-01-01"), max = as.Date("2017-01-01"), value = c(as.Date("2016-01-01"), as.Date("2017-01-01")), animate = animationOptions(interval = 0))
  48. )
  49. )
  50. server <- function(input, output, session)
  51. {
  52. # Create map
  53. output$map <- renderLeaflet({
  54. leaflet(communes, options = leafletOptions(minZoom = 7, maxZoom = 15)) %>%
  55. addProviderTiles(providers$CartoDB.Positron) %>%
  56. addPolygons(weight = 1, color = "#222222", opacity = .1, fillOpacity = .1, fillColor = "#EEEEEE") %>%
  57. setView(lat = 48.35, lng = 6, zoom = 9)
  58. })
  59. # Filter in bounds
  60. in_bounds <- reactive({
  61. if (is.null(input$map_bounds))
  62. return(communes[F,])
  63. bounds <- input$map_bounds
  64. latrng <- range(bounds$north, bounds$south)
  65. lngrng <- range(bounds$east, bounds$west)
  66. communes[lats >= latrng[1] & lats <= latrng[2] & lngs >= lngrng[1] & lngs <= lngrng[2],]
  67. })
  68. # Filter GHM
  69. # filteredData <- reactive({
  70. # GHM %>%
  71. # filter(Sexe %in% input$check_sexe,
  72. # Age <= input$slider_age[2] & Age >= input$slider_age[1],
  73. # GHM %in% input$choice_ghm,
  74. # date_entree <= input$slider_date[2] & date_sortie >= input$slider_date[1],
  75. # CP %in% in_bounds$insee)
  76. # })
  77. # Plots
  78. # observe({
  79. # output$plot_sexe <- renderPlotly({
  80. # filteredData() %>%
  81. # (
  82. # ggplot() +
  83. # aes(x = Sexe) +
  84. # geom_bar(position = "stacked")
  85. # ) %>% ggplotly
  86. # })
  87. # })
  88. # observe({
  89. # output$plot_age <- renderPlotly({
  90. # filteredData() %>%
  91. # (
  92. # ggplot() +
  93. # aes(x = Age, y = ..density..) +
  94. # geom_histogram() +
  95. # geom_density()
  96. # ) %>% ggplotly
  97. # })
  98. # })
  99. # observe({
  100. # leafletProxy("map", data = in_bounds()) %>%
  101. # clearShapes() %>%
  102. # })
  103. # Use a separate observer to recreate the legend as needed.
  104. # observe({
  105. # Remove any existing legend, and only if the legend is
  106. # enabled, create a new one.
  107. # leafletProxy("map", data = quakes) %>%
  108. # clearControls()
  109. # if (input$legend) {
  110. # pal <- colorpal()
  111. # proxy %>% addLegend(position = "bottomright",
  112. # pal = pal, values = ~mag
  113. # )
  114. # }
  115. # })
  116. }
  117. shinyApp(ui = ui, server = server)