#' Get Play by Play Data
#'
#' Function that pulls NBA Play By Play Data from yesterday's games
#' - Season can be a specific year (2021) or a list (2000:2021)
#' - Season Type must be Regular Season, Play-in, or Playoffs
#' @return
#' @export
#'
#'
#' @importFrom nbastatR play_by_play_v2 game_logs
#' @importFrom dplyr select filter distinct pull left_join
#'
#' @examples
#' df <- get_pbp()
#'
get_pbp <- function(){
yesterday <- Sys.Date() - 1
game_dates <- suppressWarnings(game_logs(seasons = 2021, league = 'NBA', result_types = 'team', season_types = 'Playoffs')) %>%
filter(dateGame == yesterday) %>%
select(GameID = idGame, Date = dateGame)
gameid_yesterday_df <- game_dates %>%
select(GameID) %>%
distinct() %>%
pull()
playbyplay_yesterday <- play_by_play_v2(game_ids = gameid_yesterday_df, nest_data = FALSE, return_message = TRUE) %>%
select(slugScore, namePlayer1, slugTeamPlayer1, namePlayer2, slugTeamPlayer2, namePlayer3, slugTeamPlayer3, GameID = idGame,
numberPeriod, timeQuarter, descriptionPlayHome, descriptionPlayVisitor, timeRemaining, scoreAway:marginScore) %>%
left_join(game_dates) %>%
distinct()
print(paste0('Retrieving ', nrow(playbyplay_yesterday), ' PBP Rows'))
return(playbyplay_yesterday)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.