Fix report() crash when character vector has only one unique value - #579
Conversation
Co-authored-by: rempsyc <13123390+rempsyc@users.noreply.github.com>
Co-authored-by: rempsyc <13123390+rempsyc@users.noreply.github.com>
|
@gemini-code-assist review |
There was a problem hiding this comment.
Code Review
This pull request improves the handling of character vectors in reporting functions by refining table sorting and implementing bounds checking for the number of entries displayed in parameters and statistics. It also introduces new test cases for single unique values. Review feedback correctly identifies a potential crash when the 'n_entries' argument is set to 'all', as the current implementation uses 'min()' which cannot handle string inputs; a conditional check is recommended to maintain compatibility with existing package logic.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Fixes a crash in report() when summarizing character vectors (or character columns in data frames) that contain only a single unique value, and prevents NA-filled summaries when more entries are requested than exist.
Changes:
- Adjusted
report_table.character()to build a stable two-column frequency table even when only one unique value is present. - Added bounds checking in
report_parameters.character()andreport_statistics.character()to avoid out-of-range slicing. - Added regression tests covering single-unique character vectors and data frame context.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
R/report.character.R |
Fixes the single-unique-value table construction and prevents out-of-bounds summary slicing. |
tests/testthat/test-report_basic_methods.R |
Adds regression tests for the single-unique character case and data frame scenario. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Description
Fixes a bug where
report()would crash with an error when processing a character vector containing only one unique value.The Problem
When calling
report()on a character vector with a single unique value (e.g.,c("big")orc("big", "big", "big")), the function would fail with:This also affected data frames containing such character columns, preventing analysis of datasets where categorical variables had only one level.
Root Cause
The issue was in
report_table.character()whereas.data.frame(sort(table(x), decreasing = TRUE))creates a data frame with only 1 column (instead of the expected 2) when the table has a single unique value. Additionally,report_parameters.character()andreport_statistics.character()would generate NA values when attempting to sliceparam_text[1:n_entries]with more entries requested than available.Changes Made
Fixed table conversion in
report_table.character(): Changed fromas.data.frame(sort(table(x), decreasing = TRUE))to a two-step process that ensures proper column structure:Fixed indexing in
report_parameters.character()andreport_statistics.character(): Added bounds checking to prevent NA values whenn_entriesexceeds the number of unique values:Examples
The fix now works correctly for all cases:
Testing
Added comprehensive test coverage in
test-report_basic_methods.Rfor:All existing tests continue to pass.
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.