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.

119 lines
4.2KB

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