Skip to content

Commit

Permalink
add condition for broken player box games with no stats
Browse files Browse the repository at this point in the history
  • Loading branch information
saiemgilani committed Apr 3, 2023
1 parent df6f56b commit c6f970d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 12 additions & 1 deletion R/espn_mbb_data.R
Expand Up @@ -2710,6 +2710,7 @@ espn_mbb_player_stats <- function(athlete_id, year, season_type='regular', total
#' @importFrom lubridate with_tz ymd_hm
#' @export
helper_espn_mbb_pbp <- function(resp){

game_json <- resp %>%
jsonlite::fromJSON()

Expand All @@ -2718,6 +2719,7 @@ helper_espn_mbb_pbp <- function(resp){
plays <- game_json %>%
purrr::pluck("plays") %>%
dplyr::as_tibble()

if (pbp_source != "none" && nrow(plays) > 10) {
homeAway1 <- jsonlite::fromJSON(resp)[['header']][['competitions']][['competitors']][[1]][['homeAway']][1]

Expand Down Expand Up @@ -3225,8 +3227,17 @@ helper_espn_mbb_player_box <- function(resp){
boxScoreAvailable <- game_json[["header"]][["competitions"]][["boxscoreAvailable"]]

boxScoreSource <- game_json[["header"]][["competitions"]][["boxscoreSource"]]

# This is checking if [[athletes]][[1]]'s stat rebounds is able to be converted to a numeric value
# without introducing NA's
suppressWarnings(
valid_stats <- players_box_score_df[["statistics"]][[1]][["athletes"]][[1]][["stats"]][[1]] %>%
purrr::pluck(7) %>%
as.numeric()
)
if (boxScoreAvailable == TRUE &&
length(players_box_score_df[["statistics"]][[1]][["athletes"]][[1]]) > 1) {
length(players_box_score_df[["statistics"]][[1]][["athletes"]][[1]]) > 1 &&
!is.na(valid_stats)) {
players_df <- players_box_score_df %>%
tidyr::unnest("statistics") %>%
tidyr::unnest("athletes")
Expand Down
14 changes: 13 additions & 1 deletion R/espn_nba_data.R
Expand Up @@ -2396,10 +2396,13 @@ helper_espn_nba_pbp <- function(resp){

game_json <- resp %>%
jsonlite::fromJSON()

pbp_source <- game_json[["header"]][["competitions"]][["playByPlaySource"]]

plays <- game_json %>%
purrr::pluck("plays") %>%
dplyr::as_tibble()

if (pbp_source != "none" && nrow(plays) > 10) {
homeAway1 <- jsonlite::fromJSON(resp)[['header']][['competitions']][['competitors']][[1]][['homeAway']][1]

Expand Down Expand Up @@ -2909,8 +2912,17 @@ helper_espn_nba_player_box <- function(resp){
boxScoreAvailable <- game_json[["header"]][["competitions"]][["boxscoreAvailable"]]

boxScoreSource <- game_json[["header"]][["competitions"]][["boxscoreSource"]]

# This is checking if [[athletes]][[1]]'s stat rebounds is able to be converted to a numeric value
# without introducing NA's
suppressWarnings(
valid_stats <- players_box_score_df[["statistics"]][[1]][["athletes"]][[1]][["stats"]][[1]] %>%
purrr::pluck(7) %>%
as.numeric()
)
if (boxScoreAvailable == TRUE &&
length(players_box_score_df[["statistics"]][[1]][["athletes"]][[1]]) > 1) {
length(players_box_score_df[["statistics"]][[1]][["athletes"]][[1]]) > 1 &&
!is.na(valid_stats)) {
players_df <- players_box_score_df %>%
tidyr::unnest("statistics") %>%
tidyr::unnest("athletes")
Expand Down

0 comments on commit c6f970d

Please sign in to comment.