Skip to content

Commit

Permalink
linting and re-documenting
Browse files Browse the repository at this point in the history
  • Loading branch information
saiemgilani committed Mar 29, 2023
1 parent c905b58 commit 1870358
Show file tree
Hide file tree
Showing 220 changed files with 954 additions and 920 deletions.
57 changes: 38 additions & 19 deletions R/kp_box_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @returns Returns a tibble of game box scores with names: away_team, home_team,
#' linescore, officials
#'
#' **away_team**
#' **away_team**
#'
#'
#' |col_name |types |
Expand All @@ -19,9 +19,12 @@
#' |o_rtg |numeric |
#' |percent_ps |numeric |
#' |pts |numeric |
#' |fgm2_a |character |
#' |fgm3_a |character |
#' |ftm_a |character |
#' |fgm_2 |numeric |
#' |fga_2 |numeric |
#' |fgm_3 |numeric |
#' |fga_3 |numeric |
#' |ftm |numeric |
#' |fta |numeric |
#' |or |numeric |
#' |dr |numeric |
#' |a |numeric |
Expand All @@ -48,9 +51,12 @@
#' |o_rtg |numeric |
#' |percent_ps |numeric |
#' |pts |numeric |
#' |fgm2_a |character |
#' |fgm3_a |character |
#' |ftm_a |character |
#' |fgm_2 |numeric |
#' |fga_2 |numeric |
#' |fgm_3 |numeric |
#' |fga_3 |numeric |
#' |ftm |numeric |
#' |fta |numeric |
#' |or |numeric |
#' |dr |numeric |
#' |a |numeric |
Expand Down Expand Up @@ -96,7 +102,7 @@
#'
#' @examples
#' \donttest{
#' x<-try(kp_box(game_id = 6, year = 2021))
#' try(kp_box(game_id = 6, year = 2021))
#' }

