Skip to content

Fix report() crash when character vector has only one unique value - #579

Merged
strengejacke merged 8 commits into
mainfrom
copilot/fix-f0d5d979-3a40-47d4-8f54-386410829563
Apr 29, 2026
Merged

Fix report() crash when character vector has only one unique value#579
strengejacke merged 8 commits into
mainfrom
copilot/fix-f0d5d979-3a40-47d4-8f54-386410829563

Conversation

Copilot AI commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

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") or c("big", "big", "big")), the function would fail with:

Error in names(n_char) <- c("Entry", "n_Entry") : 
'names' attribute [2] must be the same length as the vector [1]

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() where as.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() and report_statistics.character() would generate NA values when attempting to slice param_text[1:n_entries] with more entries requested than available.

Changes Made

  1. Fixed table conversion in report_table.character(): Changed from as.data.frame(sort(table(x), decreasing = TRUE)) to a two-step process that ensures proper column structure:

    n_char <- as.data.frame(table(x))
    n_char <- n_char[order(n_char$Freq, decreasing = TRUE), ]
  2. Fixed indexing in report_parameters.character() and report_statistics.character(): Added bounds checking to prevent NA values when n_entries exceeds the number of unique values:

    n_entries_actual <- min(n_entries, length(param_text))

Examples

The fix now works correctly for all cases:

library(report)

# Single unique value in a data frame (original issue)
df <- data.frame(id = 1:6, size = "big")
report(df)
#> The data contains 6 observations of the following 2 variables:
#>   - id: n = 6, Mean = 3.50, SD = 1.87, ...
#>   - size: 1 entry, such as big (n = 6) (0 missing)

# Single character value
report(c("big"))
#> x: 1 entry, such as big (n = 1) (0 missing)

# Single unique value repeated multiple times
report(c("big", "big", "big", "big"))
#> x: 1 entry, such as big (n = 4) (0 missing)

Testing

Added comprehensive test coverage in test-report_basic_methods.R for:

  • Single unique character value
  • Single unique value repeated multiple times
  • Single unique value in data frame context

All existing tests continue to pass.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: report fails when a character vector has only one unique value</issue_title>
<issue_description>### Describe the bug

report fails when a character vector has only one unique value with

Error in names(n_char) <- c("Entry", "n_Entry") :
'names' attribute [2] must be the same length as the vector [1]

Reproducible example

library(report)
packageVersion("report")
single_char_vector <- c("big")
df <- data.frame("id" = 1:6, "size" = single_char_vector)
report(df)

Session Info

R version 4.5.1 (2025-06-13 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
LAPACK version 3.12.1

locale:

[1] LC_COLLATE=Welsh_United Kingdom.utf8 LC_CTYPE=Welsh_United Kingdom.utf8
[3] LC_MONETARY=Welsh_United Kingdom.utf8 LC_NUMERIC=C
[5] LC_TIME=Welsh_United Kingdom.utf8

time zone: Europe/London
tzcode source: internal

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] report_0.6.1

loaded via a namespace (and not attached):
[1] compiler_4.5.1 tools_4.5.1 datawizard_1.2.0 rstudioapi_0.17.1 insight_1.4.2 </issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #578

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 1, 2025 18:31
Co-authored-by: rempsyc <13123390+rempsyc@users.noreply.github.com>
Co-authored-by: rempsyc <13123390+rempsyc@users.noreply.github.com>
Copilot AI changed the title [WIP] [Bug]: report fails when a character vector has only one unique value Fix report() crash when character vector has only one unique value Oct 1, 2025
Copilot AI requested a review from rempsyc October 1, 2025 18:36
@strengejacke

Copy link
Copy Markdown
Member

@gemini-code-assist review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread R/report.character.R
Comment thread R/report.character.R
@strengejacke
strengejacke marked this pull request as ready for review April 29, 2026 09:04
Copilot AI review requested due to automatic review settings April 29, 2026 09:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() and report_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.

Comment thread tests/testthat/test-report_basic_methods.R Outdated
Comment thread tests/testthat/test-report_basic_methods.R
strengejacke and others added 2 commits April 29, 2026 11:35
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@strengejacke
strengejacke merged commit 3eedea4 into main Apr 29, 2026
25 of 27 checks passed
@strengejacke
strengejacke deleted the copilot/fix-f0d5d979-3a40-47d4-8f54-386410829563 branch April 29, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: report fails when a character vector has only one unique value

4 participants