R/get_gamelogs.R

Defines functions get_gamelogs

Documented in get_gamelogs

#' Get Gamelogs
#'
#' Function that pulls NBA Game Logs from specified Season and Season Type
#' - Season can be a specific year (2021) or a list (2000:2021)
#' - Season Type must be Regular Season, Play-in, or Playoffs
#' @return
#' @export
#'
#' @param season_year numeric
#' @param season_types character
#'
#' @importFrom nbastatR game_logs
#' @importFrom dplyr select mutate
#' @importFrom stringr str_to_lower
#'
#' @examples
#' df <- get_gamelogs(2021, 'Regular Season')
#'
get_gamelogs <- function(season_year, season_choice){
  if (season_year < 1947) {
    stop("NBA data starts for the 1946-47 season")
  }
  slug_type <- season_choice %>%
    str_to_lower()

  if (!slug_type %in% c("regular season", "playoffs", "playin")) {
    stop("Season Type must be Regular Season, Playoffs, or PlayIn")
  }

  gameLogs <- suppressWarnings(game_logs(seasons = as.numeric(season_year), league = "NBA",
                                         result_types = "player", season_types = as.character(slug_type)))
  gameLogsYesterday <- gameLogs %>%
    select(Season = yearSeason, Date = dateGame, GameID = idGame, TeamGamesPlayed = numberGameTeamSeason, Team = slugTeam,
           Location = locationGame, DaysRest = countDaysRestTeam, Opponent = slugOpponent, Outcome = outcomeGame,
           Player = namePlayer, PlayerID = idPlayer, FGM = fgm, FGA = fga, FGPercent = pctFG, threePFGMade = fg3m,
           threePAttempted = fg3a, threePointPercent = pctFG3, FTPercent = pctFT, twoPointFGMade = fg2m,
           twoPointFGAttempted = fg2a, twoPointFGPercent = pctFG2, MP = minutes, FTM = ftm, FTA = fta, OREB = oreb,
           DREB = dreb, TRB = treb, AST = ast, STL = stl, BLK = blk, TOV = tov, PF = pf, PTS = pts, PlusMinus = plusminus) %>%
    mutate(Type = as.character(season_choice))

  suppressWarnings(rm(df_nba_player_dict))

  print(paste0('Retrieving ', nrow(gameLogsYesterday), ' Game log Rows for NBA ', season_choice))
  return(gameLogsYesterday)
}
jyablonski/practice_package documentation built on Dec. 21, 2021, 5:14 a.m.