-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
184 lines (131 loc) · 4.78 KB
/
app.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
#---- batR ----
# a tool to analyse batting statistics
#---- libraries ----
library(shiny)
library(bslib)
library(cricketdata)
library(tidyverse)
library(DT)
library(shinyjs)
library(plotly)
library(fmsb)
#---- load data ----
mens_t20_data <- read_rds("data/mens_ball_by_ball_data.rds")
womens_t20_data <- read_rds("data/womens_ball_by_ball_data.rds")
# vector of tournaments for men and women
tournaments <- cricsheet_codes
mens_t20_tournaments <- tournaments |>
select(code) |>
filter(code %in% c("t20s", "it20s", "ipl", "apl", "bbl", "bpl", "cpl", "ctc", "ilt", "ipl", "ipt", "lpl", "msl", "ntb", "psl", "sat", "sma", "ssm")) |>
pull(code)
womens_t20_tournaments <- tournaments |>
select(code) |>
filter(code %in% c("t20s", "blz", "cec", "frb", "wbb", "wcl", "wsl", "wtc")) |>
pull(code)
#---- functions ----
# get ball by ball data of a player. Generates a list where each item is an innings
source("functions/find_bbb.R")
# two functions: one to filter out wides and put each ball in a column for an innings, one to apply that function to a list of innings and join the tibbles to create ball by ball data
source("functions/career_bbb.R")
# calculate mean runs scored and mean SR by ball faced
source("functions/career_mean_bbb.R")
# calculate mean runs scored and mean SR by ball faced broken down by tournament
source("functions/tournament_mean_bbb.R")
# create a tibble of all the innings a player has played
source("functions/innings_table.R")
# calculate balls per boundary
source("functions/balls_per_boundary.R")
# calculate dot ball %
source("functions/dot_ball_percentage.R")
# career summary table
source("functions/career_summary_table.R")
# spider plot
source("functions/spider_plot.R")
# metrics by tournament
source("functions/metrics_by_tournament.R")
# ball by ball data by phase of a player
source("functions/phase_bbb.R")
# metrics by phase
source("functions/metrics_by_phase.R")
# spider plot by phase
source("functions/spider_plot_by_phase.R")
# find initials
source("functions/find_initials.R")
#---- modules ----
source("modules/about.R")
source("modules/ball_by_ball_analysis.R")
source("modules/career_summary.R")
source("modules/find_player.R")
source("modules/select_player.R")
source("modules/stats_breakdown.R")
#---- UI ----
ui <- page_navbar(
title = "batR",
selected = "batR",
useShinyjs(),
theme = bs_theme(version = 5, bg = "#FBFFF1", fg = "#000000", primary = "#1A281F", secondary = "#FFA630", font_scale = 0.8),
nav_panel(
title = "batR",
value = "batR",
select_player_UI("select_player"),
hidden(div(id = "career_summary_UI", career_summary_UI("career_summary"))),
hidden(div(id = "stats_breakdown_UI", stats_breakdown_UI("stats_breakdown"))),
hidden(div(id = "ball_by_ball_analysis_UI", ball_by_ball_analysis_UI("ball_by_ball_analysis")))
),
nav_panel(
title = "Find Player",
value = "Find Player",
find_player_UI("find_player")
),
nav_panel(
title = "About",
value = "About",
about_UI("about")
)
)
#---- server ----
server <- function(input, output, session){
# get innings list for selected player ----
selected_player <- select_player_server("select_player", mens_t20_data, womens_t20_data)
# computations ----
# create ball by ball data
ball_by_ball_data <- reactive({
if(length(selected_player$innings_list()) > 0){
career_bbb(selected_player$innings_list(), with_progress = TRUE)
}
})
# mean runs scored and mean SR by ball faced
ball_by_ball_mean <- reactive({
career_mean_bbb(ball_by_ball_data())
})
# linear model for strike rate by ball number
model <- reactive({
lm(ball_by_ball_mean()$`mean SR` ~ ball_by_ball_mean()$ball)
})
# create tibble of all innings played by the player
player_innings <- reactive({
innings_table(ball_by_ball_data())
})
# career summary table
player_summary_table <- reactive({
career_summary_table(ball_by_ball_data(), player_innings(), model())
})
# show hidden UI ---
observe({
toggle(id = "career_summary_UI", condition = req(ball_by_ball_data()))
toggle(id = "stats_breakdown_UI", condition = req(ball_by_ball_data()))
toggle(id = "ball_by_ball_analysis_UI", condition = req(ball_by_ball_data()))
})
# render career summary stats ----
career_summary_server("career_summary", player_summary_table, player_innings)
# stats breakdowns ----
stats_breakdown_server("stats_breakdown", ball_by_ball_data, player_innings, selected_player)
# ball_by_ball_analysis ----
ball_by_ball_analysis_server("ball_by_ball_analysis", ball_by_ball_data, ball_by_ball_mean, model, selected_player, player_summary_table)
# find_player ----
find_player_server("find_player")
# about ----
about_server("about")
}
#---- app ----
shinyApp(ui, server)