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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sweep GHA Fix] Fix failing GitHub Actions #141

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 76 additions & 17 deletions R/kp_misc_stats.R
Expand Up @@ -26,32 +26,35 @@
#' |ppg |numeric |
#'
#' @importFrom cli cli_abort
#' @importFrom dplyr filter mutate_at
#' @importFrom dplyr select filter mutate mutate_at
#' @import rvest
#' @export
#' @keywords Trends
#' @keywords Refs
#' @family KP Misc. Functions
#'
#' @examples
#' \donttest{
#' try(kp_trends())
#' try(kp_officials(year = 2021))
#' }

kp_trends <- function(){
kp_officials <- function(year = most_recent_mbb_season()){
tryCatch(
expr = {
if (!has_kp_user_and_pw()) stop("This function requires a KenPom subscription e-mail and password combination, set as the system environment variables KP_USER and KP_PW.", "\n See ?kp_user_pw for details.", call. = FALSE)

browser <- login()
if (!(is.numeric(year) && nchar(year) == 4 && year >= 2016)) {
# Check if year is numeric, if not NULL
cli::cli_abort("Enter valid year as a number (YYYY), data only goes back to 2016")
}


### Pull Data
url <- "https://kenpom.com/trends.php"
url <- paste0("https://kenpom.com/officials.php?y=",year)
page <- rvest::session_jump_to(browser, url)
Sys.sleep(5)
header_cols <- c("Season","Efficiency","Tempo","eFG.Pct","TO.Pct",
"OR.Pct","FTRate","FG_2.Pct","FG_3.Pct","FG_3A.Pct",'FT.Pct',
"A.Pct","Blk.Pct","Stl.Pct","NonStl.Pct","Avg.Hgt",
"Continuity","HomeWin.Pct","PPG")
header_cols <- c("Rk","OfficialName","RefRating","Gms","Last.Game",
"Last.Game.1","Last.Game.2")

x <- (page %>%
xml2::read_html() %>%
Expand All @@ -60,21 +63,26 @@ kp_trends <- function(){
as.data.frame()

colnames(x) <- header_cols
x <- x %>%
dplyr::select(-"Last.Game.2") %>%
suppressWarnings(
x <- x %>%
dplyr::filter(!is.na(as.numeric(.data$RefRating)))
)
x <- dplyr::mutate(x,
"Year" = year)
suppressWarnings(
x <- x %>%
dplyr::filter(!is.na(as.numeric(.data$eFG.Pct)))
dplyr::mutate_at(c("Year","RefRating","Gms"), as.numeric) %>%
as.data.frame()
)
### Store Data
kenpom <- x %>%
dplyr::mutate_at(c("Season","Efficiency","Tempo","eFG.Pct","TO.Pct",
"OR.Pct","FTRate","FG_2.Pct","FG_3.Pct","FG_3A.Pct",'FT.Pct',
"A.Pct","Blk.Pct","Stl.Pct","NonStl.Pct","Avg.Hgt",
"Continuity","HomeWin.Pct","PPG"), as.numeric) %>%
janitor::clean_names()

},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no trends data available!"))
message(glue::glue("{Sys.time()}: Invalid arguments or no officials data for {year} available!"))
},
warning = function(w) {
},
Expand All @@ -83,6 +91,11 @@ kp_trends <- function(){
)
return(kenpom)
}
finally = {
}
)
return(kenpom)
}

#' **Get officials rankings**
#'
Expand Down Expand Up @@ -360,7 +373,12 @@ kp_hca <- function(){
#' try(kp_arenas(year=2021))
#' }

kp_arenas <- function(year=most_recent_mbb_season()){
kp_arenas <- function(year=most_recent_mbb_season(){
tryCatch({
if (!has_kp_user_and_pw()) stop("This function requires a KenPom subscription e-mail and password combination, set as the system environment variables KP_USER and KP_PW.", call. = FALSE)
if (!(is.numeric(year) && nchar(year) == 4 && year >= 2010)) stop("Enter valid year as a number (YYYY), data only goes back to 2010", call. = FALSE)
kenpom <- NULL
}
tryCatch(
expr = {
if (!has_kp_user_and_pw()) stop("This function requires a KenPom subscription e-mail and password combination, set as the system environment variables KP_USER and KP_PW.", "\n See ?kp_user_pw for details.", call. = FALSE)
Expand Down Expand Up @@ -430,7 +448,48 @@ kp_arenas <- function(year=most_recent_mbb_season()){
#' @import rvest
#' @export
#' @keywords Game
#' @family KP Misc. Functions
tryCatch({
if (is.null(Sys.getenv("KP_USER")) || is.null(Sys.getenv("KP_PW"))) {
stop("This function requires a KenPom subscription e-mail and password combination, set as the system environment variables KP_USER and KP_PW.", call. = FALSE)
}
if (!(is.numeric(year) && nchar(year) == 4 && year >= 2010)) {
# Check if year is numeric, if not NULL
stop("Enter valid year as a number (YYYY), data only goes back to 2010", call. = FALSE)
}
url <- paste0("https://kenpom.com/game_attrs.php?",
"y=", year,
"&s=", attr)
tryCatch({
page <- rvest::session_jump_to(browser, url)
Sys.sleep(5)
header_cols <- c("Rk","Data","Game",
"col","Location","Conf",
attr)

x <- (page %>%
xml2::read_html() %>%
rvest::html_elements("table"))[[1]] %>%
rvest::html_table()
colnames(x) <- header_cols
x <- dplyr::mutate(x,
"Year" = as.numeric(year)) %>%
as.data.frame()
### Store Data
kenpom <- x %>%
dplyr::select(-"col") %>%
janitor::clean_names()
},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no game attributes for {attr} available!"))
kenpom <- NULL
}
)
},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no game attributes for {attr} available!"))
kenpom <- NULL
}
)
#'
#' @examples
#' \donttest{
Expand Down