Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 1, 2025

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 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
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

2 participants