Fix mixed column type error when combining paginated pages (DP-1547) - #26
Conversation
When per-page type_convert() infers different types for the same column across pages (e.g. <double> on pages with numeric Post Code values, <character> on pages where the field is blank), dplyr::bind_rows() would throw "Can't combine <double> and <character>". Fix: coerce all columns to character before bind_rows(), then re-run readr::type_convert() on the full combined dataset so types are inferred from all values at once rather than page-by-page. This also preserves the existing start_time/end_time exclusion and guess_col_type = FALSE path. Adds two regression tests that reproduce the exact error and verify the fix. Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
📝 WalkthroughWalkthrough
ChangesPaginated page combining type-coercion fix
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/testthat/test-export_handler.R (1)
102-118: ⚡ Quick winAdd an assertion for metadata ID types in the
guess_col_type = FALSEtest.This test currently validates only
Post Code. Please also assertuser_idremains numeric to guard the documented metadata-column contract in paginated mode.Suggested assertion
expect_s3_class(result, "data.frame") expect_equal(nrow(result), 4L) # Without re-typing, column stays character (coerced from double then not re-converted) expect_type(result$`Post Code`, "character") + expect_type(result$user_id, "double")🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/testthat/test-export_handler.R` around lines 102 - 118, The test for `.combine_paginated_pages() with guess_col_type = FALSE` currently only validates that the `Post Code` column remains character type, but it should also assert that metadata ID columns like `user_id` maintain their numeric type to verify the documented metadata-column contract in paginated mode. Add an additional expect_type assertion after the existing `Post Code` check to verify that result$user_id remains numeric, ensuring metadata columns are not affected by the guess_col_type setting.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@R/export_handler.R`:
- Around line 200-213: All columns in the combined data frame are coerced to
character type in the bind_rows operation (lines 200-205), but when
guess_col_type is FALSE, there is no restoration step to convert metadata ID
columns back to numeric type, violating the documented output contract. After
the conditional block that handles type conversion when
isTRUE(arg$option$guess_col_type), add logic to ensure metadata ID columns are
converted back to numeric type regardless of the guess_col_type setting. This
can be done by identifying the ID columns (such as those ending in "_id" or
explicitly defined metadata ID column names) and using dplyr::mutate with
as.numeric to restore their proper types after the combined data frame is
finalized.
---
Nitpick comments:
In `@tests/testthat/test-export_handler.R`:
- Around line 102-118: The test for `.combine_paginated_pages() with
guess_col_type = FALSE` currently only validates that the `Post Code` column
remains character type, but it should also assert that metadata ID columns like
`user_id` maintain their numeric type to verify the documented metadata-column
contract in paginated mode. Add an additional expect_type assertion after the
existing `Post Code` check to verify that result$user_id remains numeric,
ensuring metadata columns are not affected by the guess_col_type setting.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: cb75c108-a1e9-404f-ba2f-2cf755e2d5cf
📒 Files selected for processing (2)
R/export_handler.Rtests/testthat/test-export_handler.R
The character coercion applied before bind_rows() left user_id, entered_by_user_id, and event_id as character when guess_col_type = FALSE, breaking the documented numeric output contract for those columns. Add an else branch that explicitly converts the known metadata ID columns back to numeric after combining, so the contract holds regardless of the guess_col_type setting. User form field columns are left as character (expected behaviour when guess_col_type = FALSE). Updates the regression test to assert user_id is numeric in this path. Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Summary
Relates to
DP-1547 (NSWIS — mixed column types when combining paginated pages)
Test plan
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests