diff --git a/NAMESPACE b/NAMESPACE index f1f246c..ef64240 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,8 @@ # Generated by roxygen2: do not edit by hand export("%>%") +export(cbd_add_net_quad) export(cbd_create_account) -export(cbd_gt_logos) export(cbd_kenpom_authorization) export(cbd_kenpom_ratings) export(cbd_kenpom_ratings_archive) @@ -29,9 +29,7 @@ export(cbd_torvik_similar_resumes) export(cbd_torvik_team_factors) export(cbd_torvik_team_history) export(cbd_torvik_team_split) -export(gt_theme_athletic) import(dplyr) -import(gt) import(httr) import(httr2) importFrom(arrow,read_parquet) @@ -65,9 +63,7 @@ importFrom(purrr,pmap) importFrom(purrr,reduce) importFrom(readr,parse_number) importFrom(readr,read_csv) -importFrom(rlang,as_string) importFrom(rlang,check_installed) -importFrom(rlang,ensym) importFrom(rlang,expr) importFrom(rlang,inform) importFrom(rlang,list2) diff --git a/R/cbd_add_net_quad.R b/R/cbd_add_net_quad.R new file mode 100644 index 0000000..b8df87e --- /dev/null +++ b/R/cbd_add_net_quad.R @@ -0,0 +1,49 @@ +#' Add Net Rankings and Quadrants to Data +#' +#' A utility function for adding current NET rankings and quadrant boundaries to +#' data. +#' +#' @param df Data frame to alter +#' @param net_team_col A column of team names which should be used to match +#' against current NET rankings +#' @param net_col The name of your NET column (or the name to call the new NET +#' column) +#' @param location_col The name of your game location column +#' @param add_net Should NET rankings first be added to your data? Set to FALSE +#' if you already have NET rankings and reference that column name in +#' `net_col.` Defaults to TRUE. +#' +#' @export + +cbd_add_net_quad <- function(df, net_team_col = opp, net_col = net, location_col = location, + add_net = TRUE) { + + # convert string inputs to symbols + location_col <- ensym(location_col) + net_col <- ensym(net_col) + net_team_col <- ensym(net_team_col) + + # if add_net is true, grab net rankings and join to data on net_team_col var. + if(add_net) { + net_data <- cbd_torvik_current_resume() %>% + select(!! net_team_col := team, !! net_col := net) + + # join over the data on the `net_team_col` variable + df <- df %>% + left_join(net_data, by = as.character(net_team_col)) + } + + # define quadrants + df <- df %>% + mutate(quad = case_when( + .data[[net_col]] <= ifelse(.data[[location_col]] == "H", 30, ifelse(.data[[location_col]] == "N", 50, 75)) ~ "Quadrant 1", + .data[[net_col]] > ifelse(.data[[location_col]] == "H", 30, ifelse(.data[[location_col]] == "N", 50, 75)) & + .data[[net_col]] <= ifelse(.data[[location_col]] == "H", 75, ifelse(.data[[location_col]] == "N", 100, 135)) ~ "Quadrant 2", + .data[[net_col]] > ifelse(.data[[location_col]] == "H", 75, ifelse(.data[[location_col]] == "N", 100, 135)) & + .data[[net_col]] <= ifelse(.data[[location_col]] == "H", 160, ifelse(.data[[location_col]] == "N", 200, 240)) ~ "Quadrant 3", + .data[[net_col]] > ifelse(.data[[location_col]] == "H", 160, ifelse(.data[[location_col]] == "N", 200, 240)) ~ "Quadrant 4" + )) + + return(df) +} + diff --git a/R/cbd_gt_logos.R b/R/cbd_gt_logos.R deleted file mode 100644 index 0c4c1db..0000000 --- a/R/cbd_gt_logos.R +++ /dev/null @@ -1,48 +0,0 @@ -#' Render in Logo-Team HTML for Tables -#' -#' Appends a column with HTML that will render in team logos and names -#' for table plotting. -#' -#' I really hate putting team logos and names in separate columns, -#' and this helper function will add team logos or wordmarks and create -#' the HTML code to render in logos and teams in the same column for tables. -#' If you are using `gt` to plot, you must use `fmt_markdown(team_logo)` to -#' render in the HTML. Set `logo_column` to your `team` variable if you wish -#' to overwrite your `team` column with the logo HTML. -#' -#' @returns Returns data with an appended HTML column. -#' @param data Pass through your plotting data -#' @param team_column Indicate which column contains your `team` variable -#' @param logo_column Indicates the name of the HTML column -#' @param logo_type Indicate whether you want to plot logos ('logo', default) or -#' wordmarks ('wordmark') -#' @param logo_height The height of the logo or wordmark in the HTML string -#' @import dplyr -#' @importFrom glue glue -#' @importFrom rlang ensym as_string -#' @importFrom magrittr %>% -#' @examples -#' \dontrun{try(cbd_torvik_ratings(year=2023) %>% head() %>% select(team, conf) %>% -#' cbbdata::cbd_gt_logos(team, team) %>% gt::gt() %>% gt::fmt_markdown(team))} -#' -#' @export -cbd_gt_logos <- function(data, team_column, logo_column = 'team_logo', logo_type = 'logo', logo_height = 25) { - - cbbdata:::check_key() # ensure user is logged-in - - team_column <- ensym(team_column) - - # get team logos - logo_data <- cbbdata::cbd_teams() %>% select(team = common_team, logo, wordmark) - - # create column in df. that combines the logo and team name - data <- data %>% - # match logo to team name - left_join(logo_data, multiple = 'first', by = join_by(!!team_column == 'team')) %>% - mutate(selected_logo = case_when(logo_type == 'logo' ~ logo, .default = wordmark)) %>% - mutate({{logo_column}} := glue("<img src='{selected_logo}' style='height: {logo_height}px; width: auto; vertical-align: middle;'> {.data[[as_string(team_column)]]}")) %>% - select(-c(selected_logo, logo, wordmark)) - - return(data) - -} diff --git a/R/cbd_torvik_team_factors.R b/R/cbd_torvik_team_factors.R index 183756e..4f6fd9a 100644 --- a/R/cbd_torvik_team_factors.R +++ b/R/cbd_torvik_team_factors.R @@ -47,7 +47,7 @@ cbd_torvik_team_factors <- function(year = NULL, venue = "all", game_type = "all } else { - cbbdata::cbd_torvik_ratings(year = year) %>% distinct(team, conf) + conf_info <- cbbdata::cbd_torvik_ratings(year = year) %>% distinct(team, conf) } cbbdata:::validate_input(venue, c('all', 'home', 'away', 'neutral', 'road'), "Please input correct venue value (see details)") diff --git a/R/gt_theme_athletic.R b/R/gt_theme_athletic.R deleted file mode 100644 index 917c407..0000000 --- a/R/gt_theme_athletic.R +++ /dev/null @@ -1,98 +0,0 @@ -#' The Athletic `gt` Table Theme -#' -#' A theme for styling `gt` tables similar to The Athletic. -#' -#' @returns Returns data with an appended HTML column. -#' @param gt_object An existing gt table object of class `gt_tbl` -#' @param ... Optional additional arguments to [gt::tab_options()] -#' @import gt -#' @importFrom magrittr %>% -#' @examples -#' \dontrun{try(mtcars %>% head() %>% gt::gt() %>% cbbdata::gt_theme_athletic())} -#' -#' @export -gt_theme_athletic <- function(gt_object, ...) { - - stopifnot(`'gt_object' must be a 'gt_tbl', have you accidentally passed raw data?` = "gt_tbl" %in% - class(gt_object)) - - table_id <- subset(gt_object[['_options']], parameter == 'table_id')$value[[1]] - - if (is.na(table_id)) { - table_id <- gt::random_id() - opt_position <- which("table_id" %in% gt_object[["_options"]][["parameter"]])[[1]] - gt_object[["_options"]][["value"]][[opt_position]] <- table_id - } - - table <- gt_object %>% - gt::opt_table_font( - font = list( - gt::google_font('Spline Sans Mono'), - gt::default_fonts() - ), - weight = 500 - ) %>% - gt::tab_style( - locations = gt::cells_column_labels( - columns = gt::everything() - ), - style = gt::cell_text( - font = gt::google_font('Work Sans'), - weight = 650, - size = px(12), - transform = 'uppercase' - ) - ) %>% - gt::tab_style( - locations = gt::cells_title('title'), - style = gt::cell_text( - font = gt::google_font('Work Sans'), - weight = 650 - ) - ) %>% - gt::tab_style( - locations = gt::cells_title('subtitle'), - style = gt::cell_text( - font = gt::google_font('Work Sans'), - weight = 500 - ) - ) %>% - gt::tab_style( - style = gt::cell_borders(sides = 'left', weight = px(0.5), color = 'black'), - locations = gt::cells_body( - columns = c(-names(gt_object[['_data']])[1]) - ) - ) %>% - gt::tab_style( - style = gt::cell_borders(sides = "top", color = 'black', weight = px(1.5), style = 'dotted'), - locations = gt::cells_body( - rows = gt::everything() - ) - ) %>% - gt::cols_align( - align = 'center', - columns = gt::everything() - ) %>% - gt::tab_options( - table.font.size = 12, - column_labels.border.bottom.width = 2, - column_labels.border.bottom.color = 'black', - column_labels.border.top.color = 'white', - row_group.border.bottom.color = 'white', - table.border.top.style = 'none', - table.border.bottom.style = 'none', - heading.border.bottom.style = 'none', - heading.align = 'left', - heading.title.font.size = px(26), - source_notes.border.lr.style = 'none', - source_notes.font.size = 10, - ... - ) %>% - gt::opt_css( - paste0("#", table_id, " tbody tr:last-child {border-bottom: 2px solid #ffffff00;}"), - add = TRUE - ) - - return(table) - -} diff --git a/_pkgdown.yml b/_pkgdown.yml index 3645b4c..0c234fa 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -58,6 +58,7 @@ reference: - cbd_torvik_game_factors - cbd_torvik_game_stats - cbd_torvik_season_schedule + - cbd_add_net_quad - title: Predictions desc: Functions for running T-Rank predictions and Monte Carlo simulations contents: @@ -72,11 +73,6 @@ reference: - cbd_torvik_ncaa_sheets - cbd_torvik_resume_database - cbd_torvik_similar_resumes -- title: gt Utilities - desc: Utility functions for gt tables - contents: - - gt_theme_athletic - - cbd_gt_logos - title: Other contents: - cbd_create_account diff --git a/man/cbd_add_net_quad.Rd b/man/cbd_add_net_quad.Rd new file mode 100644 index 0000000..de01e73 --- /dev/null +++ b/man/cbd_add_net_quad.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/cbd_add_net_quad.R +\name{cbd_add_net_quad} +\alias{cbd_add_net_quad} +\title{Add Net Rankings and Quadrants to Data} +\usage{ +cbd_add_net_quad( + df, + net_team_col = opp, + net_col = net, + location_col = location, + add_net = TRUE +) +} +\arguments{ +\item{df}{Data frame to alter} + +\item{net_team_col}{A column of team names which should be used to match +against current NET rankings} + +\item{net_col}{The name of your NET column (or the name to call the new NET +column)} + +\item{location_col}{The name of your game location column} + +\item{add_net}{Should NET rankings first be added to your data? Set to FALSE +if you already have NET rankings and reference that column name in +\code{net_col.} Defaults to TRUE.} +} +\description{ +A utility function for adding current NET rankings and quadrant boundaries to +data. +} diff --git a/man/cbd_gt_logos.Rd b/man/cbd_gt_logos.Rd deleted file mode 100644 index 47c95f3..0000000 --- a/man/cbd_gt_logos.Rd +++ /dev/null @@ -1,46 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/cbd_gt_logos.R -\name{cbd_gt_logos} -\alias{cbd_gt_logos} -\title{Render in Logo-Team HTML for Tables} -\usage{ -cbd_gt_logos( - data, - team_column, - logo_column = "team_logo", - logo_type = "logo", - logo_height = 25 -) -} -\arguments{ -\item{data}{Pass through your plotting data} - -\item{team_column}{Indicate which column contains your \code{team} variable} - -\item{logo_column}{Indicates the name of the HTML column} - -\item{logo_type}{Indicate whether you want to plot logos ('logo', default) or -wordmarks ('wordmark')} - -\item{logo_height}{The height of the logo or wordmark in the HTML string} -} -\value{ -Returns data with an appended HTML column. -} -\description{ -Appends a column with HTML that will render in team logos and names -for table plotting. -} -\details{ -I really hate putting team logos and names in separate columns, -and this helper function will add team logos or wordmarks and create -the HTML code to render in logos and teams in the same column for tables. -If you are using \code{gt} to plot, you must use \code{fmt_markdown(team_logo)} to -render in the HTML. Set \code{logo_column} to your \code{team} variable if you wish -to overwrite your \code{team} column with the logo HTML. -} -\examples{ -\dontrun{try(cbd_torvik_ratings(year=2023) \%>\% head() \%>\% select(team, conf) \%>\% -cbbdata::cbd_gt_logos(team, team) \%>\% gt::gt() \%>\% gt::fmt_markdown(team))} - -} diff --git a/man/gt_theme_athletic.Rd b/man/gt_theme_athletic.Rd deleted file mode 100644 index ed78797..0000000 --- a/man/gt_theme_athletic.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/gt_theme_athletic.R -\name{gt_theme_athletic} -\alias{gt_theme_athletic} -\title{The Athletic \code{gt} Table Theme} -\usage{ -gt_theme_athletic(gt_object, ...) -} -\arguments{ -\item{gt_object}{An existing gt table object of class \code{gt_tbl}} - -\item{...}{Optional additional arguments to \code{gt::table_options()}} -} -\value{ -Returns data with an appended HTML column. -} -\description{ -A theme for styling \code{gt} tables similar to The Athletic. -} -\examples{ -\dontrun{try(mtcars \%>\% head() \%>\% gt::gt() \%>\% cbbdata::gt_theme_athletic())} - -}