Browse Source

make actual shiny, reshaping data

Petra Lamborn 3 years ago
parent
commit
a204b2821a
2 changed files with 90 additions and 5 deletions
  1. 65
    0
      scratch.R
  2. 25
    5
      viz/app.R

+ 65
- 0
scratch.R View File

@@ -27,3 +27,68 @@ locgraph <- graph(edges = elist)
27 27
 # plot(locgraph, label = NA)
28 28
 
29 29
 sg <- decompose(locgraph, mode="weak")
30
+
31
+
32
+work_travel %>% rename(
33
+  res_code = SA2_code_usual_residence_address,
34
+  res_name = SA2_name_usual_residence_address,
35
+  res_east = SA2_usual_residence_easting,
36
+  res_north = SA2_usual_residence_northing,
37
+  work_code = SA2_code_workplace_address,
38
+  work_name = SA2_name_workplace_address,
39
+  work_east = SA2_workplace_easting,
40
+  work_north = SA2_workplace_northing,
41
+  home = Work_at_home,
42
+  private = Drive_a_private_car_truck_or_van,
43
+  company = Drive_a_company_car_truck_or_van,
44
+  passenger = Passenger_in_a_car_truck_van_or_company_bus,
45
+  bus = Public_bus,
46
+  train = Train,
47
+  bicycle = Bicycle,
48
+  walk = Walk_or_jog,
49
+  ferry = Ferry,
50
+  other = Other,
51
+  total = Total
52
+) -> work_simp
53
+
54
+work_simp %>% 
55
+  group_by(res_code,
56
+                         res_name,
57
+                         res_east,
58
+                         res_north) %>%
59
+  summarise(
60
+    home = sum(ifelse(home < 0, 0, home)),
61
+    private = sum(ifelse(private < 0, 0, private)),
62
+    company = sum(ifelse(company < 0, 0, company)),
63
+    passenger = sum(ifelse(passenger < 0, 0, passenger)),
64
+    bus = sum(ifelse(bus < 0, 0, bus)),
65
+    train = sum(ifelse(train < 0, 0, train)),
66
+    bicycle = sum(ifelse(bicycle < 0, 0, bicycle)),
67
+    walk = sum(ifelse(walk < 0, 0, walk)),
68
+    ferry = sum(ifelse(ferry < 0, 0, ferry)),
69
+    other = sum(ifelse(other < 0, 0, other)),
70
+    total = sum(ifelse(total < 0, 0, total)), .groups="drop"
71
+  ) -> work_from
72
+    
73
+
74
+work_simp %>% 
75
+  group_by(work_code,
76
+                         work_name,
77
+                         work_east,
78
+                         work_north) %>%
79
+  summarise(
80
+    home = sum(ifelse(home < 0, 0, home)),
81
+    private = sum(ifelse(private < 0, 0, private)),
82
+    company = sum(ifelse(company < 0, 0, company)),
83
+    passenger = sum(ifelse(passenger < 0, 0, passenger)),
84
+    bus = sum(ifelse(bus < 0, 0, bus)),
85
+    train = sum(ifelse(train < 0, 0, train)),
86
+    bicycle = sum(ifelse(bicycle < 0, 0, bicycle)),
87
+    walk = sum(ifelse(walk < 0, 0, walk)),
88
+    ferry = sum(ifelse(ferry < 0, 0, ferry)),
89
+    other = sum(ifelse(other < 0, 0, other)),
90
+    total = sum(ifelse(total < 0, 0, total)), .groups="drop"
91
+  ) -> work_to
92
+
93
+tencols <-  c("#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", 
94
+              "#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a")

+ 25
- 5
viz/app.R View File

@@ -1,19 +1,39 @@
1 1
 library(shiny)
2 2
 library(leaflet)
3 3
 library(rgdal)
4
+library(readr)
5
+library(dplyr)
4 6
 
5
-sf <- readOGR(dsn="../shapefiles/sa20025WGSfilcth")
6
-leaflet(sf, options = leafletOptions(minZoom = 3, maxZoom = 13)) %>% 
7
-  addPolygons(color="#000", opacity = 1, weight=1,
8
-                            popup = sf@data$SA22018__1) %>%
9
-  setView(174, -41, 5)
7
+work_travel <- read_csv("../travel-work.csv")
8
+shpf <- readOGR(dsn="../shapefiles/sa20025WGSfilcth")
10 9
 
11 10
 # Define UI
12 11
 ui <- fluidPage(
12
+  leafletOutput("map")
13 13
 )
14 14
 
15 15
 # Define server logic
16 16
 server <- function(input, output) {
17
+  output$map <- renderLeaflet({
18
+    leaflet(shpf, options = leafletOptions(minZoom = 3, maxZoom = 13)) %>% 
19
+      addPolygons(color="#000", opacity = 1, weight=1,
20
+                                popup = shpf@data$SA22018__1) %>%
21
+      setView(174, -41, 5)
22
+  })
23
+  observeEvent(input$map_shape_click, {
24
+    p <- input$map_shape_click
25
+    print(p)
26
+    pdat <- data.frame(Longitude = p$lng,
27
+                      Latitude =p$lat)
28
+    # Assignment modified according
29
+    coordinates(pdat) <- ~ Longitude + Latitude
30
+    # Set the projection of the SpatialPointsDataFrame using the projection of the shapefile
31
+    proj4string(pdat) <- proj4string(shpf)
32
+    ppoly <- over(pdat, shpf)
33
+    print(ppoly)
34
+    print(work_travel[work_travel$SA2_code_usual_residence_address == 
35
+                        ppoly[1,"SA22018_V1"],])
36
+  })
17 37
 }
18 38
 
19 39
 # Run the application