-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject2.Rmd
More file actions
324 lines (254 loc) · 9.43 KB
/
Project2.Rmd
File metadata and controls
324 lines (254 loc) · 9.43 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
---
title: "Wellbeing in the UK"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
source_code: embed
theme: simplex
runtime: shiny
---
```{r context="setup", include=FALSE}
library(shiny)
library(ggplot2)
library(dplyr)
library(flexdashboard)
library(plotly)
require(rgdal)
require(leaflet)
require(readxl)
library(jsonlite) # fromJSON
library(utils) # URLencode functions
require(stringr)
require(leaflet.extras)
```
```{r, data_load, results='hide'}
railway_data <- readOGR("./GBR_rails.shp", GDAL1_integer64_policy=TRUE) # for lines map
admin_data <- read.csv("GBR_adm2.csv") # for poly map
admin.load <- readOGR("./GBR_adm2.shp", GDAL1_integer64_policy=TRUE) # for poly map
happiness_data <- read.csv("HappinessData.csv")
```
```{r, cleaning}
admin_data <- admin_data[ -c(2:5, 7, 9, 11:12) ]
names(admin_data) <- c("OBJECTID", "Country", "Area", "Type")
happiness_data <- subset(happiness_data, happiness_data$Area!="City of London")
# change necessary columns to numeric data for plotting
happiness_data$Happiness = as.numeric(happiness_data$Happiness)
happiness_data$NoReligion = as.numeric(happiness_data$NoReligion)
happiness_data$NoSport = as.numeric(happiness_data$NoSport)
happiness_data$ChildhoodObesity = as.numeric(happiness_data$ChildhoodObesity)
happiness_data$SuicideRAtes = as.numeric(happiness_data$SuicideRates)
happiness_data$CrimeRates = as.numeric(happiness_data$CrimeRates)
# merge happiness dataset with administrative areas dataset
admin_data <-left_join(x = admin_data, y = happiness_data, by = "Area")
# merge above dataset with the shape file
admin <- admin.load[admin.load$ID_2 %in% admin_data$OBJECTID,]
admin@data <- merge(admin@data, admin_data, sort=FALSE, by.x='ID_2', by.y='OBJECTID') %>%
distinct(Area, .keep_all=TRUE) # don't use area duplicates when merging
```
Inputs {.sidebar}
=======================================================================
```{r context="render"}
radioButtons(inputId = "countrySelect",
label = "Country Filter:",
choices = c('England' = 'England',
'Northern Ireland' = 'Northern Ireland',
'Scotland' = 'Scotland',
'Wales' = 'Wales'),
selected = ('England'))
```
```{r context="render"}
checkboxGroupInput(inputId = 'Year',
label = "Select Year(s):",
choices = c('2011' = '2011',
'2012' = '2012',
'2013' = '2013',
'2014' = '2014',
'2015' = '2015',
'2016' = '2016'),
selected = ('2016'))
```
```{r}
admin_subset <- reactive({
admin_data %>%
# filter for year in happiness data
# but keep location information when ther eis no happiness data
filter(Year %in% input$Year | is.na(admin_data$Year))
})
```
```{r context="render"}
selectInput(inputId = 'x_input',
label = "Plotting against Happiness:",
choices = c('No Religion' = 'NoReligion',
'No Sport' = 'NoSport',
'Childhood Obesity' = 'ChildhoodObesity',
'Suicide Rates' = 'SuicideRates',
'Crime Rates' = 'CrimeRates',
selected = 'NoSport'))
```
```{r context="render"}
output$downloadData <- downloadHandler(
filename = function() {
paste('data-', Sys.Date(), '.csv', sep='')
},
content = function(file) {
write.csv(admin_subset(), file)
}
)
downloadLink(outputId = "downloadData", label = "Click Here to Download Data")
```
Main
=======================================================================
Row {}
-----------------------------------------------------------------------
### United Kingdom Counties and Railroads
```{r context="server"}
countryInputs <- reactive({
admin2 <- subset(admin, Country == input$countrySelect)
return(admin2)
})
output$leaflet <- renderLeaflet({
leaflet() %>%
# Determine base maps
addProviderTiles("OpenStreetMap.HOT", group = "Street Map") %>%
addProviderTiles("Esri.WorldTerrain", group = "World Terrain Map") %>%
setView(lng = -5, lat = 55, zoom = 5) %>%
# Add in layers control
addLayersControl(
baseGroups = c("Street Map", "World Terrain Map"),
options = layersControlOptions(collapsed=FALSE))
})
```
```{r context="render"}
# leaflet proxy updating layer
observe({
proxy <- leafletProxy("leaflet")
proxy %>%
clearShapes() %>%
addPolygons(data = countryInputs(),color = "tomato", fillOpacity = 0.2, weight=2,
highlightOptions = highlightOptions(color="White", bringToFront=TRUE),
popup = ~paste0(countryInputs()$Area)) %>%
addPolylines(data = railway_data, color = "black", weight=1, opacity=1)
})
leafletOutput("leaflet")
```
Row {data-height=100}
-----------------------------------------------------------------------
This map represents administrative areas (such as counties) of the United Kingdom along with lines indicating the presence of railroads. Multiple converging railroads signifies a "hub" of activity, such as a city like London. This mape and the graphs below give you the opportunity to look at data related to overall wellbeing and view the area geographically--for example, Ealing, England had the highest suicide rates of 2016 and it is located near London with multiple train lines running through the small county.
Row {data-height=400}
-----------------------------------------------------------------------
### Relationship between Input and Happiness
```{r context="server"}
output$InputPoint <- renderPlotly({
ggplotly(
# subset out any rows that do not have happiness data
ggplot(subset(admin_subset(), !is.na(admin_subset()$Year)),
aes_string(x=input$x_input, y='Happiness', color='Happiness')) +
scale_color_gradient(low = 'red', high = 'lightyellow') +
geom_point() +
theme(legend.position="none"),
tooltip = c('x', 'y')
)
})
```
```{r context="render"}
plotlyOutput('InputPoint')
```
Row {data-height=250}
-----------------------------------------------------------------------
```{r context="server"}
output$InputBar <- renderPlotly({
ggplotly(
# subset out any rows that do not have happiness data
# those rows were needed for mapping but not plotting
ggplot(subset(admin_subset(), !is.na(admin_subset()$Year)),
aes_string(x=paste0('reorder(Area, desc(Happiness))'), y=input$x_input, fill='Happiness')) +
scale_fill_gradient(low = 'red', high = 'lightyellow') +
geom_bar(stat="identity", position=position_dodge()) +
theme(axis.title.y=element_blank(), axis.text.x=element_blank(),legend.position="none") +
coord_flip(),
tooltip = c('y')
)
})
```
```{r context="render"}
plotlyOutput('InputBar')
```
```{r context="server"}
output$HappBar <- renderPlotly({
ggplotly(
# subset out any rows that do not have happiness data
ggplot(subset(admin_subset(), !is.na(admin_subset()$Year)),
aes(x=reorder(Area, desc(Happiness)), y=Happiness, fill=Happiness)) +
scale_fill_gradient(low = 'red', high = 'lightyellow') +
geom_bar(stat="identity", position=position_dodge()) +
theme(axis.title.y=element_blank(), axis.text.x=element_blank(), legend.position="none") +
coord_flip(),
tooltip = c('y')
)
})
```
```{r context="render"}
plotlyOutput('HappBar')
```
Data Table
=======================================================================
Row {}
-----------------------------------------------------------------------
### Avg. Happiness
```{r}
renderValueBox({
avg_Happ <- mean(admin_subset()$Happiness, na.rm = TRUE)
valueBox(avg_Happ)
})
```
### Avg. Not Religious
```{r}
renderValueBox({
avg_Religion <- mean(admin_subset()$NoReligion, na.rm = TRUE)
valueBox(round(avg_Religion, 2))
})
```
### Avg. No Sport
```{r}
renderValueBox({
avg_Sport <- mean(admin_subset()$NoSport, na.rm = TRUE)
valueBox(round(avg_Sport, 2))
})
```
Row {}
-----------------------------------------------------------------------
### Avg. Childhood Obesity
```{r}
renderValueBox({
avg_Ob <- mean(admin_subset()$ChildhoodObesity, na.rm = TRUE)
valueBox(round(avg_Ob, 2))
})
```
### Avg. Suicide Rates
```{r}
renderValueBox({
avg_SuRa <- mean(admin_subset()$SuicideRates, na.rm = TRUE)
valueBox(round(avg_SuRa, 2))
})
```
### Avg. Crime Rates
```{r}
renderValueBox({
avg_Crime <- mean(admin_subset()$CrimeRates, na.rm = TRUE)
valueBox(round(avg_Crime, 2))
})
```
Row {data-height=250}
-----------------------------------------------------------------------
### Happiness Dataset
```{r context="server"}
output$DataTable <- DT::renderDataTable({
DT::datatable(admin_subset(),
options=list(pageLength=10),
rownames=FALSE)
})
```
```{r context="render"}
DT::dataTableOutput('DataTable')
```