FantasyFootballAnalytics / ffanalytics

ffanalytics R package
http://ffanalytics.fantasyfootballanalytics.net/
163 stars 100 forks source link

add_aav() in combination with add_uncertainty() breaking #150

Closed JamesWWilson closed 2 months ago

JamesWWilson commented 3 months ago

Issue occurs running the following code with stack trace. Only occurs when add_aav() present, otherwise when omitted code runs fine.

Can provide additional details on the details that go into scrape_data.

main_roster_scrape <- scrape_data(pos = positions, season = season_choice, week = 0)
main_projections <- projections_table(main_roster_scrape, scoring_rules = sleeper_league_half_ppr_points) 

main_projections <- main_projections %>% 
  add_ecr() %>% 
  add_adp() %>% 
  add_aav() %>%
  add_uncertainty() 

Scraping ECR data (w/ 2 second delay between pages if not cached)
Scraping ADP data
Scraping AAV Data
Error in `httr2::resp_body_json()`:
! Unexpected content type "text/html".
• Expecting type "application/json" or suffix "json".
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
<error/rlang_error>
Error in `httr2::resp_body_json()`:
! Unexpected content type "text/html".
• Expecting type "application/json" or suffix "json".
---
Backtrace:
     ▆
  1. ├─... %>% add_uncertainty()
  2. ├─ffanalytics::add_uncertainty(.)
  3. ├─ffanalytics::add_aav(.)
  4. │ └─ffanalytics::get_adp(sources, metric = "aav")
  5. │   └─base::lapply(...)
  6. │     └─ffanalytics (local) FUN(X[[i]], ...)
  7. │       └─match.fun(paste0(source, "_draft"))(metric = metric)
  8. │         └─base::lapply(...)
  9. │           └─ffanalytics (local) FUN(X[[i]], ...)
 10. │             └─... %>% base::`[[`("players")
 11. └─httr2::resp_body_json(.)
Run rlang::last_trace(drop = FALSE) to see 4 hidden frames.
atungate commented 3 months ago

Hey James, when I run the following example I am not getting errors

library(ffanalytics)
library(dplyr)

main_roster_scrape <- scrape_data(
  src = c("NFL", "ESPN", "CBS"),
  pos = c("QB", "RB", "WR", "TE"), 
  season = 2024, 
  week = 0
  )

sleeper_league_half_ppr_points = scoring
sleeper_league_half_ppr_points$rec$rec = .5

proj_table_sleeper_halfppr = main_roster_scrape %>%
  projections_table(scoring_rules = sleeper_league_half_ppr_points) %>%
  add_ecr() %>% 
  add_adp() %>% 
  add_aav() %>%
  add_uncertainty() 

Judging by your error, it is possible this was patched with https://github.com/FantasyFootballAnalytics/ffanalytics/commit/06603cab6d2b8d452b548d846e05d3f473a78bb4. Do you have the latest version of the package / can you try to restart R, and update the package? I.e.,

remotes::install_github("FantasyFootballAnalytics/ffanalytics")
JamesWWilson commented 3 months ago

Thank you for the response. Even after the above update and a forced update of all packages, I still see the error occur with the provided code block above. It appears to be related to possible html issues. For context, here is my session Info:

R version 4.3.1 (2023-06-16) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Sonoma 14.4.1

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles tzcode source: internal

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] remotes_2.4.2.1 stringr_1.5.1 ggplot2_3.5.0 openxlsx_4.2.5.2 tidyr_1.3.1 readr_2.1.5
[7] dplyr_1.1.4 googlesheets4_1.1.1 ffanalytics_3.1.2.0003

loaded via a namespace (and not attached): [1] rappdirs_0.3.3 utf8_1.2.4 generics_0.1.3 xml2_1.3.6 stringi_1.8.4 hms_1.1.3 digest_0.6.34
[8] magrittr_2.0.3 evaluate_0.23 grid_4.3.1 fastmap_1.1.1 cellranger_1.1.0 jsonlite_1.8.8 zip_2.3.1
[15] rematch2_2.1.2 promises_1.2.1 googledrive_2.1.1 httr_1.4.7 rvest_1.0.4 purrr_1.0.2 fansi_1.0.6
[22] selectr_0.4-2 scales_1.3.0 httr2_1.0.1 cli_3.6.3 crayon_1.5.3 rlang_1.1.4 rrapply_1.2.7
[29] munsell_0.5.0 withr_3.0.1 yaml_2.3.8 tools_4.3.1 tzdb_0.4.0 gargle_1.5.2 colorspace_2.1-0 [36] httpuv_1.6.14 curl_5.2.1 vctrs_0.6.5 R6_2.5.1 lifecycle_1.0.4 fs_1.6.3 pkgconfig_2.0.3
[43] later_1.3.2 pillar_1.9.0 gtable_0.3.4 data.table_1.15.4 glue_1.7.0 Rcpp_1.0.12 xfun_0.41
[50] tibble_3.2.1 tidyselect_1.2.1 rstudioapi_0.15.0 knitr_1.45 htmltools_0.5.7 rmarkdown_2.25 compiler_4.3.1
[57] askpass_1.2.0 readxl_1.4.3 openssl_2.2.1

