From b2dc63aa7b34a4bb9c9eaccb60f68ea6fb95d189 Mon Sep 17 00:00:00 2001 From: Tan Ho Date: Thu, 27 Jul 2023 23:27:24 -0300 Subject: [PATCH] v1.4.8.04 - update ff_scoring to work with newest dplyr, resolves #402 --- DESCRIPTION | 2 +- NEWS.md | 1 + R/mfl_scoring.R | 22 ++++++++++++++-------- tests/testthat/test-ff_scoring.R | 3 +++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4e0c49e8..a75df9ec 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.03 +Version: 1.4.8.04 Authors@R: c(person(given = "Tan", family = "Ho", diff --git a/NEWS.md b/NEWS.md index 36c19730..e0db9b2a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,7 @@ should be more robust (#397, thank you @logan-888!) (v1.4.8.02) - `ff_startpositions.sleeper_conn()` now properly handles leagues with flex positions that did not have dedicated posititions (#400, thank you @mcarman8!) (v1.4.8.03) +- `ff_scoring.mfl_conn()` now works better with new dplyr versions (#402) (v1.4.8.04) # ffscrapr 1.4.8 diff --git a/R/mfl_scoring.R b/R/mfl_scoring.R index 56c3035f..bd3a5bf6 100644 --- a/R/mfl_scoring.R +++ b/R/mfl_scoring.R @@ -25,21 +25,27 @@ ff_scoring.mfl_conn <- function(conn) { df <- df %>% tibble::tibble() %>% tidyr::unnest_wider(1) + } else if (!is.null(df$positions)) { + df <- df %>% + tibble::as_tibble() } - if (!is.null(df$positions)) { - df <- df %>% tibble::as_tibble() + # convert each nested list of rules to tibble by bind_rows + # so that it can be unnested nicely + # see ffscrapr#344 for history + parse_rule <- function(rule){ + vec_depth <- purrr::vec_depth(rule) + if(vec_depth == 3) rule <- tibble::as_tibble(rule) + if(vec_depth == 4) rule <- dplyr::bind_rows(rule) + rule %>% + dplyr::mutate_all(~unlist(.x) %>% unname()) } - df <- df %>% dplyr::mutate( - # convert each nested list of rules to tibble by bind_rows - # so that it can be unnested nicely - # see ffscrapr#344 for history - rule = purrr::map(.data$rule, dplyr::bind_rows) + rule = purrr::map(.data$rule, parse_rule), ) %>% - tidyr::unnest("rule") %>% + tidyr::unnest_wider("rule") %>% tidyr::unnest(c("points", "event", "range")) %>% tidyr::separate_rows("positions", sep = "\\|") %>% dplyr::left_join(mfl_allrules(conn), by = c("event" = "abbrev")) %>% diff --git a/tests/testthat/test-ff_scoring.R b/tests/testthat/test-ff_scoring.R index 076fa787..9baf3e8b 100644 --- a/tests/testthat/test-ff_scoring.R +++ b/tests/testthat/test-ff_scoring.R @@ -3,6 +3,8 @@ test_that("ff_scoring returns a tibble", { dlf <- mfl_connect(2019, 37920) dlf_scoring <- ff_scoring(dlf) + wade <- mfl_connect(2023, 65804) + wade_scoring <- ff_scoring(wade) jml_conn <- sleeper_connect(league_id = "522458773317046272", season = 2020) jml_scoring <- ff_scoring(jml_conn) @@ -17,6 +19,7 @@ test_that("ff_scoring returns a tibble", { expect_tibble(jml_scoring, min.rows = 10) expect_tibble(joe_scoring, min.rows = 10) expect_tibble(tony_scoring, min.rows = 10) + expect_tibble(wade_scoring, min.rows = 10) }) test_that("ff_scoring for templates return tibbles", {