R/get_winner_playoff_bracket.R

Defines functions get_winner_playoff_bracket

Documented in get_winner_playoff_bracket

#' Gather Winner Playoff Bracket
#'
#' Given a league ID, get the winner playoff bracket for the league.
#'
#' @return Returns a data frame containing playoff information for the winner bracket.
#' @author Nick Bultman, \email{njbultman74@@gmail.com}, December 2021
#' @keywords league playoff winner
#' @importFrom httr GET content
#' @importFrom jsonlite fromJSON
#' @export
#' @examples
#' \dontrun{get_winner_playoff_bracket(688281863499907072)}
#'
#' @param league_id League ID generated by Sleeper (numeric or character)
#'
get_winner_playoff_bracket <- function(league_id) {
  # Execute query to API given league ID specified
  x <- jsonlite::fromJSON(httr::content(httr::GET(paste0("https://api.sleeper.app/v1/league/", league_id, "/winners_bracket")), as = "text"))
  # Check if returned object is NULL
  if(is.null(x)) {
    # If NULL, inform the user and return nothing
    message("League ID did not return any results. Did you enter the league ID correctly?")
  } else {
    # If not NULL, split out nested data frames
    x_t1_from <- x$t1_from
    x_t2_from <- x$t2_from
    # Remove nested data frames from main query
    x$t1_from <- NULL
    x$t2_from <- NULL
    # Change column names in nested data frames for easy interpretability
    names(x_t1_from) <- c("t1_from_w", "t1_from_l")
    names(x_t2_from) <- c("t2_from_w", "t2_from_l")
    # Bind split out data frames to main query
    x_fin <- cbind(x, x_t1_from, x_t2_from)
    # Return final data frame
    return(x_fin)
  }
}

Try the sleeperapi package in your browser

Any scripts or data that you put into this service are public.

sleeperapi documentation built on June 22, 2024, 9:29 a.m.