Fix report() crash when character vector has only one unique value #579
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.