123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- library(shiny)
- library(shinyjs)
- library(leaflet)
- library(rgdal)
- library(dplyr)
- library(leaflet.extras)
-
- # work_travel <- read_csv("../travel-work.csv")
- load(file="datasets.RData")
- shpf <- readOGR(dsn="../shapefiles/sa20025WGSfilcth")
- sa.in.dest <- shpf@data$SA22018_V1 %in% work_to$work_code
- sa.in.home <- shpf@data$SA22018_V1 %in% work_from$res_code
- transport.t <- c("Work at home", "Private car", "Company car",
- "Carpool", "Bus", "Train", "Bicycle", "Walk",
- "Ferry", "Other", "None")
- cols.labs <- c(transport.t[1:10], "Total")
-
- codelist <- shpf@data %>%
- mutate(sa2_code = as.numeric(as.character(SA22018_V1))) %>%
- select(sa2_code)
-
- startcols <- codelist %>% left_join(work_from, by = c("sa2_code" = "res_code"))
- startcols <- tencols[startcols$MAX]
- startcols <- ifelse(is.na(startcols), "#808080", startcols)
-
-
- # Define UI
- ui <- fluidPage(
- useShinyjs(),
- tags$style(type = "text/css",
- "html, body {
- width:100%;
- height:100%
- }
- #map {
- height: 100% !important;
- position: absolute !important;
- top: 0;
- left: 0;
- }
- #loading {
- cursor: progress !important;
- }
- #loading p {
- border-radius: 25px;
- background: #FFFFFF;
- padding: 10px;
- border: 2px solid #000000;
- font-size: 1.5em;
- font-weight: bold;
- }
- #mapcontrol {
- background-color: rgba(255, 255, 255, 0.8);
- border-radius: 5px;
- box-shadow: 0 0 15px rgba(0,0,0,0.2);
- padding: 6px 8px;
- font: 14px/16px Arial, Helvetica, sans-serif;
- }
- #lochtml ul {
- padding-left: 15px;
- }"),
- leafletOutput("map"),
- absolutePanel(top = 10, right = 10, id="mapcontrol",
- radioButtons("radioinout", label=NULL,
- choices = c(
- "Work in" = "work",
- "Live in" = "res"
- ),
- inline = TRUE),
- div(id="locinfo",
- htmlOutput("lochtml"))),
- absolutePanel(bottom = 30, left = 30, id="loading",
- p("Loading..."))
- )
-
- # Define server logic
- server <- function(input, output) {
- sel.SA2.code <- reactiveVal(0)
- p.layers <- c("polya", "polyb")
- output$map <- renderLeaflet({
- leaf <- leaflet(shpf, options = leafletOptions(minZoom = 3, maxZoom = 13)) %>%
- addPolygons(group = p.layers[1] ,color="#000", opacity = 1, weight=1,
- fillColor = startcols,
- label = shpf@data$SA22018__1,
- fillOpacity = 1) %>%
- setView(174, -41, 5) %>%
- addResetMapButton() %>%
- addLegend(position = "topleft",
- colors = c(tencols, "#808080"),
- labels = transport.t, opacity = 1)
- shinyjs::hideElement(selector="#loading p", asis = TRUE,
- anim=TRUE, animType = "slide", time=7)
- leaf
- })
- updateMap <- function() {
- shinyjs::showElement(selector="#loading p", asis = TRUE,
- anim=TRUE, animType = "slide")
- fcols <- startcols
- if (sel.SA2.code() != 0) {
- codvs <- work_simp %>% filter(work_code == sel.SA2.code())
- codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "res_code"))
- codvs <- tencols[codvs$MAX]
- fcols <- ifelse(is.na(codvs), "#808080", codvs)
- }
- leafletProxy("map", data = shpf) %>%
- addPolygons(group = p.layers[2] ,color="#000", opacity = 1, weight=1,
- fillColor = fcols,
- label = shpf@data$SA22018__1,
- fillOpacity = 1) %>%
- clearGroup(p.layers[1])
- p.layers <<- rev(p.layers)
- shinyjs::hideElement(selector="#loading p", asis=TRUE,
- anim=TRUE, animType = "slide", time = 7)
- }
- observeEvent(input$map_shape_click, {
- p <- input$map_shape_click
- pdat <- data.frame(Longitude = p$lng,
- Latitude =p$lat)
- coordinates(pdat) <- ~ Longitude + Latitude
- proj4string(pdat) <- proj4string(shpf)
- ppoly <- over(pdat, shpf)
- codetmp <- as.numeric(as.character(ppoly[1,"SA22018_V1"]))
- sel.SA2.code(ifelse(sel.SA2.code() == codetmp, 0, codetmp))
- updateMap()
- })
- output$lochtml <- renderUI({
- seled <- sel.SA2.code()
- if (seled == 0) {
- HTML("")
- } else {
- str <- sprintf("<hr style='border-top: 1px solid #000;'/><h4>%s</h4>",
- shpf@data$SA22018__1[shpf@data$SA22018_V1 == seled])
- if (input$radioinout == "work") {
- vals <- as.numeric(work_to[work_to$work_code == seled, 5:15])
- vals <- ifelse(is.na(vals), 0, vals)
- vals <- ifelse(vals < 0, "~0", as.character(vals))
- listi <- paste0(sprintf("<li>%s: %s</li>", cols.labs,
- vals),
- collapse="")
- str <- paste0(str, "<ul>", listi, "</ul>")
- } else {
- vals <- as.numeric(work_from[work_from$res_code == seled, 5:15])
- vals <- ifelse(is.na(vals), 0, vals)
- vals <- ifelse(vals < 0, "~0", as.character(vals))
- listi <- paste0(sprintf("<li>%s: %s</li>", cols.labs,
- vals),
- collapse="")
- str <- paste0(str, "<ul>", listi, "</ul>")
- }
- HTML(str)
- }
- })
- }
-
- # Run the application
- shinyApp(ui = ui, server = server)
|