kp_box <- function(game_id, year){
Expand Down Expand Up @@ -192,11 +198,24 @@ kp_box <- function(game_id, year){

# box <- xml2::xml_text(box_stat)
suppressWarnings(
x <- x %>% tidyr::separate(col = "Hgt-Wgt-Yr",
x <- x %>%
tidyr::separate(col = "Hgt-Wgt-Yr",
into = c("Hgt", "Wgt", "Yr"),
sep = "\\s") %>%
tidyr::separate(col = "FGM2-A",
into = c("FGM_2","FGA_2"),
sep = "-") %>%
tidyr::separate(col = "FGM3-A",
into = c("FGM_3","FGA_3"),
sep = "-") %>%
tidyr::separate(col = "FTM-A",
into = c("FTM","FTA"),
sep = "-") %>%
dplyr::mutate_at(c("Wgt", "Number", "Min",
"ORtg", "%Ps", "Pts",
"FGM_2", "FGA_2",
"FGM_3", "FGA_3",
"FTM", "FTA",
"OR", "DR", "A", "TO",
"Blk", "Stl", "PF"), as.numeric)
)
Expand Down Expand Up @@ -308,7 +327,7 @@ kp_winprob <- function(game_id, year){
expr = {
if (!has_kp_user_and_pw()) stop("This function requires a KenPom subscription e-mail and password combination,\n 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>=2010)) {
if (!(is.numeric(year) && nchar(year) == 4 && year >= 2010)) {
# Check if year is numeric, if not NULL
cli::cli_abort("Enter valid year as a number (YYYY), data only goes back to 2010")
}
Expand Down Expand Up @@ -354,15 +373,15 @@ kp_winprob <- function(game_id, year){
)
#---- game_data --------
game_data_str <- stringr::str_remove(stringr::str_remove(q[2], "(.+)var data="),"makeWP\\(data\\);")
vn <- data.frame(vn = t(gsub(pattern = "'","", stringr::str_remove(stringr::str_remove(stringr::str_extract_all(q[2],pattern="venue:\'(.+)\', city:"),pattern=", city:"),pattern="venue:"))))
cty <- data.frame(cty = t(gsub(pattern = "'","", stringr::str_remove(stringr::str_remove(stringr::str_extract_all(q[2], pattern="city:\'(.+)\', gameTime:"),pattern=", gameTime:"),pattern="city:"))))
gmtm <- data.frame(gmtm = t(gsub(pattern = "'","", stringr::str_remove(stringr::str_remove(stringr::str_extract_all(q[2],pattern="gameTime:\'(.+)\', dominance:"),pattern=", dominance:"),pattern="gameTime:"))))
dateofgame <- data.frame(dateofgame = t(gsub(pattern = "'","", stringr::str_remove(stringr::str_remove(stringr::str_extract_all(q[2],pattern="dateOfGame:\'(.+)\', ymd:"),pattern=", ymd:"),pattern="dateOfGame:"))))
game_data_str <- stringr::str_remove(game_data_str,pattern="venue:\'(.+)\',(?= city:)")
game_data_str <- stringr::str_remove(game_data_str,pattern="city:\'(.+)\',(?= gameTime:)")
game_data_str <- stringr::str_remove(game_data_str,pattern="gameTime:\'(.+)\',(?= dominance:)")
game_data_str <- stringr::str_remove(game_data_str,pattern="dateOfGame:\'(.+)\',(?= ymd:)")
game_data_str <- stringr::str_remove(game_data_str,pattern=", input:(.+)(?=\\})")
vn <- data.frame(vn = t(gsub(pattern = "'","", stringr::str_remove(stringr::str_remove(stringr::str_extract_all(q[2], pattern = "venue:\'(.+)\', city:"), pattern = ", city:"), pattern = "venue:"))))
cty <- data.frame(cty = t(gsub(pattern = "'","", stringr::str_remove(stringr::str_remove(stringr::str_extract_all(q[2], pattern = "city:\'(.+)\', gameTime:"), pattern = ", gameTime:"), pattern = "city:"))))
gmtm <- data.frame(gmtm = t(gsub(pattern = "'","", stringr::str_remove(stringr::str_remove(stringr::str_extract_all(q[2], pattern = "gameTime:\'(.+)\', dominance:"), pattern = ", dominance:"), pattern = "gameTime:"))))
dateofgame <- data.frame(dateofgame = t(gsub(pattern = "'","", stringr::str_remove(stringr::str_remove(stringr::str_extract_all(q[2], pattern = "dateOfGame:\'(.+)\', ymd:"), pattern = ", ymd:"), pattern = "dateOfGame:"))))
game_data_str <- stringr::str_remove(game_data_str,pattern = "venue:\'(.+)\',(?= city:)")
game_data_str <- stringr::str_remove(game_data_str,pattern = "city:\'(.+)\',(?= gameTime:)")
game_data_str <- stringr::str_remove(game_data_str,pattern = "gameTime:\'(.+)\',(?= dominance:)")
game_data_str <- stringr::str_remove(game_data_str,pattern = "dateOfGame:\'(.+)\',(?= ymd:)")
game_data_str <- stringr::str_remove(game_data_str,pattern = ", input:(.+)(?=\\})")
game_data_str <- gsub("\\{ ",'\\{ "', game_data_str)
game_data_str <- gsub(", ",', "', game_data_str)
game_data_str <- gsub(":",'":', game_data_str)
Expand Down
38 changes: 19 additions & 19 deletions R/kp_historical_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#'
#' @examples
#' \donttest{
#' x<-try(kp_team_history(team = 'Florida St.'))
#' try(kp_team_history(team = 'Florida St.'))
#' }
#'

Expand Down Expand Up @@ -396,31 +396,31 @@ kp_coach_history <- function(coach){

page <- rvest::session_jump_to(browser, url)
Sys.sleep(5)
header_cols<- c('Year','Team.Rk','Team', 'Conf','WL', 'AdjT', 'AdjO', 'AdjD',
'Off.eFG.Pct', 'Off.TO.Pct', 'Off.OR.Pct','Off.FTRate',
'Off.FG_2.Pct', 'Off.FG_3.Pct', 'Off.FT.Pct', 'Off.FG_3A.Pct',
'Off.A.Pct', 'Off.APL',
'Def.eFG.Pct', 'Def.TO.Pct', 'Def.OR.Pct', 'Def.FTRate',
'Def.FG_2.Pct', 'Def.FG_3.Pct',
'Def.Blk.Pct', 'Def.FG_3A.Pct', 'Def.A.Pct',
'Def.APL', 'Foul2Partic.Pct')

x<- (page %>%
header_cols <- c('Year','Team.Rk','Team', 'Conf','WL', 'AdjT', 'AdjO', 'AdjD',
'Off.eFG.Pct', 'Off.TO.Pct', 'Off.OR.Pct','Off.FTRate',
'Off.FG_2.Pct', 'Off.FG_3.Pct', 'Off.FT.Pct', 'Off.FG_3A.Pct',
'Off.A.Pct', 'Off.APL',
'Def.eFG.Pct', 'Def.TO.Pct', 'Def.OR.Pct', 'Def.FTRate',
'Def.FG_2.Pct', 'Def.FG_3.Pct',
'Def.Blk.Pct', 'Def.FG_3A.Pct', 'Def.A.Pct',
'Def.APL', 'Foul2Partic.Pct')

x <- (page %>%
xml2::read_html() %>%
rvest::html_elements(css='#player-table'))[[1]]
rvest::html_elements(css = '#player-table'))[[1]]

## removing national rankings for easier manipulation
## TODO: Add these rankings back as columns
conf <- (page %>%
xml2::read_html() %>%
rvest::html_elements(css='#player-table'))[[1]]
rvest::html_elements(css = '#player-table'))[[1]]

conf_record <- (page %>%
xml2::read_html() %>%
rvest::html_elements("td:nth-child(5) > span"))
conf_record_wl <- dplyr::bind_rows(lapply(rvest::html_text(conf_record),
function(x){
data.frame(x, stringsAsFactors=FALSE)
data.frame(x, stringsAsFactors = FALSE)
}))
conf_record_wl <- conf_record_wl %>%
dplyr::rename("WL.Conf" = "x")
Expand Down Expand Up @@ -635,15 +635,15 @@ kp_program_ratings <- function(){

page <- rvest::session_jump_to(browser, url)
Sys.sleep(5)
header_cols<- c('Rk', 'Team', 'Conf','Rtg',
header_cols <- c('Rk', 'Team', 'Conf','Rtg',
'Best.Rk','Best.Yr',
'Worst.Rk','Worst.Yr', 'KP.Median',
'Top10', 'Top25', 'Top50', 'CH',
'F4', 'S16', 'R1', 'Chg')

x <- (page %>%
xml2::read_html() %>%
rvest::html_elements(css='#ratings-table'))[[1]]
rvest::html_elements(css = '#ratings-table'))[[1]]

x <- x %>%
rvest::html_table() %>%
Expand Down Expand Up @@ -1124,7 +1124,7 @@ kp_confstats <- function(year = most_recent_mbb_season()){
Sys.sleep(5)
x <- (page %>%
xml2::read_html() %>%
rvest::html_elements(css="#confrank-table"))[[1]] %>%
rvest::html_elements(css = "#confrank-table"))[[1]] %>%
rvest::html_table() %>%
as.data.frame()

Expand Down Expand Up @@ -1235,7 +1235,7 @@ kp_confhistory <- function(conf){


# Check conf parameter in teams_list$Conf names
if(!(conf %in% hoopR::teams_links$Conf)){
if (!(conf %in% hoopR::teams_links$Conf)) {
cli::cli_abort("Incorrect conference name as compared to the website, see hoopR::teams_links for conference name parameter specifications.")
}
conf_name = unique(hoopR::teams_links$conf.link.ref[hoopR::teams_links$Conf == conf])
Expand All @@ -1247,7 +1247,7 @@ kp_confhistory <- function(conf){
Sys.sleep(5)
x <- page %>%
xml2::read_html() %>%
rvest::html_elements(css="#player-table")
rvest::html_elements(css = "#player-table")

## removing national rankings for easier manipulation
## TO-DO: Add these rankings back as columns
Expand Down
6 changes: 3 additions & 3 deletions R/kp_misc_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ kp_officials <- function(year = most_recent_mbb_season()){
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)) {
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")
}
Expand Down Expand Up @@ -205,7 +205,7 @@ kp_referee <- function(referee, year){
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)) {
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")
}
Expand Down Expand Up @@ -366,7 +366,7 @@ kp_arenas <- function(year=most_recent_mbb_season()){
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>=2010)) {
if (!(is.numeric(year) && nchar(year) == 4 && year >= 2010)) {
# Check if year is numeric, if not NULL
cli::cli_abort("Enter valid year as a number (YYYY), data only goes back to 2010")
}
Expand Down
Loading

0 comments on commit 1870358

Please sign in to comment.