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

app.R 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. library(shiny)
  2. library(shinyjs)
  3. library(leaflet)
  4. library(rgdal)
  5. library(dplyr)
  6. library(leaflet.extras)
  7. source("leafletfunctions.R")
  8. # work_travel <- read_csv("../travel-work.csv")
  9. load(file="datasets.RData")
  10. shpf <- readOGR(dsn="../shapefiles/sa20025WGSfilcth")
  11. sa.in.dest <- shpf@data$SA22018_V1 %in% work_to$work_code
  12. sa.in.home <- shpf@data$SA22018_V1 %in% work_from$res_code
  13. transport.t <- c("Work at home", "Private car", "Company car",
  14. "Carpool", "Bus", "Train", "Bicycle", "Walk",
  15. "Ferry", "Other", "None")
  16. cols.labs <- c(transport.t[1:10], "Total")
  17. codelist <- shpf@data %>%
  18. mutate(sa2_code = as.numeric(as.character(SA22018_V1))) %>%
  19. select(sa2_code)
  20. startcols <- codelist %>% left_join(work_from, by = c("sa2_code" = "res_code"))
  21. startcols <- tencols[startcols$MAX]
  22. startcols <- ifelse(is.na(startcols), "#808080", startcols)
  23. # Define UI
  24. ui <- fluidPage(
  25. useShinyjs(),
  26. leafletjs,
  27. tags$style(type = "text/css",
  28. "html, body {
  29. width:100%;
  30. height:100%
  31. }
  32. #map {
  33. height: 100% !important;
  34. position: absolute !important;
  35. top: 0;
  36. left: 0;
  37. }
  38. #loading {
  39. cursor: progress !important;
  40. }
  41. #loading p {
  42. border-radius: 5px;
  43. background-color: rgba(255, 255, 255, 0.8);
  44. padding: 6px 8px;
  45. box-shadow: 0 0 15px rgba(0,0,0,0.2);
  46. font-size: 1.5em;
  47. font-weight: bold;
  48. }
  49. #mapcontrol {
  50. background-color: rgba(255, 255, 255, 0.8);
  51. border-radius: 5px;
  52. box-shadow: 0 0 15px rgba(0,0,0,0.2);
  53. padding: 6px 8px;
  54. font: 14px/16px Arial, Helvetica, sans-serif;
  55. }
  56. #lochtml ul {
  57. padding-left: 15px;
  58. }"),
  59. leafletOutput("map"),
  60. absolutePanel(top = 10, right = 10, id="mapcontrol",
  61. radioButtons("radioinout", label=NULL,
  62. choices = c(
  63. "Work in" = "work",
  64. "Live in" = "res"
  65. ),
  66. inline = TRUE),
  67. div(id="locinfo",
  68. htmlOutput("lochtml"))),
  69. absolutePanel(bottom = 30, left = 30, id="loading",
  70. p("Loading..."))
  71. )
  72. # Define server logic
  73. server <- function(input, output) {
  74. sel.SA2.code <- reactiveVal(0)
  75. p.layers <- c("polya", "polyb")
  76. output$map <- renderLeaflet({
  77. leaf <- leaflet(shpf, options = leafletOptions(minZoom = 3, maxZoom = 13)) %>%
  78. addPolygons(color="#000", opacity = 1, weight=1,
  79. fillColor = startcols,
  80. layerId = ~SA22018_V1,
  81. label = shpf@data$SA22018__1,
  82. fillOpacity = 1) %>%
  83. setView(174, -41, 5) %>%
  84. addResetMapButton() %>%
  85. addLegend(position = "topleft",
  86. colors = c(tencols, "#808080"),
  87. labels = transport.t, opacity = 1)
  88. shinyjs::hideElement(selector="#loading p", asis = TRUE,
  89. anim=TRUE, animType = "slide", time=7)
  90. leaf
  91. })
  92. updateMap <- function() {
  93. selcode <- sel.SA2.code()
  94. selcode <- ifelse(is.na(selcode), 0, selcode)
  95. shinyjs::showElement(selector="#loading p", asis = TRUE,
  96. anim=TRUE, animType = "slide")
  97. fcols <- startcols
  98. if (selcode != 0) {
  99. codvs <- work_simp %>% filter(work_code == selcode)
  100. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "res_code"))
  101. codvs <- tencols[codvs$MAX]
  102. fcols <- ifelse(is.na(codvs), "#808080", codvs)
  103. }
  104. leafletProxy("map", data = shpf) %>%
  105. setShapeStyle(layerId = ~SA22018_V1, fillColor = fcols) %>%
  106. clearControls() %>%
  107. addLegend(position = "topleft",
  108. colors = c(tencols, "#808080"),
  109. labels = transport.t, opacity = 1)
  110. p.layers <<- rev(p.layers)
  111. shinyjs::hideElement(selector="#loading p", asis=TRUE,
  112. anim=TRUE, animType = "slide")
  113. }
  114. observeEvent(input$map_shape_click, {
  115. p <- input$map_shape_click
  116. pdat <- data.frame(Longitude = p$lng,
  117. Latitude =p$lat)
  118. coordinates(pdat) <- ~ Longitude + Latitude
  119. proj4string(pdat) <- proj4string(shpf)
  120. ppoly <- over(pdat, shpf)
  121. codetmp <- as.numeric(as.character(ppoly[1,"SA22018_V1"]))
  122. codetmp <- ifelse(is.na(codetmp), 0, codetmp)
  123. sel.SA2.code(ifelse(sel.SA2.code() == codetmp, 0, codetmp))
  124. updateMap()
  125. })
  126. output$lochtml <- renderUI({
  127. seled <- sel.SA2.code()
  128. seled <- ifelse(is.na(seled), 0, seled)
  129. if (seled == 0) {
  130. HTML("")
  131. } else {
  132. str <- sprintf("<hr style='border-top: 1px solid #000;'/><h4>%s</h4>",
  133. shpf@data$SA22018__1[shpf@data$SA22018_V1 == seled])
  134. if (input$radioinout == "work") {
  135. vals <- as.numeric(work_to[work_to$work_code == seled, 5:15])
  136. vals <- ifelse(is.na(vals), 0, vals)
  137. vals <- ifelse(vals < 0, "~0", as.character(vals))
  138. listi <- paste0(sprintf("<li>%s: %s</li>", cols.labs,
  139. vals),
  140. collapse="")
  141. str <- paste0(str, "<ul>", listi, "</ul>")
  142. } else {
  143. vals <- as.numeric(work_from[work_from$res_code == seled, 5:15])
  144. vals <- ifelse(is.na(vals), 0, vals)
  145. vals <- ifelse(vals < 0, "~0", as.character(vals))
  146. listi <- paste0(sprintf("<li>%s: %s</li>", cols.labs,
  147. vals),
  148. collapse="")
  149. str <- paste0(str, "<ul>", listi, "</ul>")
  150. }
  151. HTML(str)
  152. }
  153. })
  154. }
  155. # Run the application
  156. shinyApp(ui = ui, server = server)