-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
71 lines (58 loc) · 3.08 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
#library(datasets)
library(shiny)
library(dplyr)
library(magrittr)
library(lubridate)
require(ggplot2)
shinyServer(function(input, output) {
addClass(selector = "Sidebar", class = "sidebar-collapse")
load("RforAp.RData") # This is a dataset where every time point in a semester has been predicted
autoInvalidate = reactiveTimer(60000)
# Needs to update the test dataframe for the following variables
Pred_data$Semester = ifelse(Pred_data$Month > 6, 1, 2)
# Creates predictions and adds a timestamp
filtered_test = reactive({
test1 = filter(Pred_data, term_week==input$Week)
test2 = filter(test1, Day == input$Day)
timestamp = paste(test2$Hour,":", test2$Minute,":00",sep="")
df= cbind.data.frame(wait_time=test2$wait_time, timestamp, number=1:length(test2$wait_time))
})
# Prints the current estimated waiting time and refreshes
output$text1 <- renderPrint({
autoInvalidate() # Needed for loops that refresh after set time
Hour_p = Sys.time() %>% hour()
Day_p = Sys.time() %>% weekdays()
Min_p = Sys.time() %>% minute()
# Here
# Filters stored test data so that it matches current time
test_p = filter(Pred_data, Hour == Hour_p & Day == Day_p & Minute == Min_p, term_week==ifelse((Sys.time() %>% week())>36,
(Sys.time() %>% week())-36, 0))
# Weird hours where MSC shuts early
if(nrow(test_p)==0){
cat("The MSC is closed currently. It opens weekdays at 10am.")
}else if(test_p$term_week == 2){
time_now = round(test_p$wait_time, 0)
cat("Estimated waiting time is", time_now, "minutes. *Please check timetable as some hours don't start until week 3.")
}else{
time_now = round(test_p$wait_time, 0)
cat("Estimated waiting time is", time_now, "minutes.")
}
})
output$hist <- renderPlot({
df= filtered_test()
max_p = max(df[,1])
ggplot(df, aes(x = timestamp, y = as.numeric(wait_time))) + geom_point(size = 0.5, col="white") +
stat_smooth(aes(x=number, y=wait_time), method="loess",span=0.1, se = FALSE) +
scale_x_discrete(breaks = levels(df$timestamp)[c(T, rep(F, 59))]) +theme_bw()+
labs(x="Time of Day", y="Waiting\nTime in\nMinutes") +
xlab( paste("Time for ", input$Day, " in Term Week ", input$Week, sep="") )+
scale_y_continuous(breaks=seq(0, 60, 5))+ expand_limits(y=c(0,max_p+5))+
theme(
plot.title = element_text(lineheight=0, color='black', face="bold", size=14),
axis.title.x=element_text(angle=0, color='black', face='bold', size=14),
axis.title.y=element_text(angle=0, vjust = 1, color='black',face='bold', size=14),
axis.text.x=element_text(angle=0, color='black', size=12),
axis.text.y=element_text(angle=0, color='black', size=13)
) + theme(legend.position = "none")
})
})