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