Skip to content

Commit

Permalink
Export NFL schedule data and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
k5cents committed Sep 11, 2021
1 parent c5073a5 commit f0b5790
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(league_name)
export(league_size)
export(league_teams)
export(player_info)
export(pro_schedule)
export(roster_settings)
export(schedule_settings)
export(score_settings)
Expand All @@ -38,3 +39,4 @@ importFrom(httr,user_agent)
importFrom(jsonlite,fromJSON)
importFrom(jsonlite,toJSON)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
21 changes: 19 additions & 2 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,32 @@
#' @source \url{https://fantasy.espn.com/apis/v3/games/ffl/seasons/2020?view=proTeamSchedules_wl}
"nfl_teams"

#' 2015-2020 GAA Teams
#' 2021 NFL Schedule
#'
#' The 2021 NFL season schedule by team, as of September 10th.
#'
#' @format A data frame with 544 rows and 6 variables:
#' \describe{
#' \item{seasonId}{Season year}
#' \item{scoringPeriodId}{Matchup period}
#' \item{proTeam}{Team abbreviation}
#' \item{opponent}{Team opponent}
#' \item{isHome}{Team the home?}
#' \item{date}{Match date and time}
#' ...
#' }
#' @source \url{https://fantasy.espn.com/apis/v3/games/ffl/seasons/2020?view=proTeamSchedules_wl}
"nfl_schedule"

#' 2015-2021 GAA Teams
#'
#' The GAA is the fantasy league of this package's author.
#'
#' @format A data frame with 13 rows and 3 variables:
#' \describe{
#' \item{team}{Unique yearly team ID}
#' \item{abbrev}{Team abbreviation}
#' \item{years}{Nested list of active years}
#' \item{years}{Nested vector of active years}
#' ...
#' }
#' @source \url{https://fantasy.espn.com/apis/v3/games/ffl/seasons/2021/segments/0/leagues/252353}
Expand Down
37 changes: 37 additions & 0 deletions R/pro-schedule.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' Professional schedule
#'
#' The opponents each team faces every week in a regular season.
#'
#' @param seasonId Season schedule (2004-present), defaults to [ffl_year()].
#' @return Data frame of team opponents by week.
#' @examples
#' pro_schedule(seasonId = ffl_year(-2))
#' @importFrom tibble tibble
#' @export
pro_schedule <- function(seasonId = ffl_year()) {
dat <- try_json(
url = sprintf("https://fantasy.espn.com/apis/v3/games/ffl/seasons/%i", seasonId),
query = list(view = "proTeamSchedules_wl")
)
p <- dat$settings$proTeams$proGamesByScoringPeriod
tm <- data.frame(
stringsAsFactors = FALSE,
team = dat$settings$proTeams$id,
abbrev = dat$settings$proTeams$abbrev
)
sched <- rep(list(NA), length(p))
for (i in seq_along(p)) {
x <- unique(do.call("rbind", p[[i]]))
x <- data.frame(
seasonId = as.integer(seasonId),
scoringPeriodId = unique(x$scoringPeriodId),
proTeam = pro_abbrev(c(x$homeProTeamId, x$awayProTeamId)),
opponent = pro_abbrev(c(x$awayProTeamId, x$homeProTeamId)),
isHome = c(rep(TRUE, nrow(x)), rep(FALSE, nrow(x))),
date = ffl_date(rep(x$date, 2))
)
sched[[i]] <- x[order(x$proTeam), ]
}
sched <- do.call("rbind", sched)
as_tibble(sched)
}
Binary file added data/nfl_schedule.rda
Binary file not shown.
4 changes: 2 additions & 2 deletions man/gaa_teams.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/nfl_schedule.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/pro_schedule.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f0b5790

Please # to comment.