R/nhl_helpers.R

Defines functions nhl_get_team_id_type get_id get_this_season

Documented in nhl_get_team_id_type

#' Helper functions for nhl team functions.
#'

# Helper function to classify how a team is being identified (e.g. BUF, Sabres, Buffalo Sabres).
# The idea is that users should be able to input whatever identifier is easiest for them in
# team-specific functions.
#' @export
nhl_get_team_id_type <- function(identifier, teams) {
  if(identifier %in% teams$id) {
    type <- 'id'
  } else if(identifier %in% teams$name) {
    type <- 'name'
  } else if(identifier %in% teams$fullName) {
    type <- 'fullName'
  } else if(toupper(identifier) %in% teams$abbr) {
    type <- 'abbr'
  } else {
    type <- NULL
  }
  return(type)
}



# Helper function to return team id (numeric) from name, fullname, or abbreviation
#' @export
get_id <- function(x, type, teams) {
  switch(type,
         id       = teams$id[teams$id == x],
         name     = teams$id[teams$name == x],
         abbr     = teams$id[teams$abbr == toupper(x)],
         fullName = teams$id[teams$fullName == x])
}



# Helper function to return current season in format required by NHL API (e.g. 20112012)
# season cutoff is August.
#' @export
get_this_season <- function() {
  this_month <- sapply(strsplit(as.character(Sys.Date()), "-"), head, 2)[2]
  this_year <- sapply(strsplit(as.character(Sys.Date()), "-"), head, 2)[1]

  if(this_month %in% paste0('0', seq(1, 8, 1))) {
    this_season <- paste0(as.numeric(this_year) - 1, this_year)
  } else {
    this_season <- paste0(this_year, as.numeric(this_year) - 1)
  }
  return(this_season)
}
alexpavlakis/nhl documentation built on May 18, 2019, 2:35 p.m.