Skip to content

Fix dropped second-level strata headers in tbl_strata_nested_stack() (#2418)#2419

Merged
ddsjoberg merged 7 commits into
mainfrom
fix/2418-nested-stack-headers
Jul 7, 2026
Merged

Fix dropped second-level strata headers in tbl_strata_nested_stack() (#2418)#2419
ddsjoberg merged 7 commits into
mainfrom
fix/2418-nested-stack-headers

Conversation

@ddsjoberg

@ddsjoberg ddsjoberg commented Jun 26, 2026

Copy link
Copy Markdown
Owner

What changes are proposed in this pull request?

Fixed bug in tbl_strata_nested_stack() where second-level strata headers were dropped in all but the first group when using three or more strata levels. (#2418)

If there is an GitHub issue associated with this pull request, please provide link.

closes #2418


Details

The header-deduplication loop NA'd out repeated strata values in place while later iterations still grouped on those same columns. Once an outer level was NA'd, distinct groups across different outer-strata sections collapsed together (e.g. all (NA, "C") rows from different first-level sections merged into one group), so the "first row" logic dropped valid second-level headers in every first-level group except the first.

The fix computes the first-occurrence masks for each level against the original strata values before applying any NAs, so inner-level grouping is no longer corrupted by NAs introduced for outer levels.

Verification

  • Added a regression test in tests/testthat/test-tbl_strata_nested_stack.R covering the 3-level strata case from the issue. The test fails on the previous code and passes with the fix.
  • All existing tests in test-tbl_strata_nested_stack.R continue to pass.

Reviewer Checklist (if item does not apply, mark is as complete)

  • PR branch has pulled the most recent updates from main branch.
  • If a bug was fixed, a unit test was added.
  • Run pkgdown::build_site(). Check the R console for errors, and review the rendered website.
  • Code coverage is suitable for any new functions/features: devtools::test_coverage()
  • usethis::use_spell_check() runs with no spelling errors in documentation
  • All GitHub Action workflows pass with a ✅

When the branch is ready to be merged into master:

  • Update NEWS.md with the changes from this pull request under the heading "# gtsummary (development version)". If there is an issue associated with the pull request, reference it in parentheses at the end update (see NEWS.md for examples).
  • Increment the version number using usethis::use_version(which = "dev")
  • Run usethis::use_spell_check() again
  • Approve Pull Request
  • Merge the PR. Please use "Squash and merge".

…2418)

The header-deduplication loop NA'd out repeated strata values in place while
later iterations still grouped on those same columns. Once an outer level was
NA'd, distinct groups across different outer-strata sections collapsed together,
causing valid second-level headers to be dropped in all but the first group when
using three or more strata levels.

Compute the first-occurrence masks for each level against the original strata
values before applying any NAs, so inner-level grouping is unaffected by NAs
introduced for outer levels.

Co-authored-by: Ona <no-reply@ona.com>
@ddsjoberg ddsjoberg merged commit 6c0f6a4 into main Jul 7, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: tbl_strata_nested_stack() drops second-level strata headers in non-first first-level groups (3+ levels)

1 participant