Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on usethis and progressr #134

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
1 change: 0 additions & 1 deletion .Rbuildignore
Expand Up @@ -39,7 +39,6 @@ nba_pbp_db
^data-raw$
^data-raw
^CODE_OF_CONDUCT\.md
^logo.png
^\.github$
^man/figures
^README\.Rmd$
Expand Down
11 changes: 3 additions & 8 deletions DESCRIPTION
Expand Up @@ -32,13 +32,13 @@ Description: A utility to quickly obtain clean and tidy men's
analyze the data for themselves.
License: MIT + file LICENSE
URL: https://github.com/sportsdataverse/hoopR,
http://hoopr.sportsdataverse.org/
https://hoopr.sportsdataverse.org
BugReports: https://github.com/sportsdataverse/hoopR/issues
SystemRequirements: pandoc (>= 1.12.3), pandoc-citeproc
Depends:
R (>= 4.0.0)
Imports:
cli (>= 1.1.0),
cli (>= 3.1.0),
data.table (>= 1.14.0),
dplyr,
furrr,
Expand All @@ -49,22 +49,17 @@ Imports:
jsonlite,
lubridate,
magrittr,
progressr (>= 0.6.0),
purrr (>= 0.3.0),
Rcpp (>= 1.0.7),
RcppParallel (>= 5.1.4),
rlang (>= 0.4.0),
rvest (>= 1.0.0),
stringr (>= 1.3.0),
tidyr (>= 1.0.0),
usethis (>= 1.6.0)
tidyr (>= 1.0.0)
Suggests:
crayon (>= 1.3.4),
curl,
DBI,
ggplot2,
ggrepel,
qs (>= 0.25.1),
rmarkdown,
RSQLite,
stats,
Expand Down
332 changes: 168 additions & 164 deletions NEWS.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions R/espn_mbb_data.R
Expand Up @@ -1423,7 +1423,7 @@ parse_espn_mbb_scoreboard <- function(group, season_dates) {
"broadcasts",
broadcast_market = list(1, "market"),
broadcast_name = list(1, "names", 1)) %>%
dplyr::select(!where(is.list)) %>%
dplyr::select(!dplyr::where(is.list)) %>%
janitor::clean_names() %>%
make_hoopR_data("ESPN MBB Scoreboard Information from ESPN.com",Sys.time())
} else {
Expand All @@ -1439,12 +1439,12 @@ parse_espn_mbb_scoreboard <- function(group, season_dates) {
"broadcasts",
broadcast_market = list(1, "market"),
broadcast_name = list(1, "names", 1)) %>%
dplyr::select(!where(is.list)) %>%
dplyr::select(!dplyr::where(is.list)) %>%
janitor::clean_names() %>%
make_hoopR_data("ESPN MBB Scoreboard Information from ESPN.com",Sys.time())
} else {
mbb_data %>%
dplyr::select(!where(is.list)) %>%
dplyr::select(!dplyr::where(is.list)) %>%
janitor::clean_names() %>%
make_hoopR_data("ESPN MBB Scoreboard Information from ESPN.com",Sys.time())
}
Expand Down
42 changes: 22 additions & 20 deletions R/espn_nba_data.R
Expand Up @@ -1367,7 +1367,7 @@ espn_nba_scoreboard <- function(season){
"broadcasts",
broadcast_market = list(1, "market"),
broadcast_name = list(1, "names", 1)) %>%
dplyr::select(!where(is.list)) %>%
dplyr::select(!dplyr::where(is.list)) %>%
janitor::clean_names() %>%
make_hoopR_data("ESPN NBA Scoreboard Information from ESPN.com",Sys.time())
} else {
Expand All @@ -1383,12 +1383,12 @@ espn_nba_scoreboard <- function(season){
"broadcasts",
broadcast_market = list(1, "market"),
broadcast_name = list(1, "names", 1)) %>%
dplyr::select(!where(is.list)) %>%
dplyr::select(!dplyr::where(is.list)) %>%
janitor::clean_names() %>%
make_hoopR_data("ESPN NBA Scoreboard Information from ESPN.com",Sys.time())
} else {
nba_data %>%
dplyr::select(!where(is.list)) %>%
dplyr::select(!dplyr::where(is.list)) %>%
janitor::clean_names() %>%
make_hoopR_data("ESPN NBA Scoreboard Information from ESPN.com",Sys.time())
}
Expand Down Expand Up @@ -1502,23 +1502,25 @@ espn_nba_standings <- function(year){
#joining the 2 dataframes together to create a standings table

standings <- cbind(teams, standings_data) %>%
dplyr::mutate(team_id = as.integer(.data$team_id)) %>%
dplyr::mutate_at(c(
"avgpointsagainst",
"avgpointsfor",
"clincher",
"differential",
"divisionwinpercent",
"gamesbehind",
"leaguewinpercent",
"losses",
"playoffseed",
"streak",
"winpercent",
"wins"
), as.numeric)
standings <- standings %>%
make_hoopR_data("ESPN NBA Standings Information from ESPN.com",Sys.time())
dplyr::mutate(
team_id = as.integer(.data$team_id),
dplyr::across(c(
"avgpointsagainst",
"avgpointsfor",
"clincher",
"differential",
"divisionwinpercent",
"gamesbehind",
"leaguewinpercent",
"losses",
"playoffseed",
"streak",
"winpercent",
"wins"
),
.fns = as.numeric)
) %>%
make_hoopR_data("ESPN NBA Standings Information from ESPN.com", Sys.time())
},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no standings data available!"))
Expand Down
91 changes: 48 additions & 43 deletions R/load_mbb.R
Expand Up @@ -83,7 +83,6 @@ load_mbb_pbp <- function(seasons = most_recent_mbb_season(), ...,
old <- options(list(stringsAsFactors = FALSE, scipen = 999))
on.exit(options(old))
dots <- rlang::dots_list(...)
loader <- rds_from_url
if (!is.null(dbConnection) && !is.null(tablename)) in_db <- TRUE else in_db <- FALSE

if (isTRUE(seasons)) seasons <- 2006:most_recent_mbb_season()
Expand All @@ -96,10 +95,12 @@ load_mbb_pbp <- function(seasons = most_recent_mbb_season(), ...,

urls <- paste0("https://github.com/sportsdataverse/sportsdataverse-data/releases/download/espn_mens_college_basketball_pbp/play_by_play_", seasons, ".rds")

p <- NULL
if (is_installed("progressr")) p <- progressr::progressor(along = seasons)
out <- lapply(
cli::cli_progress_along(seasons, name = "Loading"),
function(i) {
rds_from_url(urls[i])
})

out <- lapply(urls, progressively(loader, p))
out <- rbindlist_with_attrs(out)
if (in_db) {
DBI::dbWriteTable(dbConnection, tablename, out, append = TRUE)
Expand Down Expand Up @@ -179,7 +180,6 @@ load_mbb_team_box <- function(seasons = most_recent_mbb_season(), ...,
old <- options(list(stringsAsFactors = FALSE, scipen = 999))
on.exit(options(old))
dots <- rlang::dots_list(...)
loader <- rds_from_url

if (!is.null(dbConnection) && !is.null(tablename)) in_db <- TRUE else in_db <- FALSE
if (isTRUE(seasons)) seasons <- 2003:most_recent_mbb_season()
Expand All @@ -192,10 +192,12 @@ load_mbb_team_box <- function(seasons = most_recent_mbb_season(), ...,

urls <- paste0("https://github.com/sportsdataverse/sportsdataverse-data/releases/download/espn_mens_college_basketball_team_boxscores/team_box_", seasons, ".rds")

p <- NULL
if (is_installed("progressr")) p <- progressr::progressor(along = seasons)
out <- lapply(
cli::cli_progress_along(seasons, name = "Loading"),
function(i) {
rds_from_url(urls[i])
})

out <- lapply(urls, progressively(loader, p))
out <- rbindlist_with_attrs(out)
if (in_db) {
DBI::dbWriteTable(dbConnection, tablename, out, append = TRUE)
Expand Down Expand Up @@ -291,7 +293,6 @@ load_mbb_player_box <- function(seasons = most_recent_mbb_season(), ...,
old <- options(list(stringsAsFactors = FALSE, scipen = 999))
on.exit(options(old))
dots <- rlang::dots_list(...)
loader <- rds_from_url

if (!is.null(dbConnection) && !is.null(tablename)) in_db <- TRUE else in_db <- FALSE
if (isTRUE(seasons)) seasons <- 2003:most_recent_mbb_season()
Expand All @@ -304,10 +305,12 @@ load_mbb_player_box <- function(seasons = most_recent_mbb_season(), ...,

urls <- paste0("https://github.com/sportsdataverse/sportsdataverse-data/releases/download/espn_mens_college_basketball_player_boxscores/player_box_", seasons, ".rds")

p <- NULL
if (is_installed("progressr")) p <- progressr::progressor(along = seasons)
out <- lapply(
cli::cli_progress_along(seasons, name = "Loading"),
function(i) {
rds_from_url(urls[i])
})

out <- lapply(urls, progressively(loader, p))
out <- rbindlist_with_attrs(out)
if (in_db) {
DBI::dbWriteTable(dbConnection, tablename, out, append = TRUE)
Expand Down Expand Up @@ -435,10 +438,12 @@ load_mbb_schedule <- function(seasons = most_recent_mbb_season(), ...,

urls <- paste0("https://github.com/sportsdataverse/sportsdataverse-data/releases/download/espn_mens_college_basketball_schedules/mbb_schedule_", seasons, ".rds")

p <- NULL
if (is_installed("progressr")) p <- progressr::progressor(along = seasons)
out <- lapply(
cli::cli_progress_along(seasons, name = "Loading"),
function(i) {
rds_from_url(urls[i])
})

out <- lapply(urls, progressively(loader, p))
out <- rbindlist_with_attrs(out)
if (in_db) {
DBI::dbWriteTable(dbConnection, tablename, out, append = TRUE)
Expand All @@ -458,12 +463,8 @@ load_mbb_games <- function() {
}

#' **Update or create a hoopR MBB play-by-play database**
#' @name update_mbb_db
NULL
#' @title
#' **Update or create a hoopR MBB play-by-play database**
#' @rdname update_mbb_db
#' @description `update_mbb_db()` updates or creates a database with `hoopR`
#'
#' `update_mbb_db()` updates or creates a database with `hoopR`
#' play by play data of all completed and available games since 2006.
#'
#' @details This function creates and updates a data table with the name `tblname`
Expand Down Expand Up @@ -513,21 +514,26 @@ update_mbb_db <- function(dbdir = ".",
on.exit(options(old))
# rule_header("Update hoopR 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.")
suggest_required <- c("DBI", "purrr")
# need RSQLite if db_connection
if (!is.null(db_connection)) {
suggest_required <- c(suggest_required, "RSQLite")
}
rlang::check_installed(suggest_required, "for database communication.")

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 {.code force_rebuild = {.val 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!")
if (!is.logical(force_rebuild) || !is.numeric(force_rebuild)) {
cli::cli_abort("{my_time()} | {.arg 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 {.val {dbdir}} doesn't exist yet.")
cli::cli_alert_info("Trying to create it.")
dir.create(dbdir)
}

Expand All @@ -544,7 +550,7 @@ update_mbb_db <- function(dbdir = ".",
build_mbb_db(tblname, connection, rebuild = force_rebuild)
}

user_message("Checking for missing completed games...", "todo")
hoop_todo("Checking for missing completed games...")
completed_games <- load_mbb_games() %>%
# completed games since 2006, excluding the broken games
dplyr::filter(.data$season >= 2006) %>%
Expand All @@ -565,15 +571,15 @@ update_mbb_db <- function(dbdir = ".",
# new_pbp <- build_hoopR_pbp(missing, rules = FALSE)
#
# if (nrow(new_pbp) == 0) {
# user_message("Raw data of new games are not yet ready. Please try again in about 10 minutes.", "oops")
# cli::cli_alert_danger("Raw data of new games are not yet ready. Please try again in about 10 minutes.")
# } else {
# user_message("Appending new data to database...", "todo")
# hoop_todo("Appending new data to database...")
# DBI::dbWriteTable(connection, tblname, new_pbp, append = TRUE)
# }
# }

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_success(cli::col_green("Database update completed"))
cli::cli_inform("{my_time()} | Path to your db: {.path {DBI::dbGetInfo(connection)$dbname}}")
if (is.null(db_connection)) DBI::dbDisconnect(connection)
# rule_footer("DONE")
}
Expand All @@ -585,31 +591,30 @@ build_mbb_db <- function(tblname = "hoopR_mbb_pbp", db_conn, rebuild = FALSE, sh
valid_seasons <- load_mbb_games() %>%
dplyr::filter(.data$season >= 2006) %>%
dplyr::group_by(.data$season) %>%
dplyr::summarise() %>%
dplyr::ungroup()
dplyr::summarise(.groups = "drop")

if (all(rebuild == TRUE)) {
usethis::ui_todo("{my_time()} | Purging the complete data table {usethis::ui_value(tblname)} in your connected database...")
hoop_todo("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)}...")
hoop_todo("Starting download of {length(seasons)} season{?s} 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...")
hoop_todo("Purging {string} {cli::qty(length(rebuild))} 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)...")
hoop_todo("Starting download of the {string} {cli::qty(length(seasons))} 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.")
hoop_info("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)}...")
hoop_todo("Starting download of {length(seasons)} season{?s} 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 {.arg force_rebuild}. Please try again with valid input.")
}

if (!is.null(seasons)) {
Expand All @@ -629,6 +634,6 @@ get_missing_mbb_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)}.")
hoop_info("You have {length(db_ids)} game{?s} and are missing {length(need_scrape)}.")
return(need_scrape)
}