There and Back Again competition entry https://shiny.petras.space/commute/
rstats
rshiny
census
competition
leaflet
javascript
stats-nz

app.R 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. library(shiny)
  2. library(shinyjs)
  3. library(leaflet)
  4. library(rgdal)
  5. library(dplyr)
  6. # work_travel <- read_csv("../travel-work.csv")
  7. load(file="datasets.RData")
  8. shpf <- readOGR(dsn="../shapefiles/sa20025WGSfilcth")
  9. sa.in.dest <- shpf@data$SA22018_V1 %in% work_to$work_code
  10. sa.in.home <- shpf@data$SA22018_V1 %in% work_from$res_code
  11. transport.t <- c("Work at home", "Private car", "Company car",
  12. "Carpool", "Bus", "Train", "Bicycle", "Walk",
  13. "Ferry", "Other", "None")
  14. codelist <- shpf@data %>%
  15. mutate(sa2_code = as.numeric(as.character(SA22018_V1))) %>%
  16. select(sa2_code)
  17. startcols <- codelist %>% left_join(work_from, by = c("sa2_code" = "res_code"))
  18. startcols <- tencols[startcols$MAX]
  19. startcols <- ifelse(is.na(startcols), "#808080", startcols)
  20. # Define UI
  21. ui <- fluidPage(
  22. useShinyjs(),
  23. tags$style(type = "text/css",
  24. "html, body {
  25. width:100%;
  26. height:100%
  27. }
  28. #map {
  29. height: 100% !important;
  30. position: absolute !important;
  31. top: 0;
  32. left: 0;
  33. }
  34. #loading {
  35. cursor: progress !important;
  36. }
  37. #loading p {
  38. border-radius: 25px;
  39. background: #FFFFFF;
  40. padding: 10px;
  41. border: 2px solid #000000;
  42. font-size: 1.5em;
  43. font-weight: bold;
  44. }"),
  45. leafletOutput("map"),
  46. absolutePanel(bottom = 30, left = 30, id="loading",
  47. p("Loading..."))
  48. )
  49. # Define server logic
  50. server <- function(input, output) {
  51. sel.SA2.code <- 0
  52. p.layers <- c("polya", "polyb")
  53. output$map <- renderLeaflet({
  54. leaf <- leaflet(shpf, options = leafletOptions(minZoom = 3, maxZoom = 13)) %>%
  55. addPolygons(group = p.layers[1] ,color="#000", opacity = 1, weight=1,
  56. fillColor = startcols,
  57. label = shpf@data$SA22018__1,
  58. fillOpacity = 1) %>%
  59. setView(174, -41, 5) %>%
  60. addLegend(position = "topleft",
  61. colors = c(tencols, "#808080"),
  62. labels = transport.t, opacity = 1)
  63. shinyjs::hideElement(selector="#loading p", asis = TRUE,
  64. anim=TRUE, animType = "slide", time=7)
  65. leaf
  66. })
  67. observeEvent(input$map_shape_click, {
  68. shinyjs::showElement(selector="#loading p", asis = TRUE,
  69. anim=TRUE, animType = "slide")
  70. p <- input$map_shape_click
  71. print(p)
  72. pdat <- data.frame(Longitude = p$lng,
  73. Latitude =p$lat)
  74. # Assignment modified according
  75. coordinates(pdat) <- ~ Longitude + Latitude
  76. # Set the projection of the SpatialPointsDataFrame using the projection of the shapefile
  77. proj4string(pdat) <- proj4string(shpf)
  78. ppoly <- over(pdat, shpf)
  79. codetmp <- as.numeric(as.character(ppoly[1,"SA22018_V1"]))
  80. print(sel.SA2.code)
  81. sel.SA2.code <<- ifelse(sel.SA2.code == codetmp, 0, codetmp)
  82. print(ppoly)
  83. #print(work_simp[work_simp$res_code ==
  84. # sel.SA2.code,])
  85. print(sel.SA2.code)
  86. print(codetmp)
  87. fcols <- startcols
  88. if (sel.SA2.code != 0) {
  89. print(head(work_simp))
  90. codvs <- work_simp %>% filter(work_code == sel.SA2.code)
  91. print(head(codvs))
  92. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "res_code"))
  93. print(head(codvs))
  94. print(table(codvs$MAX))
  95. codvs <- tencols[codvs$MAX]
  96. print(table(codvs))
  97. fcols <- ifelse(is.na(codvs), "#808080", codvs)
  98. print(table(fcols))
  99. }
  100. print(table(fcols))
  101. leafletProxy("map", data = shpf) %>%
  102. addPolygons(group = p.layers[2] ,color="#000", opacity = 1, weight=1,
  103. fillColor = fcols,
  104. label = shpf@data$SA22018__1,
  105. fillOpacity = 1) %>%
  106. clearGroup(p.layers[1])
  107. p.layers <<- rev(p.layers)
  108. shinyjs::hideElement(selector="#loading p", asis=TRUE,
  109. anim=TRUE, animType = "slide", time = 7)
  110. })
  111. }
  112. # Run the application
  113. shinyApp(ui = ui, server = server)