-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
227 lines (170 loc) · 9.69 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
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
# server.R
library(shinydashboard)
library(googleVis)
library(leaflet)
source('scripts/calculated_metrics.R') ## Import metric calculations
## Load beginning and end industry datasets
begin_dataset <- read.csv('data/cluster2006.csv', stringsAsFactors=FALSE)
end_dataset <- read.csv('data/cluster2011.csv', stringsAsFactors=FALSE)
## Load dataset with cluster type definitions
cluster_types <- read.csv('data/clustertypes.csv', stringsAsFactors=FALSE)
shinyServer(function(input, output) {
## Create region dropdown menu
choose_region <- reactive({
return(unique(end_dataset$region))
})
output$region <- renderUI({
unique_regions <- choose_region()
selectInput("select_region", "Region of interest", choices = unique_regions,
unique_regions[1])
})
## Create bubble plot title
output$region_title <- renderText({input$select_region})
output$analysis_title <- renderText({input$analysisType})
selected_region <- reactive({input$select_region})
selected_type <- reactive({input$clusterType})
## Create data frames based on different types of analysis
build_visdf <- reactive({
## Build dataframe for Employment Growth Composition
if(input$analysisType == "Employment Growth Composition"){
yvar <- RegionShare(end_dataset, "employee_count")
xvar <- CAGR(begin_dataset, end_dataset, "employee_count", 5)
zvar <- end_dataset[,c('cluster', 'region', "employee_count")] %>% ## Round employee count to
mutate(employee_count = round(employee_count)) ## nearest integer.
## Build dataframe for Employment Share & Specialization
} else if(input$analysisType == "Employment Share & Specialization"){
xvar <- LQ(end_dataset, "employee_count")
yvar <- RegionShare(end_dataset, "employee_count")
zvar <- end_dataset[,c('cluster', 'region', "employee_count")] %>% ## Round employee count to
mutate(employee_count = round(employee_count)) ## nearest integer.
## Build dataframe for Employment Growth & Specialization
} else if(input$analysisType == "Employment Growth & Specialization"){
xvar <- LQ(end_dataset, "employee_count")
yvar <- CAGR(begin_dataset, end_dataset, "employee_count", 5)
zvar <- RegionShare(end_dataset, "employee_count")
## Build dataframe for Economic Dynamism
} else if(input$analysisType == "Economic Dynamism"){
yvar <- CAGR(begin_dataset, end_dataset, "wages", 5)
xvar <- CAGR(begin_dataset, end_dataset, "revenue", 5)
zvar <- CAGR(begin_dataset, end_dataset, "employee_count", 5)
## Build dataframe for Revenue Growth & Specialization
} else {
xvar <- LQ(end_dataset, "revenue")
yvar <- CAGR(begin_dataset, end_dataset, "revenue", 5)
zvar <- RegionShare(end_dataset, "revenue")
}
new_table <- inner_join(xvar, yvar, by = c('cluster', 'region')) %>%
inner_join(zvar, by = c('cluster', 'region'))
names(new_table) <- c("cluster", "region", "xvar", "yvar", "zvar")
if(input$analysisType == "Economic Dynamism"){
new_table <- mutate(new_table, employ_growth = ifelse(zvar > 0, "employment growth", "employment decay" ))
}
## Filter by region
new_table <- new_table[which(new_table$region == selected_region() ),] %>%
inner_join(cluster_types, by = 'cluster')
if(selected_type() == "Traded only"){
new_table <- new_table[which(new_table$type == "traded" ),]
} else if(selected_type() == "Local only") {
new_table <- new_table[which(new_table$type == "local" ),]
}
return(new_table)
})
## Axis labels
axis_labels <- reactive({
## Labels for Employment Growth Composition
if(input$analysisType == "Employment Growth Composition"){
new_labels <- c("Employment growth", "Employment share", "Employment size")
## Labels for Employment Share & Specialization
} else if(input$analysisType == "Employment Share & Specialization"){
new_labels <- c("Location quotient - Employment", "Employment share", "Employment size")
## Labels for Employment Growth & Specialization
} else if(input$analysisType == "Employment Growth & Specialization"){
new_labels <- c("Location quotient - Employment", "Employment growth", "Employment share")
## Labels for Economic Dynamism
} else if(input$analysisType == "Economic Dynamism"){
new_labels <- c("Wage growth", "Revenue growth", "Employment growth")
## Labels for Revenue Growth & Specialization
} else {
new_labels <- c("Location quotient - Revenue", "Revenue growth", "Revenue share")
}
return(new_labels)
})
## Render bubble chart
output$bubble <- renderGvis({
x <- axis_labels()[1]
y <- axis_labels()[2]
z <- axis_labels()[3]
xaxislabel <- paste0("[{title:'", x,"'}]" )
yaxislabel <- paste0("[{title:'", y,"'}]" )
vis_table <- build_visdf()
names(vis_table) <- c("cluster", "region", x, y, z, "type")
Sys.sleep(0.3) ## For some reason, Sys.sleep() needs to be here
colorpalette <- ifelse(input$analysisType == "Economic Dynamism", "['#ff2d03','#0353ff']", "['#0353ff', '#ffab03']")
gvisBubbleChart(vis_table, idvar = "cluster", xvar = x, yvar = y,
colorvar = "type", sizevar = z,
options = list(chartArea = '{left:60, top:10, bottom:50, right: 95,width:"80%", height:"50%"}',
explorer="{actions: ['dragToPan', 'rightClickToReset'], maxZoomIn:0.05}",
width= "600px", height= "450px",
hAxes=xaxislabel, vAxes=yaxislabel,
colors= colorpalette,
legend = "bottom",
bubble= "{textStyle:{color: 'none'}}"))
})
## Serve HTML files
output$htmlfile <- renderUI({
file <- switch(input$analysisType,
"Employment Growth Composition" = "html/employ-growth-comp.html",
"Employment Share & Specialization" = "html/employ-share-spec.html",
"Employment Growth & Specialization" = "html/employ-growth-spec.html",
"Revenue Growth & Specialization" = "html/rev-growth-spec.html",
"Economic Dynamism" = "html/econ-dynamism.html",
stop("Unknown option")
)
includeHTML(file)
})
## Load TopoJSON data with region boundaries for Leaflet map
loadjson <- reactive({
## Use switch statement to choose which TopoJSON file to load
region_name <- switch(input$select_region,
"Changhua County" = "changhua",
"Chiayi City" = "chiayicity",
"Chiayi County" = "chiayicounty",
"Hsinchu City" = "hsinchucity",
"Hsinchu County" = "hsinchucounty",
"Hualien County" = "hualien",
"Kaohsiung City" = "kaohsiung",
"Keelung City" = "keelung",
"Kinmen County" = "kinmen",
"Lienchiang County" = "lienchiang",
"Miaoli County"= "miaoli",
"Nantou County" = "nantou",
"New Taipei City" = "newtaipei",
"Penghu County" = "penghu",
"Pingtung County" = "pingtung",
"Taichung City" = "taichung",
"Tainan City" = "tainan",
"Taipei City" = "taipei",
"Taitung County" = "taitung",
"Taoyuan City" = "taoyuan",
"Taiwan Total" = "total",
"Yilan County" = "yilan",
"Yunlin County" = "yunlin",
"North Region" = "north",
"Northwest Region" = "northwest",
"Middle Region" = "middle",
"Southwest Region" = "southwest",
"South Region" = "south",
"East Region" = "east",
"Outer Islands" = "outerislands")
json_path <- paste0("data/topojson/", region_name, ".json") %>%
readLines()
})
## Render Leaflet map
output$twmap <- renderLeaflet({
topoData <- loadjson()
leaflet() %>%
setView(lng = 121, lat = 23.6, zoom = 7) %>%
addProviderTiles("Hydda.Base") %>%
addTopoJSON(topoData, weight = 1, color = "#FF6347", fill = TRUE)
})
})