-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
180 lines (133 loc) · 4.48 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
library(shiny)
library(shinydashboard)
library(DT)
library(fplscrapR)
library(dplyr)
library(purrr)
library(tidyr)
library(ggplot2)
library(jsonlite)
# server
server <- function(input, output) {
# Basic gameweek info
gw_info <- get_round_info()
overall_average <- sum(gw_info$average_entry_score)
# Overall average box
output$overall_average <- renderInfoBox({
infoBox(
"Average team score", overall_average, icon = icon("bar-chart"),
color = "purple"
)})
# Entry id
entry_id <- reactive({
if (input$team_id != ""){
entry_id <- input$team_id
} else {
entry_id <- 72802
}
return(entry_id)
})
season_history <- reactive({
season_history <- get_entry_season(entryid = entry_id())|>
rename(id = event)
return(season_history)
})
# Total points calc
total_points <- reactive({
total_points <- sum(season_history()$points)
return(total_points)
})
# Total points box
output$your_points <- renderInfoBox({
infoBox("Your score", total_points(), icon = icon("futbol"), color = "purple")
})
# Total bench points calc
bench_points <- reactive({
bench_points <- sum(season_history()$points_on_bench)
return(bench_points)
})
# Total points box
output$bench_points <- renderInfoBox({
infoBox("Total points on bench", bench_points(),
icon = icon("chair"), color = "purple")
})
# Get captains info
source("get_captain_table.R")
captain_table <- reactive({
get_captain_table(entry_id(), season_history())
})
# Captains table
output$captain_comparison <- DT::renderDataTable(tibble(captain_table()),
options = list(scrollX = TRUE,
#autoWidth = TRUE,
paging = FALSE))
# Get transfers tibble
source("get_transfers.R")
transfers_table <- reactive({
transfers_table <- get_transfers(entry_id(), season_history())
return(transfers_table)
})
# Transfers table
output$gw_table <- DT::renderDataTable(tibble(transfers_table()),
options = list(scrollX = TRUE,
#autoWidth = TRUE,
paging = FALSE))
plot_data <- reactive({
field_points_data <- season_history() |>
select(id, points) |>
mutate(place = "field")
bench_points_data <- season_history() |>
select(id, points_on_bench) |>
rename(points = points_on_bench) |>
mutate(place = "bench")
plot_data <- bind_rows(field_points_data, bench_points_data)
return(plot_data)
})
#Plot points
output$points_plot <- renderPlot({
ggplot(plot_data(), aes(fill=place, y=points, x= as.factor(id))) +
geom_bar(position="dodge", stat="identity") +
theme_minimal()+
theme(panel.background = element_rect(fill = '#ECF0F5', color = '#ECF0F5'),
plot.background = element_rect(fill = "#ECF0F5", colour = "#ECF0F5")) +
xlab("Gameweek")
})
#Average team data
average_team_score <- gw_info |>
drop_na() |>
tibble() |>
mutate(total_points = cumsum(average_entry_score),
ind = "average_team") |>
select(id, total_points, ind) |>
rename(event = id)
# Average top 10 managers data
source("get_top10_data.R")
average_top_points <- get_top10_data()
# My_team_data
my_team_scores <- reactive({
my_team_scores <- season_history() |>
rename(event = id) |>
select(event, total_points) |>
mutate(ind = "my_score") |>
select(event, total_points, ind)
return(my_team_scores)
})
# Top 10 comparison data
top_10_plot_data <- reactive({
top_10_plot_data <- bind_rows(average_team_score,
average_top_points,
my_team_scores())
return(top_10_plot_data)
})
# Top 10 comparison plot
output$top10_plot <- renderPlot({
ggplot(top_10_plot_data(), aes(x = as.factor(event), y = total_points,
group = ind , color = ind)) +
geom_line(size = 2) +
scale_y_continuous(n.breaks = 12) +
theme_minimal()+
theme(panel.background = element_rect(fill = '#ECF0F5', color = '#ECF0F5'),
plot.background = element_rect(fill = "#ECF0F5", colour = "#ECF0F5")) +
xlab("Gameweek")
})
}