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

app.R 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. lp <- 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. clearGroup("hpoly")
  111. if (selcode %in% shpf@data$SA22018_V1) {
  112. lp %>% addPolygons(group = "hpoly",
  113. weight = 4,
  114. data = shpf[which(shpf@data$SA22018_V1 == selcode),],
  115. color = "#000000",
  116. fill = FALSE, opacity = 1)
  117. }
  118. shinyjs::hideElement(selector="#loading p", asis=TRUE,
  119. anim=TRUE, animType = "slide",
  120. time = 1)
  121. }
  122. observeEvent(input$map_shape_click, {
  123. p <- input$map_shape_click
  124. pdat <- data.frame(Longitude = p$lng,
  125. Latitude =p$lat)
  126. coordinates(pdat) <- ~ Longitude + Latitude
  127. proj4string(pdat) <- proj4string(shpf)
  128. ppoly <- over(pdat, shpf)
  129. codetmp <- as.numeric(as.character(ppoly[1,"SA22018_V1"]))
  130. codetmp <- ifelse(is.na(codetmp), 0, codetmp)
  131. sel.SA2.code(ifelse(sel.SA2.code() == codetmp, 0, codetmp))
  132. updateMap()
  133. })
  134. output$lochtml <- renderUI({
  135. seled <- sel.SA2.code()
  136. seled <- ifelse(is.na(seled), 0, seled)
  137. if (seled == 0) {
  138. HTML("")
  139. } else {
  140. hrstr <- "<hr style='border-top: 1px solid #000;'/>"
  141. str <- sprintf("<p><b>%s</b></p>",
  142. shpf@data$SA22018__1[shpf@data$SA22018_V1 == seled])
  143. if (input$radioinout == "work") {
  144. str <- paste0("<p>People who work in</p>", str)
  145. vals <- as.numeric(work_to[work_to$work_code == seled, 5:15])
  146. vals <- ifelse(is.na(vals), 0, vals)
  147. vals <- ifelse(vals < 0, "~0", as.character(vals))
  148. listi <- paste0(sprintf("<li>%s: %s</li>", cols.labs,
  149. vals),
  150. collapse="")
  151. str <- paste0(hrstr, str, "<ul>", listi, "</ul>")
  152. } else {
  153. str <- paste0("<p>People who live in</p>", str)
  154. vals <- as.numeric(work_from[work_from$res_code == seled, 5:15])
  155. vals <- ifelse(is.na(vals), 0, vals)
  156. vals <- ifelse(vals < 0, "~0", as.character(vals))
  157. listi <- paste0(sprintf("<li>%s: %s</li>", cols.labs,
  158. vals),
  159. collapse="")
  160. str <- paste0(hrstr, str, "<ul>", listi, "</ul>")
  161. }
  162. HTML(str)
  163. }
  164. })
  165. }
  166. # Run the application
  167. shinyApp(ui = ui, server = server)