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.

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