-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
251 lines (194 loc) · 8.8 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
library(dplyr)
library(ggplot2)
shinyServer(function(input, output, session) {
output$group <- renderUI({
groupNames <- groupTable[["groupName"]]
opts <- selectInput("gc_name", "Which Group are you looking for:",choices = groupNames, selected=groupNames[2])
list(opts)
})
output$domain <- renderUI({
gc <- groupTable[groupTable$groupName == input$gc_name,]$groupCode
# x <- groupNames[1]
# gc <- groupTable[groupTable$groupName == x,]$groupCode
subdomainTable <- domainTable[domainTable$groupCode == gc,]
domainNames <- subdomainTable[["domainName"]]
opts <- selectInput("domain_name", "Which Domain are you looking for:",
choices = domainNames, selected=domainNames[1])
list(opts)
})
output$indOrAgg <- renderUI({
ind_or_agg <- data.frame(name = c("(0) Individual item (e.g. Apples, Wheat)",
"(1) Aggregated item (e.g. Total cereals, Total meat"),
code = c(0,1),
stringsAsFactors = FALSE
)
values <- ind_or_agg$name
opts <- selectInput("ind_or_agg", "Are you looking for individual item or aggregated item:",
choices = values, selected=values[1])
list(opts)
})
output$item <- renderUI({
dc <- domainTable[domainTable$domainName == input$domain_name,]$domainCode
# agg <- ind_or_agg[ind_or_agg$name == input$ind_or_agg,]$code
# x <- values[2]
# agg <- ind_or_agg[ind_or_agg$name == x,]$code
#if (agg == 1) {
# subitemTable = itemAggTable[itemAggTable$domainCode == dc,]
#} else {
subitemTable = itemTable[itemTable$domainCode == dc,]
#}
values <- subitemTable$itemName
opts <- selectInput("item_name", "Which Item are you looking for?",
choices = values, selected=values[1])
list(opts)
})
output$element <- renderUI({
dc <- domainTable[domainTable$domainName == input$domain_name,]$domainCode
subelementTable = elementTable[elementTable$domainCode == dc,]
values <- as.character(subelementTable$elementName)
opts <- selectInput("element_name", "Which Element are you looking for?",
choices = values, selected=values[1])
list(opts)
})
fao_data <- reactive({
dc <- domainTable[domainTable$domainName == input$domain_name,]$domainCode
ec <- elementTable[elementTable$elementName == input$element_name & elementTable$domainCode == dc,]$elementCode
ic <- itemTable[itemTable$itemName == input$item_name & itemTable$domainCode == dc,]$itemCode
dat <- getFAOtoSYB(domainCode = dc,
elementCode = ec,
itemCode = ic)
dat[["entity"]]
})
output$yearRange <- renderUI({
data <- fao_data()
maxim <- max(data$Year)
minim <- min(data$Year)
opts <- sliderInput("year_range", "Select year range", min = minim, max = maxim, value = c(minim,maxim), step = 1)
list(opts)
})
fao_data_filtered <- reactive({
data <- fao_data()
data %>%
filter(Year >= input$year_range[1] & Year <= input$year_range[2]) %>%
arrange(FAOST_CODE)
})
output$mytable = renderDataTable({
fao_data_filtered()
},options = list(pageLength = 10))
output$downloadData <- downloadHandler(
filename = function() { paste(input$domain_name,input$element_name,input$item_name,input$year_range[1],input$year_range[2],'.csv', sep="-") },
content = function(file) {
write.csv(fao_data_filtered(), file, row.names = FALSE)
}
)
output$timeSeries <- reactivePlot(function() {
plot_data <- fao_data_filtered()
names(plot_data) <- c("FAOST_CODE","Year","value")
plot_data <- merge(plot_data,reg,by="FAOST_CODE")
p <- ggplot(plot_data, aes(x=Year, y=value,group=FAOST_CODE,color=Region))
p <- p + geom_point() + geom_line()
p <- p + geom_text(data=plot_data[plot_data$Year ==input$year_range[2],],
aes(x=Year,y=value,label=Country))
p <- p + theme_minimal() +
theme(legend.position = "top") +
theme(text = element_text(family = "Open Sans", size= 12)) +
theme(legend.title = element_text(size = 12, face = "bold")) +
theme(axis.text= element_text(size = 10)) +
theme(axis.title = element_text(size = 12, face = "bold")) +
theme(legend.text= element_text(size = 12)) +
theme(strip.text = element_text(size = 14, face="bold")) +
guides(colour = guide_legend(override.aes = list(size=4)))
print(p)
})
#### Bivariate stuff
output$var_x_item <- renderUI({
itemNames <- itemTable[["itemName"]]
opts <- selectizeInput('varXitem', "Which item are you looking for:", choices = itemNames, selected=itemNames[1], multiple = FALSE)
list(opts)
})
output$var_x_element <- renderUI({
domainCode <- itemTable[itemTable[["itemName"]] == input$varXitem, ]$domainCode
elementNames <- as.character(elementTable[elementTable[["domainCode"]] %in% domainCode, ]$elementName)
opts <- selectizeInput('varXelement', "Which element are you looking for:", choices = elementNames, multiple = FALSE)
list(opts)
})
output$var_y_item <- renderUI({
itemNames <- itemTable[["itemName"]]
opts <- selectizeInput('varYitem', "Which item are you looking for:", choices = itemNames, selected=itemNames[3], multiple = FALSE)
list(opts)
})
output$var_y_element <- renderUI({
domainCode <- itemTable[itemTable[["itemName"]] == input$varYitem, ]$domainCode
elementNames <- as.character(elementTable[elementTable[["domainCode"]] %in% domainCode, ]$elementName)
opts <- selectizeInput('varYelement', "Which element are you looking for:", choices = elementNames, multiple = FALSE)
list(opts)
})
bivar_data <- reactive({
domainCodeX <- itemTable[itemTable[["itemName"]] == input$varXitem, ]$domainCode
domainCodeY <- itemTable[itemTable[["itemName"]] == input$varYitem, ]$domainCode
dcX <- domainCodeX
ecX <- elementTable[elementTable$elementName == input$varXelement & elementTable$domainCode == dcX,]$elementCode
icX <- itemTable[itemTable[["itemName"]] == input$varXitem, ]$itemCode
dcY <- domainCodeY
ecY <- elementTable[elementTable$elementName == input$varYelement & elementTable$domainCode == dcY,]$elementCode
icY <- itemTable[itemTable[["itemName"]] == input$varYitem, ]$itemCode
Xvar <- getFAOtoSYB(domainCode = dcX,
elementCode = ecX,
itemCode = icX)
X <- Xvar[["entity"]]
Yvar <- getFAOtoSYB(domainCode = dcY,
elementCode = ecY,
itemCode = icY)
Y <- Yvar[["entity"]]
XY <- inner_join(X, Y, by = c("FAOST_CODE","Year"))
na.omit(XY)
})
output$bivar_year <- renderUI({
year_ss <- bivar_data()
unique(year_ss$Year)
opts <- sliderInput("bivarYear", "", min = min(year_ss$Year), max = max(year_ss$Year), value = max(year_ss$Year), step = 1)
list(opts)
})
output$single_scatter <- reactivePlot(function() {
plot_data <- bivar_data()
plot_data <- plot_data[plot_data$Year == input$bivarYear,]
names(plot_data) <- c("FAOST_CODE","Year","valueX","valueY")
plot_data <- merge(plot_data,reg,by="FAOST_CODE")
p <- ggplot(plot_data, aes(x=valueX, y=valueY,group=1,color=Region,label=Country))
p <- p + geom_point()
p <- p + geom_text()
p <- p + theme_minimal() +
theme(legend.position = "top") +
theme(text = element_text(family = "Open Sans", size= 12)) +
theme(legend.title = element_text(size = 12, face = "bold")) +
theme(axis.text= element_text(size = 10)) +
theme(axis.title = element_text(size = 12, face = "bold")) +
theme(legend.text= element_text(size = 12)) +
theme(strip.text = element_text(size = 14, face="bold")) +
guides(colour = guide_legend(override.aes = list(size=4)))
p <- p + labs(x=input$varXitem,y=input$varYitem)
p <- p + geom_smooth(method = "loess")
print(p)
})
output$multi_scatter <- reactivePlot(function() {
plot_data <- bivar_data()
names(plot_data) <- c("FAOST_CODE","Year","valueX","valueY")
plot_data <- merge(plot_data,reg,by="FAOST_CODE")
p <- ggplot(plot_data, aes(x=valueX, y=valueY, group=1,color=Region,label=Country))
p <- p + geom_point()
p <- p + geom_text(size=2.5)
p <- p + theme_minimal() +
theme(legend.position = "top") +
theme(text = element_text(family = "Open Sans", size= 12)) +
theme(legend.title = element_text(size = 12, face = "bold")) +
theme(axis.text= element_text(size = 10)) +
theme(axis.title = element_text(size = 12, face = "bold")) +
theme(legend.text= element_text(size = 12)) +
theme(strip.text = element_text(size = 14, face="bold")) +
guides(colour = guide_legend(override.aes = list(size=4)))
p <- p + labs(x=input$varXitem,y=input$varyitem)
p <- p + facet_wrap(~Year)
p <- p + geom_smooth(method = "loess")
print(p)
})
})