fix: event_num typo, missing DB write in load_nba_team_box, uninitialized games in scoreboards#186
Conversation
…ninitialized games in scoreboards - R/nba_stats_pbp.R: typo even_num -> event_num in .players_on_court() fallback branch (lines 154-155). Correct column event_num is used at lines 99-100 in the same function; this branch missed the update. Triggers when a player is subbed on and off at the exact same clock time in the same period (V2 path, nba_pbp(..., version = "v2")). - R/load_nba.R: load_nba_team_box() accepted dbConnection/tablename parameters and set in_db <- TRUE but never applied the flag. Added the if (in_db) DBI::dbWriteTable(...) block matching the pattern already present in load_nba_pbp() and load_nba_player_box(). - R/nba_stats_scoreboard.R: nba_schedule(), nba_scoreboardv3(), and nba_todays_scoreboard() assigned games inside tryCatch expr but called return(games) outside it. On API error, games was undefined and the function crashed instead of returning gracefully. Added games <- list() before each tryCatch, matching the df_list <- list() pattern used by all other functions in the file.
|
Someone is attempting to deploy a commit to the sportsdataverse Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThese changes modify three R functions: adding conditional database writing to Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
R/nba_stats_scoreboard.R (1)
88-88: LGTM — preventsobject 'games' not foundon API error.Initializing
games <- list()before eachtryCatchensures the error path returns a valid (empty) value instead of crashing. One minor note: on the success path these functions return a tibble while the error path now returnslist()— the return type is inconsistent across branches. If callers downstream expect a data frame shape, considergames <- dplyr::tibble()instead. Non-blocking; the pattern matches existing sibling functions (df_list <- list()).Also applies to: 684-684, 837-837
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@R/nba_stats_scoreboard.R` at line 88, You initialized games <- list() to avoid "object 'games' not found" on API error, but that makes the error path return a list while the success path returns a tibble; change the initializer to dplyr::tibble() (or tibble::tibble()) so both success and error branches return the same data-frame-like type, and apply the same change to the other occurrences of games <- list() in the file (the similar blocks around the other tryCatch usages).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@R/nba_stats_scoreboard.R`:
- Line 88: You initialized games <- list() to avoid "object 'games' not found"
on API error, but that makes the error path return a list while the success path
returns a tibble; change the initializer to dplyr::tibble() (or
tibble::tibble()) so both success and error branches return the same
data-frame-like type, and apply the same change to the other occurrences of
games <- list() in the file (the similar blocks around the other tryCatch
usages).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: db470937-c62e-491a-ac48-99b02c2a5f8c
📒 Files selected for processing (3)
R/load_nba.RR/nba_stats_pbp.RR/nba_stats_scoreboard.R
Summary
I'm a big fan of hoopR and use it extensively for my Wizards coverage at wizardspoints.substack.com. While working with the package I ran into a few bugs I've been meaning to address — fixing them here.
Changes
R/nba_stats_pbp.R(lines 154–155): Typoeven_num→event_numin the.players_on_court()fallback branch (V2 path). The correct column nameevent_numis used at lines 99–100 in the same function; this branch missed the update. Triggers when a player is subbed on and off at the exact same clock time in the same period.R/load_nba.R:load_nba_team_box()acceptsdbConnectionandtablenameparameters and setsin_db <- TRUE, but never applies the flag. Theif (in_db) DBI::dbWriteTable(...)block present inload_nba_pbp()andload_nba_player_box()was missing entirely. Callers passing a DB connection silently received a tibble instead of writing to the database.R/nba_stats_scoreboard.R:nba_scoreboardv3(),nba_schedule(), andnba_todays_scoreboard()assigngamesinside theirtryCatchexprblock but callreturn(games)outside it. If the API errors,gamesis never defined and the function crashes with "object 'games' not found" instead of returning gracefully. Addedgames <- list()before eachtryCatch, matching the pattern used by the deprecatednba_scoreboardv2()and all otherdf_list-returning functions in the file.Test plan
nba_pbp(game_id = "0022200021", version = "v2", on_court = TRUE)— confirm no column-not-found errorload_nba_team_box(seasons = 2023, dbConnection = con, tablename = "team_box")with a DBI connection — confirm data written to DBnba_scoreboardv3(),nba_schedule(),nba_todays_scoreboard()with an invalid date — confirm gracefullist()return instead of crashdevtools::check()— 0 new errors or warningsSummary by CodeRabbit
New Features
Bug Fixes