-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
344 lines (289 loc) · 10.5 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
server <- function(input, output) {
# Remove scientific notation
options(scipen=999)
# Read data
df <- reactive({
df <- read_csv("bid-tabulations-1.csv")
# Replace NA values with 0s
df <- df %>% mutate_at(vars("Bid Price"), ~replace_na(.,0))
# Replace NA values with 0s
df <- df %>% mutate_at(vars("Class Number"), ~replace_na(.,0))
# Remove Bids without Bid Item values
df <- df %>% drop_na("Bid Item")
# Change date format from m/d/y to Y-m-d
df$`Bid Opening Date` <- format(as.Date(df$`Bid Opening Date`, "%m/%d/%Y"), format = "%Y-%m-%d")
df
})
# Number formatting
f1 <- function(num) {
format(num, big.mark = ' ')
}
# Minimum bid price value over the months
min_bid_price <- reactive({
min_df <- df() %>%
summarise(min = f1(min(df()$`Bid Price`)))
min_df$min
})
# Maximum bid price value over the months
max_bid_price <- reactive({
max_df <- df() %>%
summarise(max = f1(max(df()$`Bid Price`)))
max_df$max
})
# Mean bid price value over the months
mean_bid_price <- reactive({
mean_df <- df() %>%
summarise(mean = f1(mean(df()$`Bid Price`)))
mean_df$mean
})
# Value boxes
output$min_bp <- renderValueBox({
valueBox(
value = paste0("$ ",min_bid_price()),
subtitle = "Minimum Bid Price",
color = "danger",
icon = icon("coins"),
gradient = TRUE
)
})
output$max_bp <- renderValueBox({
valueBox(
value = paste0("$ ", max_bid_price()),
subtitle = "Maximum Bid Price",
color = "danger",
icon = icon("coins"),
gradient = TRUE
)
})
output$mean_bp <- renderValueBox({
valueBox(
value = paste0("$ ", mean_bid_price()),
subtitle = "Average Bid Price",
color = "danger",
icon = icon("coins"),
gradient = TRUE
)
})
# Distribution of Bid Prices
output$fig1 <- renderPlotly({
df() %>%
plot_ly(
y = ~`Bid Price`,
type = 'violin',
box = list(visible = T),meanline = list(visible = T), x0 = 'Bid Price') %>%
layout(
title = "Distribution of Bid Price",
yaxis = list(title = "Bid Price", zeroline = F))
})
# Most Frequent Bids
most_popular_df <- reactive({
# Contact Name
most_popular_bid_df <- df() %>% group_by(`Contact Name`) %>%
summarise(requests = n()) %>%
arrange(desc(requests))
cn <- most_popular_bid_df$`Contact Name`[1]
cnb <- most_popular_bid_df$requests[1]
# Bid Title
most_popular_bid_df1 <- df() %>% group_by(`Bid Title`) %>%
summarise(requests = n()) %>%
arrange(desc(requests))
bt <- most_popular_bid_df1$`Bid Title`[1]
btb <- most_popular_bid_df1$requests[1]
# Bid Item
most_popular_bid_df2 <- df() %>% group_by(`Bid Item`) %>%
summarise(requests = n()) %>%
arrange(desc(requests))
bi <- most_popular_bid_df2$`Bid Item`[1]
bib <- most_popular_bid_df2$requests[1]
# Bidder Name
most_popular_bid_df3 <- df() %>% group_by(`Bidder Name`) %>%
summarise(requests = n()) %>%
arrange(desc(requests))
bn <- most_popular_bid_df3$`Bidder Name`[1]
bnb <- most_popular_bid_df3$requests[1]
most_popular_df <- data.frame(
Indicator = c("Contact Name", "Bid Title", "Bid Item", "Bidder Name"),
Most_Frequent = c(cn, bt, bi, bn),
Bids = c(cnb, btb, bib, bnb)
)
most_popular_df
})
# Data Table
output$table1 <- renderDT({
DT::datatable(most_popular_df(),
rownames = F,
options = list(pageLength = 5, scrollX = TRUE, info = FALSE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#1f77b4', 'color': '#fff'});",
"}")))
})
# Average Bid Price Overtime
output$fig2 <- renderHighchart({
# Average Bid Price
average_bid_price_df <- df() %>% group_by(`Bid Opening Date`) %>%
summarise(Average_bid_price = round(mean(`Bid Price`),2))
average_bid_price_df$`Bid Opening Date` <- as.Date(average_bid_price_df$`Bid Opening Date`)
# Average Bid Price Chart
bid_vp <- xts(x = average_bid_price_df$Average_bid_price,
order.by = as.POSIXct(average_bid_price_df$`Bid Opening Date`))
highchart(type = "stock") %>%
hc_add_series(bid_vp,
type = "line",
color = "#1f77b4") %>%
hc_title(text = "Average Bid Price Overtime") %>%
hc_yAxis(title = list(text = "Average Bid Price"),
opposite = FALSE)
})
# Bid Indicator
output$bid_indicator_select <- renderUI({
varSelectInput("bid_indicator", label = "Select Bid Indicator",
df()[c(9,2,5,6)],
selected = df()[2],
width = 300)
})
# Bid Indicator According to Average Bid Price
bid_category_df <- reactive({
bid_category_df <- df() %>% group_by(!!input$bid_indicator) %>%
summarise(average_bid_price = round(mean(`Bid Price`),2)) %>%
arrange(desc(average_bid_price))
bid_category_df
})
# Top 5 Highest Average Bid Contact Name
top5_bids <- reactive({
top5_bids <- bid_category_df() %>%
slice_head(n=5)
top5_bids
})
# Top 5 Lowest Average Bid Contact Name
bottom5_bids <- reactive({
bottom5_bids <- bid_category_df() %>%
slice_tail(n=5)
bottom5_bids
})
output$fig_top5_bp <- renderHighchart({
top5_bids() %>%
hchart("column", hcaes(x = !!input$bid_indicator, y = average_bid_price),
dataLabels = list(enabled = TRUE)) %>%
hc_title(
text = "Average Bid Price") %>%
hc_colors("#1f77b4") %>%
hc_xAxis(title = list(text = paste0(input$bid_indicator))) %>%
hc_yAxis(title = list(text = "Average Bid Price"))
})
output$fig_bottom5_bp <- renderHighchart({
bottom5_bids() %>%
hchart("column", hcaes(x = !!input$bid_indicator, y = average_bid_price),
dataLabels = list(enabled = TRUE)) %>%
hc_title(
text = "Average Bid Price") %>%
hc_colors("#1f77b4") %>%
hc_xAxis(title = list(text = paste0(input$bid_indicator))) %>%
hc_yAxis(title = list(text = "Average Bid Price"))
})
# Full Data Table
output$table2 <- renderDT({
DT::datatable(df(),
rownames = F,
options = list(pageLength = 2, scrollX = TRUE, info = FALSE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#1f77b4', 'color': '#fff'});",
"}")))
})
# Contact Name Average Bid Price overtime
cn_avg_bid_price_df <- reactive({
cn_avg_bid_price_df <- df() %>%
group_by(`Contact Name`, `Bid Opening Date`) %>%
summarise(Average_bid_price = round(mean(`Bid Price`),2))
cn_avg_bid_price_df
})
# Contact Name Select
output$cn_select <- renderUI({
selectizeInput('cn_sel',
label = "Select Contact Name",
choices = unique(cn_avg_bid_price_df()$`Contact Name`),
selected = cn_avg_bid_price_df()$`Contact Name`[1],
multiple = TRUE,
options = list(maxItems = 3))
})
output$fig3 <- renderHighchart({
cn_avg_bid_price_df() %>%
filter(`Contact Name` %in% !!input$cn_sel) %>%
hchart("line", hcaes(x = `Bid Opening Date`, y = Average_bid_price, group = `Contact Name`)) %>%
hc_title(
text = paste0("Average Bid Price Overtime")
) %>%
hc_xAxis(title = list(text = "Date")) %>%
hc_yAxis(title = list(text = "Average Bid Price"))
})
# Anomalies Tab
model_df <- reactive({
model_df <- select(df(), `Bid Price`, `Bid Opening Date`)
model_df$`Bid Opening Date` <- as.Date(model_df$`Bid Opening Date`)
model_df <- model_df %>% as.tibble()
names(model_df)[1] <- "Price"
names(model_df)[2] <- "date"
model_df <- model_df %>%
tibbletime::as_tbl_time(index = date)
model_df <- model_df[order(model_df$date),]
model_df <- model_df %>%
as_period("daily")
})
# Anomaly Detection
output$fig4 <- renderPlotly({
model_df() %>%
time_decompose(Price) %>%
anomalize(remainder) %>%
time_recompose() %>%
plot_anomalies(time_recomposed = TRUE, ncol = 3, alpha_dots = 0.5) %>%
ggplotly() %>%
layout(
xaxis = list(rangeslider = list(type = "date")
)
)
})
# Anomaly Detection Breakdown
output$fig5 <- renderPlotly({
model_df() %>%
time_decompose(Price, method = "stl", frequency = "auto", trend = "auto") %>%
anomalize(remainder, method = "gesd", alpha = 0.05, max_anoms = 0.2) %>%
plot_anomaly_decomposition() %>% ggplotly()
})
# Anomaly Bids
anomaly_observations <- reactive({
anomaly_observations <- model_df() %>%
time_decompose(Price) %>%
anomalize(remainder) %>%
time_recompose() %>%
filter(anomaly == 'Yes') %>%
select(date, observed, anomaly) %>%
as.data.frame()
names(anomaly_observations)[1] <- "Bid Opening Date"
names(anomaly_observations)[2] <- "Bid Price"
anomaly_observations
})
# Bid Dataframe
bid_df <- reactive({
bid_df <- select(df(), `Bid Title`, `Bid Item`, `Bidder Name`, `Contact Name`,
`Bid Opening Date`, `Bid Price`)
bid_df
})
# Bid dataframe with anomaly observations
full_anomaly_df <- reactive({
full_anomaly_df <- merge(anomaly_observations(), bid_df(), by = c("Bid Price"))
full_anomaly_df <- full_anomaly_df[order(full_anomaly_df$`Bid Opening Date.x`),]
full_anomaly_df <- full_anomaly_df %>% select(-`Bid Opening Date.y`)
names(full_anomaly_df)[2] <- "Bid Opening Date"
full_anomaly_df
})
output$table3 <- renderDT({
DT::datatable(full_anomaly_df(),
rownames = F,
options = list(pageLength = 2, scrollX = TRUE, info = FALSE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#1f77b4', 'color': '#fff'});",
"}")))
})
}