-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.R
102 lines (76 loc) · 3.09 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
library(dplyr)
library(tidyr)
library(leaflet)
library(rgdal)
library(DT)
# data downloaded from http://data.london.gov.uk/dataset/statistical-gis-boundary-files-london
boroughs<-readOGR(dsn="Data/statistical-gis-boundaries-london/ESRI", layer="London_Borough_Excluding_MHW")
# Cut out unnecessary columns
boroughs@data<-boroughs@data[,c(1,2)]
# transform to WGS884 reference system
boroughs<-spTransform(boroughs, CRS("+init=epsg:4326"))
# Find the edges of our map
bounds<-bbox(boroughs)
# Get the income data
income_long<-read.csv("Data/income_long.csv")
function(input, output, session){
getDataSet<-reactive({
# Get a subset of the income data which is contingent on the input variables
dataSet<-income_long[income_long$Year==input$dataYear & income_long$Measure==input$meas,]
# Copy our GIS data
joinedDataset<-boroughs
# Join the two datasets together
joinedDataset@data <- suppressWarnings(left_join(joinedDataset@data, dataSet, by="NAME"))
# If input specifies, don't include data for City of London
if(input$city==FALSE){
joinedDataset@data[joinedDataset@data$NAME=="City of London",]$Income=NA
}
joinedDataset
})
# Due to use of leafletProxy below, this should only be called once
output$londonMap<-renderLeaflet({
leaflet() %>%
addTiles() %>%
# Centre the map in the middle of our co-ordinates
setView(mean(bounds[1,]),
mean(bounds[2,]),
zoom=10 # set to 10 as 9 is a bit too zoomed out
)
})
observe({
theData<-getDataSet()
# colour palette mapped to data
pal <- colorQuantile("YlGn", theData$Income, n = 10)
# set text for the clickable popup labels
borough_popup <- paste0("<strong>Borough: </strong>",
theData$NAME,
"<br><strong>",
input$meas,"
income: </strong>£",
formatC(theData$Income, format="d", big.mark=',')
)
# If the data changes, the polygons are cleared and redrawn, however, the map (above) is not redrawn
leafletProxy("londonMap", data = theData) %>%
clearShapes() %>%
addPolygons(data = theData,
fillColor = pal(theData$Income),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 2,
popup = borough_popup)
})
# table of results, rendered using data table
output$boroughTable <- renderDataTable(datatable({
dataSet<-getDataSet()
dataSet<-dataSet@data[,c(1,6)] # Just get name and value columns
names(dataSet)<-c("Borough",paste0(input$meas," income") )
dataSet
},
options = list(lengthMenu = c(5, 10, 33), pageLength = 5))
)
# year selecter; values based on those present in the dataset
output$yearSelect<-renderUI({
yearRange<-sort(unique(as.numeric(income_long$Year)), decreasing=TRUE)
selectInput("dataYear", "Year", choices=yearRange, selected=yearRange[1])
})
}