R/get_league_drafts.R

Defines functions get_league_drafts

Documented in get_league_drafts

#' Gather League Draft Information
#'
#' Given a league ID, gather draft information for that league.
#'
#' @return Returns a data frame containing draft information for that league.
#' @author Nick Bultman, \email{njbultman74@@gmail.com}, December 2021
#' @keywords draft league
#' @importFrom httr GET content
#' @importFrom jsonlite fromJSON
#' @export
#' @examples
#' \dontrun{get_league_drafts(688281863499907072)}
#'
#' @param league_id League ID generated by Sleeper (numeric or character)
#'
get_league_drafts <- function(league_id) {
  # Send request to API given league ID specified
  x <- jsonlite::fromJSON(httr::content(httr::GET(paste0("https://api.sleeper.app/v1/league/", league_id, "/drafts")), as = "text"))
  # Check if class of data returned from request is list
  if(inherits(x, "list")) {
    # If class is list, it is an empty list and no data was found - inform user and do not return anything
    message("No data found - was the league ID entered correctly?")
  } else {
    # If class is not list, break out nested data frames
    x_settings <- x$settings
    x_metadata <- x$metadata
    x_draft_order <- x$draft_order
    # Remove nested data frames from main query
    x$settings <- NULL
    x$metadata <- NULL
    x$draft_order <- NULL
    # Bind previously nested data frames into main query
    x_fin <- cbind(x, x_settings, x_metadata, x_draft_order)
    # 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.