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
 library(leaflet)
3
 library(leaflet)
4
 library(rgdal)
4
 library(rgdal)
5
 library(dplyr)
5
 library(dplyr)
6
+library(leaflet.extras)
6
 
7
 
7
 # work_travel <- read_csv("../travel-work.csv")
8
 # work_travel <- read_csv("../travel-work.csv")
8
 load(file="datasets.RData")
9
 load(file="datasets.RData")
12
 transport.t <- c("Work at home", "Private car", "Company car", 
13
 transport.t <- c("Work at home", "Private car", "Company car", 
13
                 "Carpool", "Bus", "Train", "Bicycle", "Walk",
14
                 "Carpool", "Bus", "Train", "Bicycle", "Walk",
14
                 "Ferry", "Other", "None")
15
                 "Ferry", "Other", "None")
16
+cols.labs <- c(transport.t[1:10], "Total")
15
 
17
 
16
 codelist <- shpf@data %>% 
18
 codelist <- shpf@data %>% 
17
   mutate(sa2_code = as.numeric(as.character(SA22018_V1))) %>% 
19
   mutate(sa2_code = as.numeric(as.character(SA22018_V1))) %>% 
46
     border: 2px solid #000000;
48
     border: 2px solid #000000;
47
     font-size: 1.5em;
49
     font-size: 1.5em;
48
     font-weight: bold;
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
   leafletOutput("map"),
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
   absolutePanel(bottom = 30, left = 30, id="loading",
72
   absolutePanel(bottom = 30, left = 30, id="loading",
52
                 p("Loading..."))
73
                 p("Loading..."))
53
 )
74
 )
54
 
75
 
55
 # Define server logic
76
 # Define server logic
56
 server <- function(input, output) {
77
 server <- function(input, output) {
57
-  sel.SA2.code <- 0
78
+  sel.SA2.code <- reactiveVal(0)
58
   p.layers <- c("polya", "polyb")
79
   p.layers <- c("polya", "polyb")
59
   output$map <- renderLeaflet({
80
   output$map <- renderLeaflet({
60
     leaf <- leaflet(shpf, options = leafletOptions(minZoom = 3, maxZoom = 13)) %>% 
81
     leaf <- leaflet(shpf, options = leafletOptions(minZoom = 3, maxZoom = 13)) %>% 
63
                   label = shpf@data$SA22018__1,
84
                   label = shpf@data$SA22018__1,
64
                   fillOpacity = 1) %>%
85
                   fillOpacity = 1) %>%
65
       setView(174, -41, 5) %>%
86
       setView(174, -41, 5) %>%
87
+      addResetMapButton() %>%
66
       addLegend(position = "topleft",
88
       addLegend(position = "topleft",
67
                 colors = c(tencols, "#808080"),
89
                 colors = c(tencols, "#808080"),
68
                 labels = transport.t, opacity = 1)
90
                 labels = transport.t, opacity = 1)
70
                          anim=TRUE, animType = "slide", time=7)
92
                          anim=TRUE, animType = "slide", time=7)
71
     leaf
93
     leaf
72
   })
94
   })
73
-  observeEvent(input$map_shape_click, {
95
+  updateMap <- function() {
74
     shinyjs::showElement(selector="#loading p", asis = TRUE, 
96
     shinyjs::showElement(selector="#loading p", asis = TRUE, 
75
                          anim=TRUE, animType = "slide")
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
     fcols <- startcols
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
       codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "res_code"))
101
       codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "res_code"))
99
-      print(head(codvs))
100
-      print(table(codvs$MAX))
101
       codvs <- tencols[codvs$MAX]
102
       codvs <- tencols[codvs$MAX]
102
-      print(table(codvs))
103
       fcols <- ifelse(is.na(codvs), "#808080", codvs)
103
       fcols <- ifelse(is.na(codvs), "#808080", codvs)
104
-      print(table(fcols))
105
     }
104
     }
106
-    print(table(fcols))
107
     leafletProxy("map", data = shpf) %>%
105
     leafletProxy("map", data = shpf) %>%
108
       addPolygons(group = p.layers[2] ,color="#000", opacity = 1, weight=1,
106
       addPolygons(group = p.layers[2] ,color="#000", opacity = 1, weight=1,
109
                                 fillColor = fcols,
107
                                 fillColor = fcols,
113
     p.layers <<- rev(p.layers)
111
     p.layers <<- rev(p.layers)
114
     shinyjs::hideElement(selector="#loading p", asis=TRUE, 
112
     shinyjs::hideElement(selector="#loading p", asis=TRUE, 
115
                          anim=TRUE, animType = "slide", time = 7)
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