From a9c35cbdd2281efdf38c85c650f8c405e62716de Mon Sep 17 00:00:00 2001 From: Tan Ho Date: Fri, 8 Mar 2024 22:01:17 -0500 Subject: [PATCH] v1.4.8.17 - mfl_/sleeper_ players return more standardized column types --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/mfl_players.R | 45 ++++++++++++++++++++++++++++-- R/sleeper_players.R | 68 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 110 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ddd47785..eeb1491f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: ffscrapr Title: API Client for Fantasy Football League Platforms -Version: 1.4.8.16 +Version: 1.4.8.17 Authors@R: c(person(given = "Tan", family = "Ho", diff --git a/NEWS.md b/NEWS.md index 2ed85219..edf43a19 100644 --- a/NEWS.md +++ b/NEWS.md @@ -27,6 +27,8 @@ row per player-team-season (v1.4.8.13) (thanks @john-b-edwards!) - Bugfix espn `ff_starters()` to return handle multi-week formats (v1.4.8.15) (#421) (h/t @tonyelhabr 🤠) - Bugfix espn `ff_starters()` to return less than or equal to max week (v1.4.8.16) +- [BREAKING] `mfl_players()` and `sleeper_players()` outputs now try to return +more standardized column types (v1.4.8.17) # ffscrapr 1.4.8 diff --git a/R/mfl_players.R b/R/mfl_players.R index ee527f9e..388d3d2b 100644 --- a/R/mfl_players.R +++ b/R/mfl_players.R @@ -26,17 +26,21 @@ mfl_players <- function(conn = NULL) { conn <- mfl_connect(.fn_choose_season()) } - conn %>% + df_players <- conn %>% mfl_getendpoint("players", DETAILS = 1) %>% purrr::pluck("content", "players", "player") %>% tibble::tibble() %>% tidyr::unnest_wider(1) %>% dplyr::mutate_at( - "birthdate", ~ as.numeric(.x) %>% + "birthdate", + ~ as.numeric(.x) %>% .as_datetime() %>% .as_date() ) %>% - dplyr::mutate("age" = round(as.numeric(Sys.Date() - .data$birthdate) / 365.25, 1)) %>% + dplyr::mutate( + age = round(as.numeric(Sys.Date() - .data$birthdate) / 365.25, 1), + draft_year = as.integer(.data$draft_year) + ) %>% dplyr::select( dplyr::any_of(c( "player_id" = "id", @@ -50,4 +54,39 @@ mfl_players <- function(conn = NULL) { dplyr::ends_with("_id"), dplyr::everything() ) + + df_players <- dplyr::bind_rows(.mfl_players_template(), df_players) + + return(df_players) +} + + +.mfl_players_template <- function(){ + tibble::tibble( + player_id = character(0), + player_name = character(0), + pos = character(0), + age = numeric(0), + team = character(0), + status = character(0), + draft_year = integer(0), + draft_team = character(0), + draft_round = character(0), + draft_pick = character(0), + stats_global_id = character(0), + fleaflicker_id = character(0), + stats_id = character(0), + cbs_id = character(0), + sportsdata_id = character(0), + rotowire_id = character(0), + rotoworld_id = character(0), + espn_id = character(0), + nfl_id = character(0), + jersey = character(0), + height = character(0), + weight = character(0), + college = character(0), + twitter_username = character(0), + birthdate = as.Date(character(0)) + ) } diff --git a/R/sleeper_players.R b/R/sleeper_players.R index cc8751ff..a4ea3f85 100644 --- a/R/sleeper_players.R +++ b/R/sleeper_players.R @@ -20,12 +20,26 @@ sleeper_players <- function() { tibble::tibble() %>% tidyr::unnest_wider(1) %>% dplyr::mutate_at("fantasy_positions", ~ purrr::map(.x, as.character) %>% as.character()) %>% - dplyr::mutate_at("birth_date", .as_date) %>% + dplyr::mutate_at(c("birth_date", "injury_start_date"), .as_date) %>% dplyr::mutate( age = round(as.numeric(Sys.Date() - .data$birth_date) / 365.25, 1), - gsis_id = stringr::str_squish(gsis_id) + gsis_id = stringr::str_squish(gsis_id), + weight = as.integer(weight) + ) %>% + dplyr::mutate_at( + c("injury_status", "injury_body_part", "injury_notes"), + as.character + ) %>% + dplyr::mutate_at( + dplyr::vars(dplyr::contains("_id")), + as.character + ) %>% + dplyr::select( + -dplyr::contains("search"), + -dplyr::contains("first_name"), + -dplyr::contains("last_name"), + -dplyr::contains("metadata") ) %>% - dplyr::select(-dplyr::contains("search"), -dplyr::contains("first_name"), -dplyr::contains("last_name"), -dplyr::contains("metadata")) %>% dplyr::select( "player_id", "player_name" = "full_name", @@ -39,5 +53,53 @@ sleeper_players <- function() { dplyr::everything() ) + df_players <- dplyr::bind_rows(.sleeper_players_template(), df_players) + return(df_players) } + +.sleeper_players_template <- function(){ + tibble::tibble( + player_id = character(0), + player_name = character(0), + pos = character(0), + age = numeric(0), + team = character(0), + status = character(0), + active = logical(0), + years_exp = integer(0), + swish_id = character(0), + espn_id = character(0), + fantasy_data_id = character(0), + pandascore_id = character(0), + rotowire_id = character(0), + sportradar_id = character(0), + yahoo_id = character(0), + oddsjam_id = character(0), + dl_trading_id = character(0), + rotoworld_id = character(0), + stats_id = character(0), + gsis_id = character(0), + height = character(0), + weight = integer(0), + fantasy_positions = character(0), + number = integer(0), + depth_chart_order = integer(0), + depth_chart_position = character(0), + practice_description = character(0), + birth_date = as.Date(character(0)), + birth_city = character(0), + birth_state = character(0), + birth_country = character(0), + high_school = character(0), + college = character(0), + practice_participation = character(0), + injury_status = character(0), + injury_start_date = as.Date(character(0)), + injury_body_part = character(0), + injury_notes = character(0), + news_updated = numeric(0), + hashtag = character(0), + sport = character(0) + ) +}