And resulting error:

rlang::last_trace(drop = FALSE) <error/rlang_error> Error in httr2::resp_body_json(): ! Unexpected content type "text/html". • Expecting type "application/json" or suffix "json".

Backtrace: `

  1. ├─base::source("~/.active-rstudio-document", echo = TRUE)
  2. │ ├─base::withVisible(eval(ei, envir))
  3. │ └─base::eval(ei, envir)
  4. │ └─base::eval(ei, envir)
  5. ├─... %>% add_uncertainty() at ~/.active-rstudio-document:62:1
  6. ├─ffanalytics::add_uncertainty(.)
  7. ├─ffanalytics::add_aav(.)
  8. │ └─ffanalytics::get_adp(sources, metric = "aav")
  9. │ └─base::lapply(...)
    1. │ └─ffanalytics (local) FUN(X[[i]], ...)
    2. │ └─match.fun(paste0(source, "_draft"))(metric = metric)
    3. │ └─base::lapply(...)
    4. │ └─ffanalytics (local) FUN(X[[i]], ...)
    5. │ └─... %>% base::[[("players")
    6. └─httr2::resp_body_json(.)
    7. └─httr2::resp_check_content_type(...)
    8. └─httr2:::check_content_type(...)
    9. └─cli::cli_abort(...)
    10. └─rlang::abort(...) `
atungate commented 3 months ago

Thanks for sharing the session info--our package versions line-up for all the packages that would be involved in this (and we both are using a Mac). Strange. Can you run the following and let me know if the error message is any clearer?

library(ffanalytics)

list_ffanalytics_cache(quiet = TRUE)

clear_ffanalytics_cache()
get_adp(metric = "aav")

In the past we had this issue with ESPN ADP/AAV data but I am having trouble replicating it. Let me know what comes through when you run the above code and we'll go from there.

JamesWWilson commented 2 months ago

In a very confusing manner (maybe due to a package update or a source now querying correctly), the issue no longer appears to occur with the code provided below. I haven't made any significant changes from before. Worth monitoring again for next season!

rm(list = ls())
library(ffanalytics)
library(dplyr)

main_roster_scrape <- scrape_data(
  # src = c("NFL", "ESPN", "CBS"), 
  pos = c("QB","RB","WR","TE","K","DST"),
  season = 2024, 
  week = 0
)

sleeper_league_half_ppr_points = list(
  pass = list(
    pass_att = 0, pass_comp = 0, pass_inc = 0, pass_yds = 0.04, pass_tds = 4,
    pass_int = -2, pass_40_yds = 2, pass_300_yds = 2, pass_350_yds = 2,
    pass_400_yds = 2 #pass_int = -2
  ),
  rush = list(
    all_pos = TRUE,
    rush_yds = 0.1,  rush_att = 0, rush_40_yds = 1, rush_tds = 6,
    rush_100_yds = 2, rush_150_yds = 2, rush_200_yds = 2),
  rec = list(
    all_pos = TRUE,
    rec = 0.5, rec_yds = 0.1, rec_tds = 6, rec_40_yds = 1, rec_100_yds = 2,
    rec_150_yds = 2, rec_200_yds = 2
  ),
  misc = list(
    all_pos = TRUE,
    fumbles_lost = -2, fumbles_total = 0,
    sacks = 0, two_pts = 2
  ),
  kick = list(
    xp = 1.0, fg_0019 = 3.0,  fg_2029 = 3.0, fg_3039 = 3.0, fg_4049 = 4.0,
    fg_50 = 5.0,  fg_miss = -2.5
  ),
  ret = list(
    all_pos = TRUE,
    return_tds = 6, return_yds = 0
  ),
  idp = list(
    all_pos = TRUE,
    idp_solo = 1, idp_asst = 0.5, idp_sack = 2, idp_int = 3,  idp_fum_force = 3,
    idp_fum_rec = 2,  idp_pd = 1, idp_td = 6,  idp_safety = 2
  ),
  dst = list(
    dst_fum_rec = 2,  dst_int = 2, dst_safety = 2, dst_sacks = 1, dst_td = 6,
    dst_blk = 3, dst_ret_yds = 0, dst_pts_allowed = 0
  ),
  pts_bracket = list(
    list(threshold = 0, points = 10),
    list(threshold = 6, points = 7),
    list(threshold = 13, points = 4),
    list(threshold = 20, points = 1),
    list(threshold = 34, points = -1),
    list(threshold = 99, points = -4)
  )
)

sleeper_league_half_ppr_points = scoring
sleeper_league_half_ppr_points$rec$rec = .5

proj_table_sleeper_halfppr = main_roster_scrape %>%
  projections_table(scoring_rules = sleeper_league_half_ppr_points) %>%
  add_ecr() %>% 
  add_adp()  %>% 
  add_aav() %>%
  add_uncertainty()