-
Notifications
You must be signed in to change notification settings - Fork 12
/
Statsbomb_Shiny_Part1.R
298 lines (228 loc) · 13.9 KB
/
Statsbomb_Shiny_Part1.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
library(rjson)
#library(jsonlite) [you can try jsonlite::fromJSON(x,flatten=TRUE) to see what that does when reading in the JSON file]
library(data.table)
library(dplyr)
'%!in%' <- function(x,y)!('%in%'(x,y))
####Obtain Competitions####
#Read File from JSON into a list
competitions <- fromJSON(file="C:\\Users\\sgopaladesikan\\Desktop\\open-data-master\\data\\competitions.json")
#Convert List into a DataFrame
competitions.df <- data.frame(do.call(rbind,competitions),stringsAsFactors = FALSE)
####Obtain Matches####
match.files <- list.files(path="C:\\Users\\sgopaladesikan\\Desktop\\open-data-master\\data\\matches",
full.names = TRUE,recursive = TRUE)
matches.list <- list()
for(i in 1:length(match.files)){
match.temp <- fromJSON(file=match.files[i]) ##Loop through each file which contains all the matches for a given competition and season and obtain the necessary match information
matches <- lapply(match.temp, function(x) data.frame(t(unlist(x)),stringsAsFactors = FALSE))
matches.df <- rbindlist(matches,fill=TRUE) #we use rbindlist instead of do.call(rbind,) because of column mismatch
matches.list[[i]] <- matches.df #this assigns matches.df to the matches.list list that we initialized
}
all.matches.df <- data.frame(rbindlist(matches.list,fill=TRUE)) ###Combines all matches from all competitions into one dataframe
###we are going to remove a lot of columns to just make our dataset clean
columns.to.keep <- names(which(unlist(lapply(all.matches.df,function(x) length(which(is.na(x)))))==0))
all.matches.clean <- all.matches.df[,columns.to.keep] #this selects the columns by column name
all.matches.clean$match_week <- as.numeric(all.matches.clean$match_week) #convert some variables to numeric
all.matches.clean$home_score <- as.numeric(all.matches.clean$home_score)
all.matches.clean$away_score <- as.numeric(all.matches.clean$away_score)
####Obtain Events####
event.files <- list.files(path="C:\\Users\\sgopaladesikan\\Desktop\\open-data-master\\data\\events",
full.names = TRUE,recursive = TRUE)
event.list <- list()
for(i in 1:length(event.files)){
event.temp <- fromJSON(file=event.files[i])
#unique(unlist(lapply(event.temp,function(x) x$type$name))) | Let's us see the unique events that happen in a game
teamids <- c() #Get the unique teamids participating in a match
#obtain the index where we find the event that talks about Starting XI
starting.x11.index <- which(unlist(lapply(event.temp,function(x) x$type$name))=="Starting XI")
starting.x11.list <- list()
for(s in 1:2){
#we need to remove "jersey_number" from the starting XI because sometimes it is missing and causes recylcing of the matrix
jersey.number.index <- which(names(unlist(event.temp[[s]]$tactics$lineup))=="jersey_number")
starting.x11 <- t(unlist(event.temp[[s]]$tactics$lineup)[-which(names(unlist(event.temp[[s]]$tactics$lineup))=="jersey_number")])
starting.x11.team1 <- data.frame(matrix(starting.x11,ncol=4,byrow = TRUE),stringsAsFactors = FALSE)
colnames(starting.x11.team1) <- names(unlist(event.temp[[s]]$tactics$lineup)[-which(names(unlist(event.temp[[s]]$tactics$lineup))=="jersey_number")])[1:4]
starting.x11.team1$formation <- event.temp[[s]]$tactics$formation
starting.x11.team1$team_id <- event.temp[[s]]$team$id
teamids <- c(teamids,event.temp[[s]]$team$id)
starting.x11.team1$team_name <- event.temp[[s]]$team$name
starting.x11.list[[s]] <- starting.x11.team1
}
pass.index <- which(unlist(lapply(event.temp,function(x) x$type$name))=="Pass")
#obtain the passes just for team1 (the first element in teamids)
pass.team1 <- pass.index[which(unlist(lapply(pass.index,function(x) event.temp[[x]]$team$id))==teamids[1])]
pass.team1.df <- data.frame(matrix(NA,nrow=1,ncol=12))
colnames(pass.team1.df) <- c("passid","Possession","Passer","X.Pass","Y.Pass",
"Pass.Type","Receiver","X.Receive","Y.Receive",
"Pass.Length","Pass.Angle","Body.Part")
for(p in 1:length(pass.team1)){
pass.temp <- event.temp[[pass.team1[p]]]
pass.id <- pass.temp$id
possession <- pass.temp$possession
passer <- pass.temp$player$name
pass.location <- pass.temp$location
pass.type <- pass.temp$pass$height$name
receiver <- ifelse("recipient" %!in% names(pass.temp$pass),NA,pass.temp$pass$recipient$name)
receive.location <- pass.temp$pass$end_location
pass.length <- pass.temp$pass$length
pass.angle <- pass.temp$pass$angle
body.part <- pass.temp$pass$body_part$name
row.toadd <- c(pass.id,possession,passer,pass.location,pass.type,receiver,receive.location,pass.length,pass.angle,body.part)
pass.team1.df <- rbind(pass.team1.df,row.toadd)
}
pass.team1.df <- pass.team1.df[-1,]
pass.team1.df[,c(2,4,5,8:11)] <- lapply(pass.team1.df[,c(2,4,5,8:11)],as.numeric)
pass.team1.df <- pass.team1.df %>% group_by(Possession) %>% mutate(seq = row_number())
pass.team1.df$team_id <- teamids[1]
pass.team2 <- pass.index[which(unlist(lapply(pass.index,function(x) event.temp[[x]]$team$id))==teamids[2])]
pass.team2.df <- data.frame(matrix(NA,nrow=1,ncol=12))
colnames(pass.team2.df) <- c("passid","Possession","Passer","X.Pass","Y.Pass",
"Pass.Type","Receiver","X.Receive","Y.Receive",
"Pass.Length","Pass.Angle","Body.Part")
for(p in 1:length(pass.team2)){
pass.temp <- event.temp[[pass.team2[p]]]
pass.id <- pass.temp$id
possession <- pass.temp$possession
passer <- pass.temp$player$name
pass.location <- pass.temp$location
pass.type <- pass.temp$pass$height$name
receiver <- ifelse("recipient" %!in% names(pass.temp$pass),NA,pass.temp$pass$recipient$name)
receive.location <- pass.temp$pass$end_location
pass.length <- pass.temp$pass$length
pass.angle <- pass.temp$pass$angle
body.part <- pass.temp$pass$body_part$name
row.toadd <- c(pass.id, possession,passer,pass.location,pass.type,receiver,receive.location,pass.length,pass.angle,body.part)
pass.team2.df <- rbind(pass.team2.df,row.toadd)
}
pass.team2.df <- pass.team2.df[-1,]
pass.team2.df[,c(2,4,5,8:11)] <- lapply(pass.team2.df[,c(2,4,5,8:11)],as.numeric)
pass.team2.df <- pass.team2.df %>% group_by(Possession) %>% mutate(seq = row_number())
pass.team2.df$team_id <- teamids[2]
pass.list <- list(pass.team1.df,pass.team2.df)
shot.index <- which(unlist(lapply(event.temp,function(x) x$type$name))=="Shot")
#obtain the shots just for team1 (the first element in teamids)
shots.team1 <- shot.index[which(unlist(lapply(shot.index,function(x) event.temp[[x]]$team$id))==teamids[1])]
shots.team1.df <- data.frame(matrix(NA,nrow=1,ncol=11))
colnames(shots.team1.df) <- c("Possession","Player","X.Shot","Y.Shot",
"Shot.Type","xG","keypassid","X.KeyPass","Y.KeyPass","Shot.Outcome","Shot.Foot")
if(length(shots.team1)!=0){
for(p in 1:length(shots.team1)){
shots.temp <- event.temp[[shots.team1[p]]]
possession <- shots.temp$possession
shooter <- shots.temp$player$name
shots.location <- shots.temp$location
shots.type <- shots.temp$shot$technique$name
shots.xg <- ifelse("statsbomb_xg" %!in% names(shots.temp$shot),NA, shots.temp$shot$statsbomb_xg)
keypass <- ifelse("key_pass_id" %!in% names(shots.temp$shot),NA,shots.temp$shot$key_pass_id)
keypass.location <- if(!is.na(keypass)){
as.vector(unlist(pass.team1.df[which(pass.team1.df$passid==keypass),c("X.Pass","Y.Pass")]))
}else{
c(NA,NA)
}
shots.outcome <- shots.temp$shot$outcome$name
body.part <- shots.temp$shot$body_part$name
row.toadd <- c(possession,shooter,shots.location,shots.type,shots.xg,keypass,keypass.location[1],keypass.location[2],shots.outcome,body.part)
shots.team1.df <- rbind(shots.team1.df,row.toadd)
}
shots.team1.df <- shots.team1.df[-1,]
shots.team1.df[,c(1,3,4,6,8,9)] <- lapply(shots.team1.df[,c(1,3,4,6,8,9)],as.numeric)
shots.team1.df$team_id <- teamids[1]
}
shots.team2 <- shot.index[which(unlist(lapply(shot.index,function(x) event.temp[[x]]$team$id))==teamids[2])]
shots.team2.df <- data.frame(matrix(NA,nrow=1,ncol=11))
colnames(shots.team2.df) <- c("Possession","Player","X.Shot","Y.Shot",
"Shot.Type","xG","keypassid","X.KeyPass","Y.KeyPass","Shot.Outcome","Shot.Foot")
if(length(shots.team2)!=0){
for(p in 1:length(shots.team2)){
shots.temp <- event.temp[[shots.team2[p]]]
possession <- shots.temp$possession
shooter <- shots.temp$player$name
shots.location <- shots.temp$location
shots.type <- shots.temp$shot$technique$name
shots.xg <- ifelse("statsbomb_xg" %!in% names(shots.temp$shot),NA, shots.temp$shot$statsbomb_xg)
keypass <- ifelse("key_pass_id" %!in% names(shots.temp$shot),NA,shots.temp$shot$key_pass_id)
keypass.location <- if(!is.na(keypass)){
as.vector(unlist(pass.team1.df[which(pass.team1.df$passid==keypass),c("X.Pass","Y.Pass")]))
}else{
c(NA,NA)
}
shots.outcome <- shots.temp$shot$outcome$name
body.part <- shots.temp$shot$body_part$name
row.toadd <- c(possession,shooter,shots.location,shots.type,shots.xg,keypass,keypass.location[1],keypass.location[2],shots.outcome,body.part)
shots.team2.df <- rbind(shots.team2.df,row.toadd)
}
shots.team2.df <- shots.team2.df[-1,]
shots.team2.df[,c(1,3,4,6,8,9)] <- lapply(shots.team2.df[,c(1,3,4,6,8,9)],as.numeric)
shots.team2.df$team_id <- teamids[2]
}
shot.list <- list(shots.team1.df,shots.team2.df)
match.id <- strsplit(basename(event.files[i]),"[.]")[[1]][1]
event.list[[match.id]] <- list(starting.x11.list,pass.list,shot.list)
}
##########Create a Shiny Application That Uses Shot Information##############
#Already I have found some mistakes in pulling in shot information, can you find them?
#We want to create a dataframe of all shots with meta-information (e.g. competition, season, pass location, shot location, shooter)
shots <- lapply(event.list,function(x) x[[3]]) #we want to just take the shots information from the event.list list
shots.df <- plyr::ldply(lapply(shots,function(x) rbindlist(x,fill=TRUE)),.id="match_id") #we want to combine both teams' shots within a game into a datamframe, and then combine all matches together using match_id as a new column
shots.df <- shots.df %>% group_by(match_id,team_id) %>% mutate(xGoal_Total = sum(xG)) #we are adding the sum of all shots' xG to give each team a xG_Total (how many goals were they expected to score given their shot quality)
#This is merging together the shots with the matches to get match information
shots.shiny <- merge(shots.df,all.matches.df[,c("match_id","competition.competition_name","season.season_name",
"home_score","away_score","home_team.home_team_id","away_team.away_team_id",
"home_team.home_team_name","away_team.away_team_name")],by="match_id") #some matches are missing
#This is calculating the TeamScore for the team_id column so that I can use this in calculating the difference between actual goals and expected goals for an entire match for a team
shots.shiny$TeamScore <- ifelse(shots.shiny$team_id==shots.shiny$home_team.home_team_id,
as.numeric(shots.shiny$home_score),
as.numeric(shots.shiny$away_score))
#This is the calculation for calculating the difference between actual and expected
shots.shiny$xG_Diff <- shots.shiny$TeamScore - shots.shiny$xGoal_Total
#The next three lines just gives me the an additional column to the shots dataframe to let me know which team took the shot
teams.df <- unique(all.matches.df[,c("home_team.home_team_id","home_team.home_team_name")])
shots.shiny.final <- merge(shots.shiny,teams.df,by.x="team_id",by.y="home_team.home_team_id")
colnames(shots.shiny.final)[25] <- "team_name"
#Adding a column to let me know if a shot was assisted or not
shots.shiny.final$isAssisted <- ifelse(is.na(shots.shiny.final$keypassid),FALSE,TRUE)
############Create a Simple Shiny Application#############################
source("your_file_path\\Draw_Pitch.R") #load in hori5, which contains the soccer field
#install.packages("shiny")
library(shiny)
#runExample()
#runExample("01_hello")
# Define UI for app that draws a shot map ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of shots ----
sliderInput(inputId = "shots",
label = "Number of shots:",
min = 1,
max = nrow(shots.shiny.final),
value = nrow(shots.shiny.final))
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Shot Map ----
plotOutput(outputId = "shotPlot")
)
)
)
# Define server logic required to draw a shot map ----
server <- function(input, output) {
# Shot Map of Statsbomb Data----
# with requested number of shots
# This expression that generates a shot map is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$shots) change
# 2. Its output type is a plot
output$shotPlot <- renderPlot({
x <- sample(1:nrow(shots.shiny.final),input$shots)
hori5 + geom_point(data=shots.shiny.final[x,], aes(x=X.Shot,y=Y.Shot,color=xG))
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)