Browse Source

Start to add controls

Note: starting map is reverse type
to map when clicking on location
Petra Lamborn 3 years ago
parent
commit
d4c492335b
1 changed files with 64 additions and 28 deletions
  1. 64
    28
      viz/app.R

+ 64
- 28
viz/app.R View File

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