-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.R
257 lines (220 loc) · 8.12 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
#Logic
library(shiny)
library(shinyjs)
library(readxl)
library(dplyr)
library(gtools)
library(pbapply)
library(ggplot2)
library(shinydashboard)
library(gridExtra)
library(plotly)
source("./utilities.R")
#Code outside the server function run once per R session
#Everything inside server function is different for each single user
palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
#here we assemble inputs and outputs
server <- function(input, output, session) {
#what is evaluated inside reactive is passed to each caller at the same time.
#we access the same outcome from different render function
perms = NULL
weeks = NULL
goals = NULL
predicted = NULL
actual = NULL
hide('loading')
hide('advancedOptions')
hide('teamsPanel')
selectedData <- reactive({
iris[, c(input$xcol, input$ycol)]
})
observeEvent(input$teams, {
updateTextInput(session,"currPos",value=as.character(actual[input$teams]))
updateTextInput(session,"expePos",value=as.character(predicted[input$teams]))
if(input$expePos!=""){
color = ifelse(actual[input$teams]<=predicted[input$teams],"DarkSeaGreen","LightCoral")
runjs(paste0("document.getElementById('expePos').style.backgroundColor = '", color ,"'"))
}
})
observeEvent(input$nrep, {
if(input$nrep>1000){
showNotification("ATTENZIONE! Un numero alto di simulazioni fornisce stime attendibili ma richiede molto tempo per essere calcolato. Scegli un numero basso oppure...mettiti comodo!", type = "warning")
}
})
observeEvent(input$advanced, {
if(input$advanced) show('advancedOptions')
else hide('advancedOptions')
})
observeEvent(input$quickrun, {
updateSliderInput(session,"nrep",value=100)
})
observeEvent(input$runFile2, {
showModal(modalDialog(
title = "Scarica file calendario",
p("Scarri la pagina e cerca il pulsante",img(src="button.png",width="18%"), "per scaricare il calendario della tua lega."),
tags$iframe(src=paste0("https://leghe.fantacalcio.it/",tolower(gsub(" ","-",input$file2)),"/calendario"), height=400, width=550)
))
})
observeEvent(input$file1, {
req(input$file1)
data = read_excel(input$file1$datapath)
weeks <<- parse.weeks(data)
names = team.names(weeks)
goals <<- calculateGoals(weeks)
actual <<-rank(-calculateFinal(goals,getActualRounds(weeks)),ties.method = "min")
withProgress(message = 'Loading', value = 0, {
perms <<- getPerms(names)
})
updateSliderInput(session,"nrep",max = nrow(perms))
updateSelectInput(session, "teams", choices = names)
})
#' Hide the loading splashscreen
#'
#' @param modal the splashscreen to hide is on a modal
#' @param query the splashscreen to hide is on a query panel
hideLoading = function(){
hideElement(id = 'loading')
}
#' Show the loading splashscreen
#'
#' @param modal show the loading splashscreen on a modal
#' @param query show the loading splashscreen on the query panel
showLoading = function(){
showElement(id = 'loading')
}
observeEvent(input$run, {
showLoading()
withProgress(message = 'Loading', value = 0, {
simul <- pbapply(perms[sample(1:nrow(perms), input$nrep, replace=FALSE),],1,function(row){
rounds = getRounds(row,length(weeks))
incProgress(1/input$nrep, detail = paste("Simulazione campionati..."))
return(rank(-calculateFinal(goals,rounds),ties.method = "min"))
})
})
predicted <<- rank(apply(simul, 1, mean),ties.method = "min")
updateTextInput(session,"expePos",value=as.character(predicted[input$teams]))
withProgress(message = 'Carico grafici...', value = 0, {
piazz = sort((rowSums(simul)))
podiums = na.replace(t(apply(simul,1,function(x){table(x)[c("1","2","3")]})),0)
strlist = c()
for(i in 1:nrow(podiums)){
strlist = c(strlist, paste0("team:'",row.names(podiums)[i],"', First:",podiums[i,1],
", Second:",podiums[i,2],", Third:",podiums[i,3],collapse = ""))
}
s= paste0("{",strlist,"},",collapse = "")
s= substr(s,1,nchar(s)-1)
runjs(paste0("
$('#vizStacked').empty();
new roughViz.StackedBar(
{
element: '#vizStacked',
data: [",s,"],
labels: 'team',
title: 'Piazzamenti Sul Podio',
width: window.innerWidth / 3,
strokeWidth: 2,
roughness: 3,
fillStyle: 'zigzag',
fillWeight: 1.5,
titleFontSize: '3rem',
tooltipFontSize: '2rem',
axisFontSize: '2rem'
}
);
"))
mean = apply(simul, 1, mean)
sd = apply(simul, 1, sd)
names = names(sd)
df = data.frame(names,mean,sd)
df$actual = actual[names]
runjs(paste0("document.getElementById('introContainer').style.display = 'none'"))
t= paste0("'",df$names,"',",collapse = "")
t= substr(t,1,nchar(t)-1)
v= paste0(df$mean,",",collapse = "")
v= substr(v,1,nchar(v)-1)
runjs(paste0("
$('#vizBar').empty();
new roughViz.Bar(
{
element: '#vizBar',
data: {
labels: [",t,"],
values: [",v,"]
},
title: 'Posizionamento Medio',
width: window.innerWidth/1.5,
roughness: 3,
stroke: 'skyblue',
strokeWidth: 2,
fillStyle: 'zigzag',
fillWeight: 1.5,
titleFontSize: '3rem',
tooltipFontSize: '2rem',
axisFontSize: '2rem'
}
);
"))
output$plot <- renderPlotly({
plot_ly(df, x = ~names, y = ~mean, type = 'bar', name = 'Expected')%>%
add_trace(y = ~actual, name = 'Actual') %>%
layout(yaxis = list(title = 'Posizionamento'),xaxis = list(title = 'Squadre'), barmode = 'group',
plot_bgcolor = "rgba(0, 0, 0, 0)", paper_bgcolor = "rgba(0, 0, 0, 0)")
})
incProgress(0.5, detail = 'Carico grafici...')
#percentuale di vittorie
position = 1
count = table(names(which(simul==position,arr.ind = TRUE)[,1]))
labels = paste(names(count), "\n", (count/input$nrep)*100,"%", sep="")
df <- data.frame(count)
names(df) <- c("teams","freq")
df$labels = paste(round((df$freq/input$nrep)*100,1),"%",sep = "")
df <- df %>%
arrange(desc(teams)) %>%
mutate(lab.ypos = cumsum(freq) - 0.5*freq)
df <- df %>%
mutate(end = 2 * pi * cumsum(freq)/sum(freq),
start = lag(end, default = 0),
middle = 0.5 * (start + end),
hjust = ifelse(middle > pi, 1, 0),
vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 0, 1))
incProgress(0.25, detail = 'Carico grafici...')
t= paste0("'",df$teams,"',",collapse = "")
t= substr(t,1,nchar(t)-1)
v= paste0(df$freq,",",collapse = "")
v= substr(v,1,nchar(v)-1)
runjs(paste0("
$('#vizPie').empty();
new roughViz.Pie(
{
element: '#vizPie',
data: {
labels: [",t,"],
values: [",v,"]
},
title: 'Vittorie Campionato',
width: window.innerWidth / 3,
strokeWidth: 2,
roughness: 3,
fillStyle: 'zigzag',
fillWeight: 1.5,
titleFontSize: '3rem',
tooltipFontSize: '2rem'
}
);
"))
output$plot2 <- renderPlotly({
plot_ly(df, labels = ~teams, values = ~freq, type = 'pie') %>%
layout(title = '',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
plot_bgcolor = "rgba(0, 0, 0, 0)", paper_bgcolor = "rgba(0, 0, 0, 0)")
})
incProgress(0.25, detail = 'Carico grafici...')
})
color = ifelse(input$currPos<=input$expePos,"DarkSeaGreen","LightCoral")
runjs(paste0("document.getElementById('expePos').style.backgroundColor = '", color ,"'"))
show('teamsPanel')
hideLoading()
})
}