sportsdataverse / cfbfastR

An R package to quickly obtain clean and tidy college football play by play data
https://cfbfastR.sportsdataverse.org
Other
74 stars 8 forks source link

Change object returned for empty cfbd_stats_game_advanced response #92

Open titaniumtroop opened 1 year ago

titaniumtroop commented 1 year ago

When you call cfbd_stats_game_advanced() with a valid value but receive an empty response (e.g., no games yet for 2023), the return value is an empty list. A valid response to the cfbd_stats_game_advanced returns a cfbfastR_data object. For example:

tibble(year = 2021:2023) %>%
  mutate(stats = map(year, ~ cfbd_stats_game_advanced(year = .x, season_type = "both")))
2023-04-08 12:49:21:Invalid arguments or no game advanced stats data available!
# A tibble: 3 × 2
   year stats                  
  <int> <list>                 
1  2021 <cfbfstR_ [1,692 × 60]>
2  2022 <cfbfstR_ [2,842 × 60]>
3  2023 <list [0]>   

This function does throw a warning -- but in returning objects of different types, it can cause errors in downstream processing that expects objects of the same type. For example, trying to unnest the result above generates a type conflict error:

> tibble(year = 2021:2023) %>%
+     mutate(stats = map(year, ~ cfbd_stats_game_advanced(year = .x, season_type = "both"))) %>%
+     unnest(stats)
2023-04-08 12:52:50:Invalid arguments or no game advanced stats data available!
Error in `list_unchop()`:
! Can't combine `x[[1]]` <tbl_df> and `x[[3]]` <list>.

Expected behavior The function should either throw an error when no stats are available, or it should return the same object type as a non-empty response (i.e., cfbfastR_data). I suggest returning an empty cfbfastR_data object -- a well-formed query about a valid year (e.g., the current year) in which no games happen to have been played yet should be empty, not an error. In the example above, the correct response gets returned (stats for games in two of the three seasons specified, and none for the last) but the object type conflict prevents their combination in later processing. Expected behavior would look something like this:

tibble(year = 2021:2023) %>%
  mutate(stats = map(year, ~ cfbd_stats_game_advanced(year = .x, season_type = "both")))
2023-04-08 12:49:21: Invalid arguments or no game advanced stats data available!
# A tibble: 3 × 2
   year stats                  
  <int> <list>                 
1  2021 <cfbfstR_ [1,692 × 60]>
2  2022 <cfbfstR_ [2,842 × 60]>
3  2023 <cfbfstR_ [0 × 60]>   
saiemgilani commented 1 year ago

Fair comment, and generally agree