-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.R
538 lines (464 loc) · 13.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
library(shiny)
# static model values - outside function, visible across all sessions
s1_fare_elas <- -0.4
s1_mile_elas <- 0.4
s2_fare_elas <- -0.2
s2_mile_elas <- 0.6
passenger_loss_rate <- .01
inflation_rate <- .01
tax_rate <- 1
interest_rate <- 0.05
bankrupt_level <- -50
server <- function(input, output) {
# Define reactive values
values <- reactiveValues()
# company level reactive values
values$year <- 0
values$id <- 1
values$unitcost <- 1
values$fixedcost <- 50
values$userfeedback <-
"Costs and revenues currently balance exactly. Set your desired change in fare and mileage for each service next year and click the 'Simulate next year' button. Good luck!"
values$bankrupt <- "no"
# define company dataframe
values$cdf <- data.frame(
id = as.integer(1),
year = as.integer(0),
total_revenue = 250,
fixed_costs = 50,
unit_cost = 1,
total_costs = 250,
interest_paid = 0,
gross_profit = 0,
tax = 0,
net_profit = 0,
interest_due = 0,
tax_todate = 0,
interest_todate = 0,
balance = 0,
stringsAsFactors = FALSE
)
# define service 1 dataframe
values$s1df <- data.frame(
id = as.integer(1),
year = as.integer(0),
fare_increase = 0,
mileage_increase = 0,
miles = 100,
fare = 1,
passengers = 150,
variable_cost = 100,
revenue = 150
)
# define service 2 dataframe
values$s2df <- data.frame(
id = as.integer(1),
year = as.integer(0),
fare_increase = 0,
mileage_increase = 0,
miles = 100,
fare = 1,
passengers = 100,
variable_cost = 100,
revenue = 100
)
# observe event - respond to simulate button
observeEvent(input$simulate, {
if (input$simulate > 0 & values$bankrupt == "no") {
# increase unit cost by inflation
values$unitcost <-
(values$unitcost + (inflation_rate * values$unitcost))
# increase fixed cost by inflation
values$fixedcost <-
(values$fixedcost + (inflation_rate * values$fixedcost))
# calculate service 1 values
s1_prior_miles <- values$s1df$miles[values$id]
s1_prior_fare <- values$s1df$fare[values$id]
s1_prior_passengers <- values$s1df$passengers[values$id]
s1_prior_variable_cost <- values$s1df$variable_cost[values$id]
s1_new_miles <-
s1_prior_miles * (1 + (input$s1_mileage_increase / 100))
s1_new_fare <-
s1_prior_fare * (1 + (input$s1_fare_increase / 100))
s1_new_passengers <-
(s1_prior_passengers - (passenger_loss_rate * s1_prior_passengers)) * ((s1_new_fare / s1_prior_fare) ^
s1_fare_elas) * ((s1_new_miles / s1_prior_miles) ^ s1_mile_elas)
s1_new_variable_cost <- values$unitcost * s1_new_miles
s1_revenue <- s1_new_fare * s1_new_passengers
# calculate service 2 values
s2_prior_miles <- values$s2df$miles[values$id]
s2_prior_fare <- values$s2df$fare[values$id]
s2_prior_passengers <- values$s2df$passengers[values$id]
s2_prior_variable_cost <- values$s2df$variable_cost[values$id]
s2_new_miles <-
s2_prior_miles * (1 + (input$s2_mileage_increase / 100))
s2_new_fare <-
s2_prior_fare * (1 + (input$s2_fare_increase / 100))
s2_new_passengers <-
(s2_prior_passengers - (passenger_loss_rate * s2_prior_passengers)) * ((s2_new_fare / s2_prior_fare) ^
s2_fare_elas) * ((s2_new_miles / s2_prior_miles) ^ s2_mile_elas)
s2_new_variable_cost <- values$unitcost * s2_new_miles
s2_revenue <- s2_new_fare * s2_new_passengers
# calculate company values
prior_interest_due <- values$cdf$interest_due[values$id]
prior_tax_todate <- values$cdf$tax_todate[values$id]
prior_interest_todate <- values$cdf$interest_todate[values$id]
prior_balance <- values$cdf$balance[values$id]
revenue <- s1_revenue + s2_revenue
cost <-
s1_new_variable_cost + s2_new_variable_cost + values$fixedcost
grprofit <- revenue - cost - prior_interest_due
if (grprofit > 10) {
tax <- (grprofit - 10) * tax_rate
} else {
tax <- 0
}
if (grprofit < 0) {
new_interest_due <- abs(grprofit * interest_rate)
} else {
new_interest_due <- 0
}
netprofit <- grprofit - tax
new_tax_todate <- prior_tax_todate + tax
new_interest_todate <-
prior_interest_todate + prior_interest_due
new_balance <- prior_balance + netprofit
# check for bankruptcy
if (new_balance < bankrupt_level) {
values$bankrupt <- "yes"
}
# set user feedback strings
if (values$bankrupt == "yes") {
values$userfeedback <-
"You have gone BANKRUPT! Better luck next time ..."
} else {
if (netprofit < 0) {
values$userfeedback <-
"You have made a LOSS this year! You will pay interest on borrowings next year."
}
if (netprofit > 0 & tax == 0) {
values$userfeedback <- "You have made a PROFIT this year!"
}
if (netprofit > 0 & tax > 0) {
values$userfeedback <-
"You have made EXCESSIVE PROFITS this year and have been taxed!"
}
}
# Increment id and year
values$year = values$year + 1
values$id = values$id + 1
# Update dataframes
# Update s1 df
newLine <- c(
values$id,
values$year,
input$s1_fare_increase,
input$s1_mileage_increase,
s1_new_miles,
s1_new_fare,
s1_new_passengers,
s1_new_variable_cost,
s1_revenue
)
values$s1df[nrow(values$s1df) + 1, ] <- newLine
values$s1df$id <- as.integer(values$s1df$id)
values$s1df$year <- as.integer(values$s1df$year)
# Update s2 df
newLine <- c(
values$id,
values$year,
input$s2_fare_increase,
input$s2_mileage_increase,
s2_new_miles,
s2_new_fare,
s2_new_passengers,
s2_new_variable_cost,
s2_revenue
)
values$s2df[nrow(values$s2df) + 1, ] <- newLine
values$s2df$id <- as.integer(values$s2df$id)
values$s2df$year <- as.integer(values$s2df$year)
# Update company df
newLine <- c(
values$id,
values$year,
revenue,
values$fixedcost,
values$unitcost,
cost,
prior_interest_due,
grprofit,
tax,
netprofit,
new_interest_due,
new_tax_todate,
new_interest_todate,
new_balance
)
values$cdf[nrow(values$cdf) + 1, ] <- newLine
values$cdf$id <- as.integer(values$cdf$id)
values$cdf$year <- as.integer(values$cdf$year)
}
})
# define outputs
# passengers plot
output$passengers <- renderPlot({
plot(
values$s1df$year,
values$s1df$passengers,
type = 'o',
xaxt = "n",
ylim = c(0, max(
values$s1df$passengers + values$s2df$passengers
)),
xlim = c(0, max(values$s1df$year)),
xlab = "Year",
ylab = "Number",
main = "Passengers",
col = '#4d4dff',
lwd = 1,
pch = 3
)
axis(side = 1, at = c(0:max(values$s1df$year)))
lines(
values$s1df$year,
values$s2df$passengers,
type = "o",
col = "#ff4d4d",
lwd = 1,
pch = 4
)
lines(
values$s1df$year,
(values$s1df$passengers + values$s2df$passengers),
type = "o",
col = "black",
lwd = 1,
pch = 1
)
legend(
"bottomleft",
bty = "n",
c("s1", "s2", "co"),
lty = c(1, 1, 1),
lwd = c(1, 1, 1),
pch = c(3, 4, 1),
col = c("#4d4dff", "#ff4d4d", "black")
)
})
# miles plot
output$miles <- renderPlot({
plot(
values$s1df$year,
values$s1df$miles,
type = 'o',
xaxt = "n",
ylim = c(0, max(
values$s1df$miles + values$s2df$miles
)),
xlim = c(0, max(values$s1df$year)),
xlab = "Year",
ylab = "Number",
main = "Miles",
col = '#4d4dff',
lwd = 1,
pch = 3
)
axis(side = 1, at = c(0:max(values$s1df$year)))
lines(
values$s1df$year,
values$s2df$miles,
type = "o",
col = "#ff4d4d",
lwd = 1,
pch = 4
)
lines(
values$s1df$year,
(values$s1df$miles + values$s2df$miles),
type = "o",
col = "black",
lwd = 1,
pch = 1
)
legend(
"bottomleft",
bty = "n",
c("s1", "s2", "co"),
lty = c(1, 1, 1),
lwd = c(1, 1, 1),
pch = c(3, 4, 1),
col = c("#4d4dff", "#ff4d4d", "black")
)
})
# revenue plot
output$revenue <- renderPlot({
plot(
values$s1df$year,
values$s1df$revenue,
type = 'o',
xaxt = "n",
ylim = c(0, max(
values$s1df$revenue + values$s2df$revenue
)),
xlim = c(0, max(values$s1df$year)),
xlab = "Year",
ylab = "Amount",
main = "Revenue",
col = '#4d4dff',
lwd = 1,
pch = 3
)
axis(side = 1, at = c(0:max(values$s1df$year)))
lines(
values$s1df$year,
values$s2df$revenue,
type = "o",
col = "#ff4d4d",
lwd = 1,
pch = 4
)
lines(
values$s1df$year,
(values$s1df$revenue + values$s2df$revenue),
type = "o",
col = "black",
lwd = 1,
pch = 1
)
legend(
"bottomleft",
bty = "n",
c("s1", "s2", "co"),
lty = c(1, 1, 1),
lwd = c(1, 1, 1),
pch = c(3, 4, 1),
col = c("#4d4dff", "#ff4d4d", "black")
)
})
# costs plot
output$costs <- renderPlot({
plot(
values$s1df$year,
values$s1df$variable_cost,
type = 'o',
xaxt = "n",
ylim = c(0, max(values$cdf$total_costs)),
xlim = c(0, max(values$s1df$year)),
xlab = "Year",
ylab = "Amount",
main = "Costs",
col = '#4d4dff',
lwd = 1,
pch = 3
)
axis(side = 1, at = c(0:max(values$s1df$year)))
lines(
values$s1df$year,
values$s2df$variable_cost,
type = "o",
col = "#ff4d4d",
lwd = 1,
pch = 4
)
lines(
values$s1df$year,
(values$cdf$total_costs),
type = "o",
col = "black",
lwd = 1,
pch = 1
)
legend(
"bottomleft",
bty = "n",
c("s1", "s2", "co"),
lty = c(1, 1, 1),
lwd = c(1, 1, 1),
pch = c(3, 4, 1),
col = c("#4d4dff", "#ff4d4d", "black")
)
})
# net profit plot
output$profit <- renderPlot({
plot(
values$cdf$year,
values$cdf$net_profit,
type = 'o',
xaxt = "n",
ylim = c(min(values$cdf$net_profit), max(values$cdf$net_profit)),
xlim = c(0, max(values$cdf$year)),
xlab = "Year",
ylab = "Amount",
main = "Group net profit",
col = 'black',
lwd = 1,
pch = 1
)
axis(side = 1, at = c(0:max(values$s1df$year)))
})
# balance plot
output$cbalance <- renderPlot({
plot(
values$cdf$year,
values$cdf$balance,
type = 'o',
xaxt = "n",
ylim = c(min(values$cdf$balance), max(values$cdf$balance)),
xlim = c(0, max(values$cdf$year)),
xlab = "Year",
ylab = "Amount",
main = "Group balance (to date)",
col = 'black',
lwd = 1,
pch = 1
)
axis(side = 1, at = c(0:max(values$cdf$year)))
})
# Data table output and download function
output$c_data_out <-
renderTable(values$cdf[, c(2:14)], striped = TRUE, spacing = "xs")
output$downloadData <- downloadHandler(
filename = function() {
paste('dominobus', 'csv', sep = ".")
},
content = function(file) {
# Write to a file specified by the 'file' argument
write.csv(values$cdf[, c(2:14)], file,
row.names = FALSE)
}
)
# Dashboard output
output$year <- renderText(paste({
values$year
}))
output$grossprofit <-
renderText(paste({
round(values$cdf$gross_profit[values$id], 2)
}))
output$netprofit <-
renderText(paste({
round(values$cdf$net_profit[values$id], 2)
}))
output$taxpaid <-
renderText(paste({
round(values$cdf$tax[values$id], 2)
}))
output$interestdue <-
renderText(paste({
round(values$cdf$interest_due[values$id], 2)
}))
output$balance <-
renderText(paste({
round(values$cdf$balance[values$id], 2)
}))
output$feedback <- renderText(paste({
values$userfeedback
}))
# Note this is used with conditional panel to deactivate simulation button once bankrupt
output$bankrupt <- renderText(paste({
values$bankrupt
}))
# This line is needed as output$bankrupt is not shown on the app, to prevent it being suspended
outputOptions(output, "bankrupt", suspendWhenHidden = FALSE)
}