├── .Rbuildignore ├── .Rhistory ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── R ├── construct_arc.R ├── construct_court.R ├── data.R ├── extract_shots.R ├── plot_shotchart.R ├── scrape_raw_list.R └── shooting_stats.R ├── README.Rmd ├── README.md ├── README_files └── figure-markdown_github │ └── unnamed-chunk-2-1.png ├── data-raw └── shots.R ├── data └── shots.rda ├── man ├── construct_arc.Rd ├── construct_court.Rd ├── extract_shots.Rd ├── plot_shotchart.Rd ├── scrape_raw_list.Rd └── shots.Rd └── rfeb.Rproj /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^data-raw$ 2 | ^LICENSE\.md$ 3 | ^rfeb\.Rproj$ 4 | ^\.Rproj\.user$ 5 | -------------------------------------------------------------------------------- /.Rhistory: -------------------------------------------------------------------------------- 1 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 2 | # We need to create a variable identifying the team 3 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 4 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 5 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 6 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 7 | home_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[1]]) 8 | away_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[2]]) 9 | home_stats_dfs <- Map(cbind, home_stats_dfs, 10 | "team_name" = home_team_names, 11 | "team_id" = home_team_ids, 12 | "game_id" = game_ids) 13 | away_stats_dfs <- Map(cbind, away_stats_dfs, 14 | "team_name" = away_team_names, 15 | "team_id" = away_team_ids, 16 | "game_id" = game_ids) 17 | home_stats <- do.call("rbind", home_stats_dfs) 18 | away_stats <- do.call("rbind", away_stats_dfs) 19 | View(away_stats) 20 | stats <- rbind(home_stats, away_stats) %>% 21 | dplyr::mutate(player_name = name, 22 | player_no = as.factor(no), 23 | player_id = id, 24 | fg_made = as.numeric(fgm), 25 | fg_atp = as.numeric(fga), 26 | fg_pct = fg_made / fg_atp, 27 | p2_made = as.numeric(p2m), 28 | p2_atp = as.numeric(p2a), 29 | p2_pct = p2_made / p2_atp, 30 | p3_made = as.numeric(p3m), 31 | p3_atp = as.numeric(p3a), 32 | p3_pct = p3_made / p3_atp, 33 | points = as.numeric(pts)) 34 | View(stats) 35 | devtools::load_all(".") 36 | game_ids <- 2010208:2010216 37 | raw_list <- scrape_raw_list(game_ids) 38 | stats <- shooting_stats(raw_list, game_ids) 39 | View(stats) 40 | View(stats) 41 | devtools::load_all(".") 42 | game_ids <- 2010208:2010216 43 | raw_list <- scrape_raw_list(game_ids) 44 | stats <- shooting_stats(raw_list, game_ids) 45 | shots <- extract_shots(game_ids) 46 | View(stats) 47 | final_df <- dplyr::left_join(shots, stats, by = c("game_id", "player_no")) 48 | View(final_df) 49 | levels(shots$player_no) 50 | levels(stats$player_no) 51 | str(final_df) 52 | final_df <- dplyr::left_join(shots, stats, by = c("game_id", "player_no", 53 | "team_id")) 54 | final_df <- dplyr::left_join(shots, stats, by = c("game_id", "player_no", 55 | "team_name")) 56 | str(final_df) 57 | devtools::load_all(".") 58 | devtools::load_all(".") 59 | devtools::load_all(".") 60 | View(final_df) 61 | devtools::load_all(".") 62 | x <- extract_shots(game_ids) 63 | devtools::load_all(".") 64 | x <- extract_shots(game_ids) 65 | devtools::load_all(".") 66 | x <- extract_shots(game_ids) 67 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 68 | # We need to create a variable identifying the team 69 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 70 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 71 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 72 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 73 | home_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[1]]) 74 | away_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[2]]) 75 | home_stats_dfs <- Map(cbind, home_stats_dfs, 76 | "team_name" = home_team_names, 77 | "team_id" = home_team_ids, 78 | "game_id" = game_ids) 79 | away_stats_dfs <- Map(cbind, away_stats_dfs, 80 | "team_name" = away_team_names, 81 | "team_id" = away_team_ids, 82 | "game_id" = game_ids) 83 | home_stats <- do.call("rbind", home_stats_dfs) 84 | away_stats <- do.call("rbind", away_stats_dfs) 85 | stats <- rbind(home_stats, away_stats) %>% 86 | dplyr::transmute(player_name = name, 87 | player_no = no, 88 | player_id = id, 89 | fg_made = as.numeric(fgm), 90 | fg_atp = as.numeric(fga), 91 | fg_pct = fg_made / fg_atp, 92 | p2_made = as.numeric(p2m), 93 | p2_atp = as.numeric(p2a), 94 | p2_pct = p2_made / p2_atp, 95 | p3_made = as.numeric(p3m), 96 | p3_atp = as.numeric(p3a), 97 | p3_pct = p3_made / p3_atp, 98 | points = as.numeric(pts), 99 | team_name = team_name, 100 | team_id = team_id, 101 | game_id = game_id) 102 | stats$p2_pct[is.nan(stats$p2_pct)] <- 0 103 | stats$p3_pct[is.nan(stats$p3_pct)] <- 0 104 | stats$fg_pct[is.nan(stats$fg_pct)] <- 0 105 | stats 106 | str(stats) 107 | stats <- rbind(home_stats, away_stats) %>% 108 | dplyr::transmute(player_name = name, 109 | player_no = no, 110 | player_id = id, 111 | fg_made = as.numeric(fgm), 112 | fg_atp = as.numeric(fga), 113 | fg_pct = fg_made / fg_atp, 114 | p2_made = as.numeric(p2m), 115 | p2_atp = as.numeric(p2a), 116 | p2_pct = p2_made / p2_atp, 117 | p3_made = as.numeric(p3m), 118 | p3_atp = as.numeric(p3a), 119 | p3_pct = p3_made / p3_atp, 120 | points = as.numeric(pts), 121 | team_name = as.character(team_name), 122 | team_id = team_id, 123 | game_id = game_id) 124 | stats$p2_pct[is.nan(stats$p2_pct)] <- 0 125 | stats$p3_pct[is.nan(stats$p3_pct)] <- 0 126 | stats$fg_pct[is.nan(stats$fg_pct)] <- 0 127 | stats 128 | str(stats) 129 | devtools::load_all(".") 130 | x <- extract_shots(bad_ids) 131 | bad_ids <- c(2010215, 2010150, 2010216, 2010147) 132 | x <- extract_shots(bad_ids) 133 | x <- scrape_raw_list(bad_ids) 134 | View(x) 135 | x[[1]] 136 | attr(x) 137 | attributes(x) 138 | x$game_ids 139 | attr(x, "game_ids") 140 | devtools::load_all(".") 141 | y <- extract_shots(bad_ids) 142 | y <- extract_shots(bad_ids) 143 | View(y) 144 | devtools::load_all(".") 145 | bad_ids <- c(2010215, 2010150, 2010216, 2010147) 146 | game_ids <- c(2010215, 2010150, 2010216, 2010147) 147 | error_message <- paste("Check game IDs:", bad_ids) 148 | paste("Check game IDs:" bad_ids, collapse = TRUE) 149 | ?paste 150 | paste(bad_ids, collapse = ",") 151 | paste(bad_ids, collapse = ", ") 152 | devtools::load_all(".") 153 | extract_shots(bad_ids) 154 | devtools::load_all(".") 155 | extract_shots(bad_ids) 156 | base_api <- "http://baloncestoenvivo.feb.es/api/ShotChart/" 157 | game_apis <- paste0(base_api, game_ids) 158 | api_requests <- lapply(game_apis, httr::GET) 159 | # Dealing with API request errors 160 | errors <- unlist(lapply(api_requests, httr::http_error)) 161 | good_game_ids <- game_ids[!errors] 162 | bad_game_ids <- game_ids[errors] 163 | error_message <- paste("request game ID", bad_game_ids) 164 | error_message 165 | #lapply(api_requests, httr::warn_for_status, task = error_message) 166 | Map(httr::warn_for_status, api_requests, task = error_message) 167 | #lapply(api_requests, httr::warn_for_status, task = error_message) 168 | mapply(httr::warn_for_status, api_requests, task = error_message) 169 | error_message 170 | list(error_message) 171 | library(purrr) 172 | map2(api_requests, bad_game_ids, httr::warn_for_status) 173 | Map(httr::warn_for_status, api_requests, task = error_message) 174 | as.list(error_message) 175 | #lapply(api_requests, httr::warn_for_status, task = error_message) 176 | mapply(httr::warn_for_status, api_requests, 177 | task = as.list(error_message)) 178 | #lapply(api_requests, httr::warn_for_status, task = error_message) 179 | mapply(httr::warn_for_status, api_requests, 180 | task = as.list(error_message)) 181 | library(httr) 182 | mapply(warn_for_status, api_requests, task = error_message) 183 | error_message 184 | mapply(warn_for_status, api_requests, task = error_message[1]) 185 | Map(warn_for_status, api_requests, task = error_message) 186 | length(error_message) 187 | n_errors <- length(error_message) 188 | api_requests[errors] 189 | api_errors <- api_requests[errors] 190 | n_errors <- length(api_errors) 191 | warn_for_status(api_errors[[i]], task = error_message[i]) 192 | for (i in 1:n_errors) { 193 | warn_for_status(api_errors[[i]], task = error_message[i]) 194 | } 195 | devtools::load_all(".") 196 | x <- extract_shots(bad_ids) 197 | View(x) 198 | ids <- 2010208:2010216 199 | week17_2018 <- extract_shots(ids) 200 | devtools::load_all(".") 201 | week17_2018 <- extract_shots(ids) 202 | bad_request <- scrape_raw_list(bad_ids) 203 | game_ids <- ids 204 | base_api <- "http://baloncestoenvivo.feb.es/api/ShotChart/" 205 | game_apis <- paste0(base_api, game_ids) 206 | api_requests <- lapply(game_apis, httr::GET) 207 | # Dealing with API request errors 208 | errors <- unlist(lapply(api_requests, httr::http_error)) 209 | good_game_ids <- game_ids[!errors] 210 | bad_game_ids <- game_ids[errors] 211 | error_message <- paste("request game ID", bad_game_ids) 212 | length(errors) 213 | sum(errors) 214 | id <- 2010215 215 | devtools::load_all(".") 216 | scrape_raw_list(id) 217 | x <- scrape_raw_list(id) 218 | View(x) 219 | x[[1]] 220 | id <- 2010215:2010216 221 | x <- scrape_raw_list(id) 222 | cov_pal <- x[[1]] 223 | View(cov_pal) 224 | game_ids <- 2010208:2010216 225 | # game_ids <- 2010208:2010216 226 | # raw_list <- scrape_raw_list 227 | raw_list <- scrape_raw_list(game_ids) 228 | devtools::load_all(".") 229 | raw_list <- scrape_raw_list(game_ids) 230 | stats <- shooting_stats(raw_list, game_ids) 231 | devtools::load_all(".") 232 | stats <- shooting_stats(raw_list, game_ids) 233 | View(stats) 234 | devtools::load_all(".") 235 | bad_ids <- c(2010215, 2010150, 2010216, 2010147) 236 | x <- scrape_raw_list(bad_ids) 237 | y <- extract_shots(bad_ids) 238 | View(y) 239 | game_ids <- 2010215 240 | raw_list <- scrape_raw_list(game_ids) 241 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 242 | # We need to create a variable identifying the team 243 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 244 | View(home_team_names) 245 | home_team_names[[1]] 246 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 247 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 248 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 249 | home_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[1]]) 250 | away_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[2]]) 251 | View(home_stats_dfs) 252 | home_stats_dfs[[1]] 253 | home_stats_dfs <- Map(cbind, home_stats_dfs, 254 | "team_name" = home_team_names, 255 | "team_id" = home_team_ids, 256 | "game_id" = game_ids, 257 | "home_team" = home_team_ids, 258 | "away_team" = away_team_names) 259 | View(home_stats_dfs) 260 | home_stats_dfs[[1]] 261 | home_stats_dfs <- Map(cbind, home_stats_dfs, 262 | "team_name" = home_team_names, 263 | "team_id" = home_team_ids, 264 | "game_id" = game_ids, 265 | "home_team" = home_team_names, 266 | "away_team" = away_team_names) 267 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 268 | # We need to create a variable identifying the team 269 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 270 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 271 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 272 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 273 | home_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[1]]) 274 | away_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[2]]) 275 | home_stats_dfs <- Map(cbind, home_stats_dfs, 276 | "team_name" = home_team_names, 277 | "team_id" = home_team_ids, 278 | "game_id" = game_ids, 279 | "home_team" = home_team_names, 280 | "away_team" = away_team_names) 281 | View(home_stats_dfs) 282 | home_stats_dfs[[1]] 283 | devtools::load_all(".") 284 | bad_ids <- c(2010215, 2010150, 2010216, 2010147) 285 | y <- extract_shots(bad_ids) 286 | View(y) 287 | game_ids <- bad_ids 288 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 289 | raw_list <- scrape_raw_list(game_ids) 290 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 291 | # We need to create a variable identifying the team 292 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 293 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 294 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 295 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 296 | home_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[1]]) 297 | away_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[2]]) 298 | home_stats_dfs <- Map(cbind, home_stats_dfs, 299 | "team_name" = home_team_names, 300 | "team_id" = home_team_ids, 301 | "game_id" = game_ids, 302 | "home_team" = home_team_names, 303 | "away_team" = away_team_names) 304 | View(home_stats_dfs) 305 | home_stats_dfs[[1]] 306 | View(away_team_ids) 307 | View(away_team_names) 308 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 309 | # We need to create a variable identifying the team 310 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 311 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 312 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 313 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 314 | home_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[1]]) 315 | away_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[2]]) 316 | View(away_stats_dfs) 317 | away_stats_dfs[[1]] 318 | home_stats_dfs <- Map(cbind, home_stats_dfs, 319 | "team_name" = home_team_names, 320 | "team_id" = home_team_ids, 321 | "game_id" = game_ids, 322 | "home_team" = home_team_names, 323 | "away_team" = away_team_names) 324 | View(home_stats_dfs) 325 | home_stats_dfs[[1]] 326 | View(stats_dfs) 327 | stats_dfs[[1]] 328 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 329 | # We need to create a variable identifying the team 330 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 331 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 332 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 333 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 334 | home_stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER[[1]]) 335 | View(home_stats_dfs) 336 | View(home_stats_dfs) 337 | home_stats_dfs[[1]] 338 | View(raw_list) 339 | raw_list[[1]] 340 | View(away_team_names) 341 | View(home_stats_dfs) 342 | View(stats_dfs) 343 | stats_dfs[[1]] 344 | home_stats_dfs <- lapply(stats_dfs, function(x) x$SHOTCHART$TEAM$PLAYER[[1]]) 345 | View(home_stats_dfs) 346 | View(home_stats_dfs) 347 | home_stats_dfs[[1]] 348 | View(stats_dfs) 349 | x <- stats_dfs[[1]] 350 | str(x) 351 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 352 | # We need to create a variable identifying the team 353 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 354 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 355 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 356 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 357 | home_stats_dfs <- lapply(stats_dfs, function(x) x[[1]]) 358 | str(home_stats_dfs) 359 | away_stats_dfs <- lapply(stats_dfs, function(x) x[[2]]) 360 | home_stats_dfs <- Map(cbind, home_stats_dfs, 361 | "team_name" = home_team_names, 362 | "team_id" = home_team_ids, 363 | "game_id" = game_ids, 364 | "home_team" = home_team_names, 365 | "away_team" = away_team_names) 366 | away_stats_dfs <- Map(cbind, away_stats_dfs, 367 | "team_name" = away_team_names, 368 | "team_id" = away_team_ids, 369 | "game_id" = game_ids, 370 | "home_team" = home_team_names, 371 | "away_team" = away_team_names) 372 | View(home_stats_dfs) 373 | home_stats_dfs[[1]] 374 | View(home_stats_dfs) 375 | str(home_stats_dfs) 376 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 377 | # We need to create a variable identifying the team 378 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 379 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 380 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 381 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 382 | home_stats_dfs <- lapply(stats_dfs, function(x) x[[1]]) 383 | away_stats_dfs <- lapply(stats_dfs, function(x) x[[2]]) 384 | home_stats_dfs <- Map(cbind, home_stats_dfs, 385 | "team_name" = home_team_names, 386 | "team_id" = home_team_ids, 387 | "game_id" = game_ids) 388 | away_stats_dfs <- Map(cbind, away_stats_dfs, 389 | "team_name" = away_team_names, 390 | "team_id" = away_team_ids, 391 | "game_id" = game_ids, 392 | "home_team" = home_team_names, 393 | "away_team" = away_team_names) 394 | View(home_stats_dfs) 395 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 396 | # We need to create a variable identifying the team 397 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 398 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 399 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 400 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 401 | home_stats_dfs <- lapply(stats_dfs, function(x) x[[1]]) 402 | str(home_stats_dfs) 403 | home_stats_dfs <- Map(cbind, home_stats_dfs, 404 | "team_name" = home_team_names, 405 | "team_id" = home_team_ids, 406 | "game_id" = game_ids) 407 | game_ids <- c(2010215, 2010216) 408 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 409 | # We need to create a variable identifying the team 410 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 411 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 412 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 413 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 414 | home_stats_dfs <- lapply(stats_dfs, function(x) x[[1]]) 415 | away_stats_dfs <- lapply(stats_dfs, function(x) x[[2]]) 416 | home_stats_dfs <- Map(cbind, home_stats_dfs, 417 | "team_name" = home_team_names, 418 | "team_id" = home_team_ids, 419 | "game_id" = game_ids) 420 | away_stats_dfs <- Map(cbind, away_stats_dfs, 421 | "team_name" = away_team_names, 422 | "team_id" = away_team_ids, 423 | "game_id" = game_ids, 424 | "home_team" = home_team_names, 425 | "away_team" = away_team_names) 426 | View(away_stats_dfs) 427 | away_stats_dfs[[1]] 428 | home_stats <- do.call("rbind", home_stats_dfs) 429 | away_stats <- do.call("rbind", away_stats_dfs) 430 | stats <- rbind(home_stats, away_stats) %>% 431 | dplyr::transmute(player_name = name, 432 | player_no = no, 433 | player_id = id, 434 | fg_made = as.numeric(fgm), 435 | fg_atp = as.numeric(fga), 436 | fg_pct = fg_made / fg_atp, 437 | p2_made = as.numeric(p2m), 438 | p2_atp = as.numeric(p2a), 439 | p2_pct = p2_made / p2_atp, 440 | p3_made = as.numeric(p3m), 441 | p3_atp = as.numeric(p3a), 442 | p3_pct = p3_made / p3_atp, 443 | points = as.numeric(pts), 444 | team_name = as.character(team_name), 445 | team_id = team_id, 446 | game_id = game_id, 447 | home_team = home_team, 448 | away_team = away_team) 449 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 450 | # We need to create a variable identifying the team 451 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 452 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 453 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 454 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 455 | home_stats_dfs <- lapply(stats_dfs, function(x) x[[1]]) 456 | away_stats_dfs <- lapply(stats_dfs, function(x) x[[2]]) 457 | home_stats_dfs <- Map(cbind, home_stats_dfs, 458 | "team_name" = home_team_names, 459 | "team_id" = home_team_ids, 460 | "game_id" = game_ids, 461 | "home_team" = home_team_names, 462 | "away_team" = away_team_names) 463 | away_stats_dfs <- Map(cbind, away_stats_dfs, 464 | "team_name" = away_team_names, 465 | "team_id" = away_team_ids, 466 | "game_id" = game_ids, 467 | "home_team" = home_team_names, 468 | "away_team" = away_team_names) 469 | home_stats <- do.call("rbind", home_stats_dfs) 470 | away_stats <- do.call("rbind", away_stats_dfs) 471 | stats <- rbind(home_stats, away_stats) %>% 472 | dplyr::transmute(player_name = name, 473 | player_no = no, 474 | player_id = id, 475 | fg_made = as.numeric(fgm), 476 | fg_atp = as.numeric(fga), 477 | fg_pct = fg_made / fg_atp, 478 | p2_made = as.numeric(p2m), 479 | p2_atp = as.numeric(p2a), 480 | p2_pct = p2_made / p2_atp, 481 | p3_made = as.numeric(p3m), 482 | p3_atp = as.numeric(p3a), 483 | p3_pct = p3_made / p3_atp, 484 | points = as.numeric(pts), 485 | team_name = as.character(team_name), 486 | team_id = team_id, 487 | game_id = game_id, 488 | home_team = home_team, 489 | away_team = away_team) 490 | stats$p2_pct[is.nan(stats$p2_pct)] <- 0 491 | stats$p3_pct[is.nan(stats$p3_pct)] <- 0 492 | stats$fg_pct[is.nan(stats$fg_pct)] <- 0 493 | stats 494 | View(stats) 495 | devtools::load_all(".") 496 | bad_ids <- c(2010215, 2010150, 2010216, 2010147) 497 | y <- extract_shots(bad_ids) 498 | View(y) 499 | devtools::load_all(".") 500 | bad_ids <- c(2010215, 2010150, 2010216, 2010147) 501 | y <- extract_shots(bad_ids) 502 | View(y) 503 | devtools::load_all(".") 504 | bad_ids <- c(2010215, 2010150, 2010216, 2010147) 505 | y <- extract_shots(bad_ids) 506 | y <- extract_shots(bad_ids) 507 | View(y) 508 | devtools::load_all(".") 509 | bad_ids <- c(2010215, 2010150, 2010216, 2010147) 510 | y <- extract_shots(bad_ids) 511 | y <- extract_shots(bad_ids) 512 | View(y) 513 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: rfeb 2 | Title: Tools to extract and analyse basketball data from FEB leagues in Spain 3 | Version: 0.1.0 4 | Authors@R: 5 | person(given = "Sergio", 6 | family = "Olmos-Pardo", 7 | role = c("aut", "cre"), 8 | email = "s.olmos.pardo@gmail.com") 9 | Description: Facilitates the extraction and analysis of data for the different 10 | leagues organized by the Spanish basketball federation (FEB). 11 | Depends: R (>= 2.10) 12 | License: MIT + file LICENSE 13 | Encoding: UTF-8 14 | LazyData: true 15 | Imports: 16 | httr, 17 | jsonlite, 18 | ggplot2, 19 | dplyr, 20 | magrittr 21 | RoxygenNote: 6.1.1 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2019 2 | COPYRIGHT HOLDER: Sergio Olmos Pardo 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2019 Sergio Olmos Pardo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(construct_arc) 4 | export(construct_court) 5 | export(extract_shots) 6 | export(plot_shotchart) 7 | export(scrape_raw_list) 8 | import(ggplot2) 9 | importFrom(magrittr,"%>%") 10 | -------------------------------------------------------------------------------- /R/construct_arc.R: -------------------------------------------------------------------------------- 1 | 2 | #' Construct the coordinates of an arc 3 | #' 4 | #' @param x0 Center x coordinate 5 | #' @param y0 Center y coordinate 6 | #' @param r Radius 7 | #' @param start In radians from 0 to pi 8 | #' @param stop In radians from 0 to pi 9 | #' 10 | #' @return Data frame with x, y coordinates 11 | #' @export 12 | #' 13 | #' @examples 14 | #' circle <- construct_arc(x0 = 0, y0 = 0, r = 1, start = 0, stop = 2 * pi) 15 | #' plot(circle$x, circle$y, type = "l") 16 | #' arc <- construct_arc(2, 1, 5, start = pi, stop = pi / 2) 17 | #' plot(arc$x, arc$y, type = "l") 18 | construct_arc <- function(x0, y0, r, start, stop) { 19 | by <- ifelse(start <= stop, 0.01, -0.01) 20 | theta <- seq(start, stop, by) 21 | x <- x0 + r * cos(theta) 22 | y <- y0 + r * sin(theta) 23 | data.frame(x, y) 24 | } 25 | -------------------------------------------------------------------------------- /R/construct_court.R: -------------------------------------------------------------------------------- 1 | #' Construct coordinates of basketball court 2 | #' 3 | #' @return A list containing data frames corresponding to different parts of the court 4 | #' @export 5 | #' 6 | #' @examples 7 | #' court <- construct_court() 8 | construct_court <- function() { 9 | outer_lines <- data.frame( 10 | x = c(0, 0, 1500, 1500, 0), 11 | y = c(0, 1400, 1400, 0, 0), 12 | type = "Outer lines" 13 | ) 14 | 15 | paint <- data.frame( 16 | x = c(750 - 175, 750 - 175, 750 + 175, 750 + 175), 17 | y = c(0, 580, 580, 0), 18 | type = "Paint" 19 | ) 20 | 21 | ft_circle <- data.frame( 22 | construct_arc(x0 = 750, y0 = 580, r = 175, start = 0, stop = 2 * pi), 23 | type = "FT circle" 24 | ) 25 | 26 | upper_arc3 <- data.frame( 27 | construct_arc(x0 = 750, y0 = 157.5, r = 625, start = 0, stop = pi), 28 | type = "Upper arc" 29 | ) 30 | 31 | left_corner3 <- data.frame( 32 | x = c(125, 125), 33 | y = c(0, 157.5), 34 | type = "Left corner 3" 35 | ) 36 | 37 | right_corner3 <- data.frame( 38 | x = c(1500 - 125, 1500 - 125), 39 | y = c(157.5, 0), 40 | type = "Right corner 3" 41 | ) 42 | 43 | arc3 <- rbind(right_corner3, upper_arc3, left_corner3) 44 | 45 | court <- list(outer_lines = outer_lines, paint = paint, 46 | ft_circle = ft_circle, arc3 = arc3) 47 | 48 | court 49 | } 50 | -------------------------------------------------------------------------------- /R/data.R: -------------------------------------------------------------------------------- 1 | #' Shooting data from LEB Oro 2 | #' 3 | #' Scraped data from Baloncesto en Vivo from week 17 of LEB Oro 2018/2019 4 | #' 5 | #' @format A data frame with variables: 6 | #' \describe{ 7 | #' \item{x, y}{x, y coordinates of the shot} 8 | #' \item{time}{Time elapsed since begining of game} 9 | #' ... 10 | #' } 11 | #' 12 | #' @source \url{http://baloncestoenvivo.feb.es/} 13 | "shots" 14 | -------------------------------------------------------------------------------- /R/extract_shots.R: -------------------------------------------------------------------------------- 1 | #' Extract shot data for Spanish FEB basketball leagues 2 | #' 3 | #' @param game_ids Game IDs from Baloncesto en Vivo 4 | #' 5 | #' @importFrom magrittr %>% 6 | #' 7 | #' @return A data frame with x, y coordinates 8 | #' @export 9 | #' 10 | #' @examples 11 | #' ids <- 2010208:2010216 12 | #' week17_2018 <- extract_shots(ids) 13 | extract_shots <- function(game_ids) { 14 | 15 | # Scrape the data from Baloncesto en Vivo ==== 16 | data_list <- scrape_raw_list(game_ids) 17 | game_ids <- attr(data_list, "game_ids") 18 | 19 | # Wrangle the data ==== 20 | # We only want the shot data. 21 | shots_list <- lapply(data_list, function(x) x$SHOTCHART$SHOTS) 22 | 23 | # Specify the team names as factor levels of team variable instead of 0s and 1s. 24 | team_names_levels <- lapply(data_list, function(x) x$SHOTCHART$TEAM$name) 25 | team_ids_levels <- lapply(data_list, function(x) x$SHOTCHART$TEAM$id) 26 | # We need to assign the two team factor levels to variable team of 27 | ## each element (i.e. game) of our list. 28 | assign_team_names <- function(x, y) { 29 | factor(as.numeric(x$team), labels = y) 30 | } 31 | team_names_list <- mapply(assign_team_names, 32 | shots_list, team_names_levels) 33 | # Convert list into a single vector that we will append to our final data frame. 34 | team_names_character_list <- lapply(team_names_list, as.character) 35 | team_names_vector <- do.call("c", team_names_character_list) 36 | team_names_to_append <- as.factor(team_names_vector) 37 | 38 | # Create variable identifying the game before merging all elements/games of 39 | ## our list into one big data frame 40 | rows_per_game <- sapply(shots_list, nrow) 41 | ids_to_append <- rep(game_ids, rows_per_game) 42 | 43 | shots_raw <- do.call("rbind", shots_list) 44 | shots_raw$game_id <- ids_to_append 45 | shots_raw$team_name <- team_names_to_append 46 | 47 | # Raw coordinates are in a different scale than court drawn. 48 | ## Trial and error gave me 5.45 a good transformation. 49 | # The x coordinate needs to be flipped accross the vertical line 50 | ## going through the middle of the baseline (750). 51 | ## 2 * 750 - x gives us the desired transformation. 52 | shots <- shots_raw %>% 53 | dplyr::mutate(made = factor(as.numeric(m), levels = c(0, 1), 54 | labels = c("Missed", "Made")), 55 | x = 2 * 750 - 5.45 * as.numeric(x), 56 | y = 5.45 * as.numeric(y), 57 | quarter = as.factor(as.numeric(quarter)), 58 | game_id = game_id, 59 | team_name = as.character(team_name), 60 | player_no = player) %>% 61 | dplyr::select(-m, -player, -team) 62 | 63 | # We need to extract the stats from the raw data list 64 | stats <- shooting_stats(data_list, game_ids) 65 | final_shots <- dplyr::left_join(shots, stats, 66 | by = c("game_id", 67 | "player_no", 68 | "team_name")) 69 | final_shots 70 | } 71 | 72 | -------------------------------------------------------------------------------- /R/plot_shotchart.R: -------------------------------------------------------------------------------- 1 | #' Plot shot charts 2 | #' 3 | #' @param df data frame with at least two coordinate columns named x and y 4 | #' @param ... arguments specified to geom_point(aes(x, y, ...)) 5 | #' 6 | #' @import ggplot2 7 | #' 8 | #' @return A ggplot object 9 | #' @export 10 | #' 11 | #' @examples 12 | #' library(ggplot2) 13 | #' ids <- 2010208:2010216 14 | #' week17_2018 <- extract_shots(ids) 15 | #' plot_shotchart(week17_2018) 16 | #' plot_shotchart(week17_2018, color = made) + facet_wrap(~game_id) 17 | plot_shotchart <- function(df, ...) { 18 | 19 | court <- construct_court() 20 | 21 | plt <- ggplot(environment = environment()) + 22 | geom_point(data = df, aes(x, y, ...), alpha = 0.8) + 23 | geom_path(aes(x, y), size = 0.2, data = court$outer_lines) + 24 | geom_path(aes(x, y), size = 0.2, data = court$paint) + 25 | geom_path(aes(x, y), size = 0.2, data = court$ft_circle) + 26 | geom_path(aes(x, y), size = 0.2, data = court$arc3) + 27 | coord_fixed() + 28 | theme_void() + 29 | theme(panel.background = element_rect(fill = "whitesmoke"), 30 | legend.position = "bottom") + 31 | labs(color = "") 32 | plt 33 | } 34 | -------------------------------------------------------------------------------- /R/scrape_raw_list.R: -------------------------------------------------------------------------------- 1 | #' Scrape raw list object with all the shooting data from Baloncesto en Vivo 2 | #' 3 | #' @param game_ids Game IDs 4 | #' 5 | #' 6 | #' @return A list object 7 | #' @export 8 | #' 9 | #' @examples 10 | #' bad_ids <- c(2010215, 2010150, 2010216, 2010147) 11 | #' good_ids <- 2010213:2010216 12 | #' good_requests <- scrape_raw_list(good_ids) 13 | #' bad_request <- scrape_raw_list(bad_ids) 14 | scrape_raw_list <- function(game_ids) { 15 | base_api <- "http://baloncestoenvivo.feb.es/api/ShotChart/" 16 | game_apis <- paste0(base_api, game_ids) 17 | api_requests <- lapply(game_apis, httr::GET) 18 | 19 | # Dealing with API request errors 20 | errors <- unlist(lapply(api_requests, httr::http_error)) 21 | good_game_ids <- game_ids[!errors] 22 | bad_game_ids <- game_ids[errors] 23 | error_message <- paste("request game ID", bad_game_ids) 24 | # Why not working? 25 | # mapply(httr::warn_for_status, api_requests, task = as.list(error_message)) 26 | bad_requests <- api_requests[errors] 27 | n_errors <- length(bad_requests) 28 | if (sum(errors) != 0) { 29 | for (i in 1:n_errors) { 30 | httr::warn_for_status(bad_requests[[i]], task = error_message[i]) 31 | } 32 | } 33 | good_requests <- api_requests[!errors] 34 | 35 | json_list <- lapply(good_requests, httr::content, 36 | as = "text", encoding = "UTF-8") 37 | raw_list <- lapply(json_list, jsonlite::fromJSON) 38 | 39 | attr(raw_list, "game_ids") <- good_game_ids 40 | 41 | raw_list 42 | } 43 | -------------------------------------------------------------------------------- /R/shooting_stats.R: -------------------------------------------------------------------------------- 1 | shooting_stats <- function(raw_list, game_ids) { 2 | stats_dfs <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$PLAYER) 3 | # We need to create a variable identifying the team 4 | home_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[1]) 5 | home_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[1]) 6 | away_team_names <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$name[2]) 7 | away_team_ids <- lapply(raw_list, function(x) x$SHOTCHART$TEAM$id[2]) 8 | 9 | home_stats_dfs <- lapply(stats_dfs, function(x) x[[1]]) 10 | away_stats_dfs <- lapply(stats_dfs, function(x) x[[2]]) 11 | 12 | home_stats_dfs <- Map(cbind, home_stats_dfs, 13 | "team_name" = home_team_names, 14 | "team_id" = home_team_ids, 15 | "game_id" = game_ids, 16 | "home_team" = home_team_names, 17 | "away_team" = away_team_names) 18 | away_stats_dfs <- Map(cbind, away_stats_dfs, 19 | "team_name" = away_team_names, 20 | "team_id" = away_team_ids, 21 | "game_id" = game_ids, 22 | "home_team" = home_team_names, 23 | "away_team" = away_team_names) 24 | home_stats <- do.call("rbind", home_stats_dfs) 25 | away_stats <- do.call("rbind", away_stats_dfs) 26 | stats <- rbind(home_stats, away_stats) %>% 27 | dplyr::transmute(player_name = name, 28 | player_no = no, 29 | player_id = id, 30 | fg_made = as.numeric(fgm), 31 | fg_atp = as.numeric(fga), 32 | fg_pct = fg_made / fg_atp, 33 | p2_made = as.numeric(p2m), 34 | p2_atp = as.numeric(p2a), 35 | p2_pct = p2_made / p2_atp, 36 | p3_made = as.numeric(p3m), 37 | p3_atp = as.numeric(p3a), 38 | p3_pct = p3_made / p3_atp, 39 | points = as.numeric(pts), 40 | team_name = as.character(team_name), 41 | team_id = team_id, 42 | game_id = game_id, 43 | home_team = home_team, 44 | away_team = away_team) 45 | stats$p2_pct[is.nan(stats$p2_pct)] <- 0 46 | stats$p3_pct[is.nan(stats$p3_pct)] <- 0 47 | stats$fg_pct[is.nan(stats$fg_pct)] <- 0 48 | 49 | stats 50 | } 51 | 52 | # str(stats) 53 | # game_ids <- 2010208:2010216 54 | # raw_list <- scrape_raw_list(game_ids) 55 | # stats <- shooting_stats(raw_list, game_ids) 56 | 57 | # shots <- extract_shots(game_ids) 58 | # final_df <- dplyr::left_join(shots, stats, by = c("game_id", "player_no", 59 | # "team_name")) 60 | 61 | #str(final_df) 62 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | ```{r setup, include=FALSE} 6 | knitr::opts_chunk$set(echo = TRUE) 7 | ``` 8 | 9 | # rfeb 10 | 11 | rfeb provides a set of tools for scraping and analyzing basketball data from Spain's FEB leagues. 12 | 13 | 14 | ## Installation 15 | 16 | ```{r, eval = FALSE} 17 | # install.packages("devtools") 18 | devtools::install_github("solmos/rfeb") 19 | ``` 20 | 21 | ## Examples 22 | 23 | ```{r} 24 | library(rfeb) 25 | library(ggplot2) 26 | game_ids <- 2010208:2010216 27 | shots <- extract_shots(game_ids) 28 | plot_shotchart(shots, color = made) + 29 | facet_wrap(~game_id) 30 | ``` 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | rfeb 3 | ==== 4 | 5 | rfeb provides a set of tools for scraping and analyzing basketball data from Spain's FEB leagues. 6 | 7 | Installation 8 | ------------ 9 | 10 | ``` r 11 | # install.packages("devtools") 12 | devtools::install_github("solmos/rfeb") 13 | ``` 14 | 15 | Examples 16 | -------- 17 | 18 | ``` r 19 | library(rfeb) 20 | library(ggplot2) 21 | game_ids <- 2010208:2010216 22 | shots <- extract_shots(game_ids) 23 | plot_shotchart(shots, color = made) + 24 | facet_wrap(~game_id) 25 | ``` 26 | 27 | ![](README_files/figure-markdown_github/unnamed-chunk-2-1.png) 28 | -------------------------------------------------------------------------------- /README_files/figure-markdown_github/unnamed-chunk-2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solmos/rfeb/8a89545c532246e9baae0901e45b09986b0f8a4d/README_files/figure-markdown_github/unnamed-chunk-2-1.png -------------------------------------------------------------------------------- /data-raw/shots.R: -------------------------------------------------------------------------------- 1 | game_id <- 2010208:2010216 2 | shots <- extract_shots(game_ids) 3 | usethis::use_data(shots, shots, overwrite = TRUE) 4 | -------------------------------------------------------------------------------- /data/shots.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solmos/rfeb/8a89545c532246e9baae0901e45b09986b0f8a4d/data/shots.rda -------------------------------------------------------------------------------- /man/construct_arc.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/construct_arc.R 3 | \name{construct_arc} 4 | \alias{construct_arc} 5 | \title{Construct the coordinates of an arc} 6 | \usage{ 7 | construct_arc(x0, y0, r, start, stop) 8 | } 9 | \arguments{ 10 | \item{x0}{Center x coordinate} 11 | 12 | \item{y0}{Center y coordinate} 13 | 14 | \item{r}{Radius} 15 | 16 | \item{start}{In radians from 0 to pi} 17 | 18 | \item{stop}{In radians from 0 to pi} 19 | } 20 | \value{ 21 | Data frame with x, y coordinates 22 | } 23 | \description{ 24 | Construct the coordinates of an arc 25 | } 26 | \examples{ 27 | circle <- construct_arc(x0 = 0, y0 = 0, r = 1, start = 0, stop = 2 * pi) 28 | plot(circle$x, circle$y, type = "l") 29 | arc <- construct_arc(2, 1, 5, start = pi, stop = pi / 2) 30 | plot(arc$x, arc$y, type = "l") 31 | } 32 | -------------------------------------------------------------------------------- /man/construct_court.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/construct_court.R 3 | \name{construct_court} 4 | \alias{construct_court} 5 | \title{Construct coordinates of basketball court} 6 | \usage{ 7 | construct_court() 8 | } 9 | \value{ 10 | A list containing data frames corresponding to different parts of the court 11 | } 12 | \description{ 13 | Construct coordinates of basketball court 14 | } 15 | \examples{ 16 | court <- construct_court() 17 | } 18 | -------------------------------------------------------------------------------- /man/extract_shots.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extract_shots.R 3 | \name{extract_shots} 4 | \alias{extract_shots} 5 | \title{Extract shot data for Spanish FEB basketball leagues} 6 | \usage{ 7 | extract_shots(game_ids) 8 | } 9 | \arguments{ 10 | \item{game_ids}{Game IDs from Baloncesto en Vivo} 11 | } 12 | \value{ 13 | A data frame with x, y coordinates 14 | } 15 | \description{ 16 | Extract shot data for Spanish FEB basketball leagues 17 | } 18 | \examples{ 19 | ids <- 2010208:2010216 20 | week17_2018 <- extract_shots(ids) 21 | } 22 | -------------------------------------------------------------------------------- /man/plot_shotchart.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot_shotchart.R 3 | \name{plot_shotchart} 4 | \alias{plot_shotchart} 5 | \title{Plot shot charts} 6 | \usage{ 7 | plot_shotchart(df, ...) 8 | } 9 | \arguments{ 10 | \item{df}{data frame with at least two coordinate columns named x and y} 11 | 12 | \item{...}{arguments specified to geom_point(aes(x, y, ...))} 13 | } 14 | \value{ 15 | A ggplot object 16 | } 17 | \description{ 18 | Plot shot charts 19 | } 20 | \examples{ 21 | library(ggplot2) 22 | ids <- 2010208:2010216 23 | week17_2018 <- extract_shots(ids) 24 | plot_shotchart(week17_2018) 25 | plot_shotchart(week17_2018, color = made) + facet_wrap(~game_id) 26 | } 27 | -------------------------------------------------------------------------------- /man/scrape_raw_list.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/scrape_raw_list.R 3 | \name{scrape_raw_list} 4 | \alias{scrape_raw_list} 5 | \title{Scrape raw list object with all the shooting data from Baloncesto en Vivo} 6 | \usage{ 7 | scrape_raw_list(game_ids) 8 | } 9 | \arguments{ 10 | \item{game_ids}{Game IDs} 11 | } 12 | \value{ 13 | A list object 14 | } 15 | \description{ 16 | Scrape raw list object with all the shooting data from Baloncesto en Vivo 17 | } 18 | \examples{ 19 | bad_ids <- c(2010215, 2010150, 2010216, 2010147) 20 | good_ids <- 2010213:2010216 21 | good_requests <- scrape_raw_list(good_ids) 22 | bad_request <- scrape_raw_list(bad_ids) 23 | } 24 | -------------------------------------------------------------------------------- /man/shots.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{shots} 5 | \alias{shots} 6 | \title{Shooting data from LEB Oro} 7 | \format{A data frame with variables: 8 | \describe{ 9 | \item{x, y}{x, y coordinates of the shot} 10 | \item{time}{Time elapsed since begining of game} 11 | ... 12 | }} 13 | \source{ 14 | \url{http://baloncestoenvivo.feb.es/} 15 | } 16 | \usage{ 17 | shots 18 | } 19 | \description{ 20 | Scraped data from Baloncesto en Vivo from week 17 of LEB Oro 2018/2019 21 | } 22 | \keyword{datasets} 23 | -------------------------------------------------------------------------------- /rfeb.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 4 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | --------------------------------------------------------------------------------