Skip to content

Commit

Permalink
cfbfastR v1.4.0 (#31)
Browse files Browse the repository at this point in the history
* convert pieces to markdown in Rd files.

* separate code into a different file to try not to confuse people

* add updated urls for hoopR/wehoop to sdv org version

* add weather function

* switch to cli, updated docs

* Update DESCRIPTION

* Update pkgdown.yaml
saiemgilani authored Aug 25, 2021
1 parent 7cf3dff commit a08cb19
Showing 41 changed files with 693 additions and 658 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -43,12 +43,6 @@ jobs:

- name: Install package
run: R CMD INSTALL .
- name: Render README
run: Rscript -e 'rmarkdown::render("README.Rmd")'
- name: Commit results of README
run: |
git commit README.md -m 'Re-build README.Rmd' || echo "No changes to commit"
git push origin || echo "No changes to commit"
- name: Deploy package
run: |
git config --local user.email "actions@github.com"
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: cfbfastR
Title: Functions to Access College Football Play by Play Data
Version: 1.3.3
Version: 1.4.0
Authors@R:
c(person(given = "Saiem",
family = "Gilani",
@@ -55,6 +55,7 @@ Depends:
R (>= 3.5.0)
Imports:
assertthat,
cli (>= 1.1.0),
dplyr,
furrr,
future,
@@ -72,10 +73,8 @@ Imports:
stringr (>= 1.3.0),
tibble (>= 3.0),
tidyr (>= 1.0.0),
usethis (>= 1.6.0),
xgboost (>= 1.1)
Suggests:
cli (>= 1.1.0),
crayon (>= 1.3.4),
curl,
DBI,
@@ -85,7 +84,8 @@ Suggests:
rmarkdown,
RSQLite,
stats,
testthat
testthat,
usethis (>= 1.6.0)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ export(cfbd_game_media)
export(cfbd_game_player_stats)
export(cfbd_game_records)
export(cfbd_game_team_stats)
export(cfbd_game_weather)
export(cfbd_key)
export(cfbd_metrics_ppa_games)
export(cfbd_metrics_ppa_players_games)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# **cfbfastR v1.4.0**

### Added [```cfbd_game_weather()```](https://saiemgilani.github.io/cfbfastR/reference/cfbd_game_weather.html)

# **cfbfastR v1.3.3**

### Hotfix [```cfbd_game_player_stats()```](https://saiemgilani.github.io/cfbfastR/reference/cfbd_game_player_stats.html)
41 changes: 20 additions & 21 deletions R/cfb_pbp.R
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ NULL
#' or writes it into a db using some forwarded arguments in the dots
#' @param seasons A vector of 4-digit years associated with given College Football seasons.
#' @param ... Additional arguments passed to an underlying function that writes
#' the season data into a database (used by \code{\link[=update_cfb_db]{update_cfb_db()}}).
#' the season data into a database (used by [`update_cfb_db()`][update_cfb_db]).
#' @param qs Wheter to use the function [qs::qdeserialize()] for more efficient loading.
#' @export
load_cfb_pbp <- function(seasons, ..., qs = FALSE) {
@@ -17,19 +17,19 @@ load_cfb_pbp <- function(seasons, ..., qs = FALSE) {
if (all(c("dbConnection", "tablename") %in% names(dots))) in_db <- TRUE else in_db <- FALSE

if (isTRUE(qs) && !is_installed("qs")) {
usethis::ui_stop("Package {usethis::ui_value('qs')} required for argument {usethis::ui_value('qs = TRUE')}. Please install it.")
cli::cli_abort("Package {.val qs} required for argument {.val qs = TRUE}. Please install it.")
}

most_recent <- most_recent_season()

if (!all(seasons %in% 2014:most_recent)) {
usethis::ui_stop("Please pass valid seasons between 2014 and {most_recent}")
cli::cli_abort("Please pass valid seasons between 2014 and {most_recent}")
}

if (length(seasons) > 1 && is_sequential() && isFALSE(in_db)) {
usethis::ui_info(c(
cli::cli_alert_info(c(
"It is recommended to use parallel processing when trying to load multiple seasons.",
"Please consider running {usethis::ui_code('future::plan(\"multisession\")')}!",
"Please consider running {.code future::plan(\"multisession\")}!",
"Will go on sequentially..."
))
}
@@ -129,23 +129,23 @@ update_cfb_db <- function(dbdir = ".",
force_rebuild = FALSE,
db_connection = NULL) {

# rule_header("Update cfbfastR Play-by-Play Database")
rule_header("Update cfbfastR Play-by-Play Database")

if (!is_installed("DBI") | !is_installed("purrr") |
(!is_installed("RSQLite") & is.null(db_connection))) {
usethis::ui_stop("{my_time()} | Packages {usethis::ui_value('DBI')}, {usethis::ui_value('RSQLite')} and {usethis::ui_value('purrr')} required for database communication. Please install them.")
cli::cli_abort("{my_time()} | Packages {.val DBI}, {.val RSQLite} and {.val purrr} required for database communication. Please install them.")
}

if (any(force_rebuild == "NEW")) {
usethis::ui_stop("{my_time()} | The argument {usethis::ui_value('force_rebuild = NEW')} is only for internal usage!")
cli::cli_abort("{my_time()} | The argument {.val 'force_rebuild = NEW'} is only for internal usage!")
}

if (!(is.logical(force_rebuild) | is.numeric(force_rebuild))) {
usethis::ui_stop("{my_time()} | The argument {usethis::ui_value('force_rebuild')} has to be either logical or numeric!")
cli::cli_abort("{my_time()} | The argument {.val 'force_rebuild'} has to be either logical or numeric!")
}

if (!dir.exists(dbdir) & is.null(db_connection)) {
usethis::ui_oops("{my_time()} | Directory {usethis::ui_path(dbdir)} doesn't exist yet. Try creating...")
cli::cli_alert_danger("{my_time()} | Directory {.file {dbdir}} doesn't exist yet. Try creating...")
dir.create(dbdir)
}

@@ -193,11 +193,10 @@ update_cfb_db <- function(dbdir = ".",
# }

message_completed("Database update completed", in_builder = TRUE)
usethis::ui_info("{my_time()} | Path to your db: {usethis::ui_path(DBI::dbGetInfo(connection)$dbname)}")
cli::cli_alert_info("{my_time()} | Path to your db: {.file {DBI::dbGetInfo(connection)$dbname}}")
if (is.null(db_connection)) DBI::dbDisconnect(connection)
# rule_footer("DONE")
rule_footer("DONE")
}

# this is a helper function to build cfbfastR database from Scratch
build_cfb_db <- function(tblname = "cfbfastR_pbp", db_conn, rebuild = FALSE, show_message = TRUE) {

@@ -208,23 +207,23 @@ build_cfb_db <- function(tblname = "cfbfastR_pbp", db_conn, rebuild = FALSE, sho
dplyr::ungroup()

if (all(rebuild == TRUE)) {
usethis::ui_todo("{my_time()} | Purging the complete data table {usethis::ui_value(tblname)} in your connected database...")
cli::cli_ul("{my_time()} | Purging the complete data table {.val {tblname}} in your connected database...")
DBI::dbRemoveTable(db_conn, tblname)
seasons <- valid_seasons %>% dplyr::pull("season")
usethis::ui_todo("{my_time()} | Starting download of {length(seasons)} seasons between {min(seasons)} and {max(seasons)}...")
cli::cli_ul("{my_time()} | Starting download of {length(seasons)} seasons between {min(seasons)} and {max(seasons)}...")
} else if (is.numeric(rebuild) & all(rebuild %in% valid_seasons$season)) {
string <- paste0(rebuild, collapse = ", ")
if (show_message){usethis::ui_todo("{my_time()} | Purging {string} season(s) from the data table {usethis::ui_value(tblname)} in your connected database...")}
if (show_message){cli::cli_ul("{my_time()} | Purging {string} season(s) from the data table {.val {tblname}} in your connected database...")}
DBI::dbExecute(db_conn, glue::glue_sql("DELETE FROM {`tblname`} WHERE season IN ({vals*})", vals = rebuild, .con = db_conn))
seasons <- valid_seasons %>% dplyr::filter(.data$season %in% rebuild) %>% dplyr::pull("season")
usethis::ui_todo("{my_time()} | Starting download of the {string} season(s)...")
cli::cli_ul("{my_time()} | Starting download of the {string} season(s)...")
} else if (all(rebuild == "NEW")) {
usethis::ui_info("{my_time()} | Can't find the data table {usethis::ui_value(tblname)} in your database. Will load the play by play data from scratch.")
cli::cli_alert_info("{my_time()} | Can't find the data table {.val {tblname}} in your database. Will load the play by play data from scratch.")
seasons <- valid_seasons %>% dplyr::pull("season")
usethis::ui_todo("{my_time()} | Starting download of {length(seasons)} seasons between {min(seasons)} and {max(seasons)}...")
cli::cli_ul("{my_time()} | Starting download of {length(seasons)} seasons between {min(seasons)} and {max(seasons)}...")
} else {
seasons <- NULL
usethis::ui_oops("{my_time()} | At least one invalid value passed to argument {usethis::ui_code('force_rebuild')}. Please try again with valid input.")
cli::cli_alert_danger("{my_time()} | At least one invalid value passed to argument {.val force_rebuild}. Please try again with valid input.")
}

if (!is.null(seasons)) {
@@ -244,6 +243,6 @@ get_missing_cfb_games <- function(completed_games, dbConnection, tablename) {

need_scrape <- completed_games[!completed_games %in% db_ids]

usethis::ui_info("{my_time()} | You have {length(db_ids)} games and are missing {length(need_scrape)}.")
cli::cli_alert_info("{my_time()} | You have {length(db_ids)} games and are missing {length(need_scrape)}.")
return(need_scrape)
}
42 changes: 23 additions & 19 deletions R/cfbd_betting.R
Original file line number Diff line number Diff line change
@@ -17,18 +17,18 @@
#' # 7 OTs LSU at TAMU
#' cfbd_betting_lines(year = 2018, week = 13, team = "Texas A&M", conference = "SEC")
#' }
#' @param game_id (\emph{Integer} optional): Game ID filter for querying a single game
#' @param game_id (*Integer* optional): Game ID filter for querying a single game
#' Can be found using the [cfbd_game_info()] function
#' @param year (\emph{Integer} required): Year, 4 digit format(\emph{YYYY})
#' @param week (\emph{Integer} optional): Week - values from 1-15, 1-14 for seasons pre-playoff (i.e. 2013 or earlier)
#' @param season_type (\emph{String} default regular): Select Season Type: regular or postseason
#' @param team (\emph{String} optional): D-I Team
#' @param home_team (\emph{String} optional): Home D-I Team
#' @param away_team (\emph{String} optional): Away D-I Team
#' @param conference (\emph{String} optional): Conference abbreviation - Select a valid FBS conference\cr
#' @param year (*Integer* required): Year, 4 digit format(*YYYY*)
#' @param week (*Integer* optional): Week - values from 1-15, 1-14 for seasons pre-playoff (i.e. 2013 or earlier)
#' @param season_type (*String* default regular): Select Season Type: regular or postseason
#' @param team (*String* optional): D-I Team
#' @param home_team (*String* optional): Home D-I Team
#' @param away_team (*String* optional): Away D-I Team
#' @param conference (*String* optional): Conference abbreviation - Select a valid FBS conference\cr
#' Conference abbreviations P5: ACC, B12, B1G, SEC, PAC\cr
#' Conference abbreviations G5 and FBS Independents: CUSA, MAC, MWC, Ind, SBC, AAC
#' @param line_provider (\emph{String} optional): Select Line Provider - Caesars, consensus, numberfire, or teamrankings
#' @param line_provider (*String* optional): Select Line Provider - Caesars, consensus, numberfire, or teamrankings
#' @param verbose Logical parameter (TRUE/FALSE, default: FALSE) to return warnings and messages from function
#' @return Betting information for games with the following columns:
#' \describe{
@@ -45,9 +45,13 @@
#' \item{`provider`:character.}{Line provider.}
#' \item{`spread`:character.}{Spread for the game.}
#' \item{`formatted_spread`:character.}{Formatted spread for the game.}
#' \item{`spread_open`:character.}{Opening spread for the game.}
#' \item{`over_under`:character.}{Over/Under for the game.}
#' \item{`over_under_open`:character.}{Opening over/under for the game.}
#' \item{`home_moneyline`:character.}{Home team moneyline.}
#' \item{`away_moneyline`:character.}{Away team moneyline.}
#' }
#' @source \url{https://api.collegefootballdata.com/lines}
#' @source <https://api.collegefootballdata.com/lines>
#' @keywords Betting Lines
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
@@ -70,21 +74,21 @@ cfbd_betting_lines <- function(game_id = NULL,
conference = NULL,
line_provider=NULL,
verbose = FALSE) {
if (!is.null(game_id) & !is.numeric(game_id)) {
if (!is.null(game_id) && !is.numeric(game_id)) {
# Check if game_id is numeric, if not NULL
usethis::ui_stop( "Enter valid game_id (numeric value)")
cli::cli_abort( "Enter valid game_id (numeric value)")
}
if (!is.null(year) & !(is.numeric(year) & nchar(year) == 4)) {
if (!is.null(year) && !(is.numeric(year) && nchar(year) == 4)) {
# Check if year is numeric, if not NULL
usethis::ui_stop("Enter valid year as a number (YYYY)")
cli::cli_abort("Enter valid year as a number (YYYY)")
}
if (!is.null(week) & !(is.numeric(week) & nchar(week) <= 2)) {
if (!is.null(week) && !(is.numeric(week) && nchar(week) <= 2)) {
# Check if week is numeric, if not NULL
usethis::ui_stop("Enter valid week 1-15\n(14 for seasons pre-playoff, i.e. 2014 or earlier)")
cli::cli_abort("Enter valid week 1-15\n(14 for seasons pre-playoff, i.e. 2014 or earlier)")
}
if (season_type != "regular" & season_type != "postseason") {
if (season_type != "regular" && season_type != "postseason") {
# Check if season_type is appropriate, if not regular
usethis::ui_stop("Enter valid season_type: regular or postseason")
cli::cli_abort("Enter valid season_type: regular or postseason")
}
if (!is.null(team)) {
if (team == "San Jose State") {
@@ -112,7 +116,7 @@ cfbd_betting_lines <- function(game_id = NULL,
if (!is.null(line_provider) && is.character(line_provider) &&
!(line_provider %in% c("Caesars", "consensus", "numberfire", "teamrankings"))) {
# Check line_provider parameter is a valid entry
usethis::ui_stop("Enter valid line provider: Caesars, consensus, numberfire, or teamrankings")
cli::cli_abort("Enter valid line provider: Caesars, consensus, numberfire, or teamrankings")
}
# cfbfastR::cfbd_betting_lines(year = 2018, week = 12, team = "Florida State")
base_url <- "https://api.collegefootballdata.com/lines?"
18 changes: 9 additions & 9 deletions R/cfbd_coaches.R
Original file line number Diff line number Diff line change
@@ -5,17 +5,17 @@
#' @description
#' **Coach information search**
#' A coach search function which provides coaching records and school history for a given coach
#' ```r
#' ```
#' cfbd_coaches(first = "Nick", last = "Saban", team = "alabama")
#' ````
#' @param first (\emph{String} optional): First name for the coach you are trying to look up
#' @param last (\emph{String} optional): Last name for the coach you are trying to look up
#' @param team (\emph{String} optional): Team - Select a valid team, D1 football
#' @param year (\emph{Integer} optional): Year, 4 digit format (\emph{YYYY}).
#' @param min_year (\emph{Integer} optional): Minimum Year filter (inclusive), 4 digit format (\emph{YYYY}).
#' @param max_year (\emph{Integer} optional): Maximum Year filter (inclusive), 4 digit format (\emph{YYYY})
#' @param first (*String* optional): First name for the coach you are trying to look up
#' @param last (*String* optional): Last name for the coach you are trying to look up
#' @param team (*String* optional): Team - Select a valid team, D1 football
#' @param year (*Integer* optional): Year, 4 digit format (*YYYY*).
#' @param min_year (*Integer* optional): Minimum Year filter (inclusive), 4 digit format (*YYYY*).
#' @param max_year (*Integer* optional): Maximum Year filter (inclusive), 4 digit format (*YYYY*)
#' @param verbose Logical parameter (TRUE/FALSE, default: FALSE) to return warnings and messages from function
#' @return [cfbd_coaches()] - A data frame with coach information with the following columns:
#' @return
#' \describe{
#' \item{`first_name`:character.}{First name of coach.}
#' \item{`last_name`:character.}{Last name of coach.}
@@ -32,7 +32,7 @@
#' \item{`sp_offense`:character.}{Bill Connelly's SP+ offense for team.}
#' \item{`sp_defense`:character.}{Bill Connelly's SP+ defense for team.}
#' }
#' @source \url{https://api.collegefootballdata.com/coaches}
#' @source <https://api.collegefootballdata.com/coaches>
#' @keywords Coaches
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
2 changes: 1 addition & 1 deletion R/cfbd_conferences.R
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
#' \item{`abbreviation`:}{Conference abbreviation.}
#' ...
#' }
#' @source \url{https://api.collegefootballdata.com/conferences}
#' @source <https://api.collegefootballdata.com/conferences>
#' @keywords Conferences
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
18 changes: 9 additions & 9 deletions R/cfbd_draft.R
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ NULL
#' \item{`nfl_display_name`: integer}{NFL team display name (usually more neat/complete).}
#' \item{`nfl_logo`: character}{URL for NFL team logo.}
#' }
#' @source \url{https://api.collegefootballdata.com/draft/teams}
#' @source <https://api.collegefootballdata.com/draft/teams>
#' @keywords NFL Teams
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
@@ -115,7 +115,7 @@ cfbd_draft_teams <- function() {
#' \item{`position_name`: character.}{NFL Position group name.}
#' \item{`position_abbreviation`: integer}{NFL position group abbreviation.}
#' }
#' @source \url{https://api.collegefootballdata.com/draft/positions}
#' @source <https://api.collegefootballdata.com/draft/positions>
#' @keywords NFL Positions
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
@@ -178,11 +178,11 @@ cfbd_draft_positions <- function() {
}
#' @title
#' **Get list of NFL draft picks**
#' @param year (\emph{Integer} required): NFL draft class, 4 digit format (\emph{YYYY})
#' @param nfl_team (\emph{String}): NFL drafting team, see [cfbd_draft_teams()] for valid selections.
#' @param college (\emph{String}): NFL draftee college team, see [cfbd_team_info()] for valid selections.
#' @param conference (\emph{String}): NFL draftee college team conference, see [cfbd_conferences()] for valid selections.
#' @param position (\emph{String}): NFL position abbreviation, see [cfbd_draft_positions()] for valid selections.
#' @param year (*Integer* required): NFL draft class, 4 digit format (*YYYY*)
#' @param nfl_team (*String*): NFL drafting team, see [cfbd_draft_teams()] for valid selections.
#' @param college (*String*): NFL draftee college team, see [cfbd_team_info()] for valid selections.
#' @param conference (*String*): NFL draftee college team conference, see [cfbd_conferences()] for valid selections.
#' @param position (*String*): NFL position abbreviation, see [cfbd_draft_positions()] for valid selections.
#' @return [cfbd_draft_picks()] - A data frame with 23 variables:
#' \describe{
#' \item{`college_athlete_id`: integer.}{College athlete referencing id.}
@@ -209,7 +209,7 @@ cfbd_draft_positions <- function() {
#' \item{`hometown_info_longitude`: character.}{Hometown longitude of the NFL draftee.}
#' \item{`hometown_info_county_fips`: character.}{Hometown FIPS code of the NFL draftee.}
#' }
#' @source \url{https://api.collegefootballdata.com/draft/picks}
#' @source <https://api.collegefootballdata.com/draft/picks>
#' @keywords NFL Draft Picks
#' @importFrom jsonlite fromJSON
#' @importFrom httr GET
@@ -233,7 +233,7 @@ cfbd_draft_picks <- function(year = NULL,
position = NULL) {
if (!is.null(year) & !(is.numeric(year) & nchar(year) == 4)) {
# Check if year is numeric, if not NULL
usethis::ui_stop("Enter valid year as a number (YYYY)")
cli::cli_abort("Enter valid year as a number (YYYY)")
}
if (!is.null(nfl_team)) {
# Encode team parameter for URL if not NULL
Loading

0 comments on commit a08cb19

Please sign in to comment.