Skip to content

Use autofit layout for add_forest() flextable#271

Open
Melkiades wants to merge 3 commits into
mainfrom
270_add_forest_autofit@main
Open

Use autofit layout for add_forest() flextable#271
Melkiades wants to merge 3 commits into
mainfrom
270_add_forest_autofit@main

Conversation

@Melkiades

Copy link
Copy Markdown
Contributor

What changes are proposed in this pull request?

The flextable returned by add_forest() was wider than the page and kept a fixed layout, so saving to .docx rendered the exact column widths and the table ran off the right edge (the forest plots became unreadable). The images themselves were sized correctly; only the table layout needed to change. Setting layout = "autofit" lets Word reflow the table while the plot column keeps its 2.5in x 0.4in size.

Related to #150 (fixed vs autofit layout discussion).

Reference GitHub issue associated with pull request. closes #270


Pre-review Checklist (if item does not apply, mark is as complete)

  • All GitHub Action workflows pass with a ✅
  • PR branch has pulled the most recent updates from master branch: usethis::pr_merge_main()
  • If a bug was fixed, a unit test was added.
  • Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): devtools::test_coverage()
  • Request a reviewer

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

  • 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()

When the branch is ready to be merged:

  • Update NEWS.md with the changes from this pull request under the heading "# cards (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).
  • All GitHub Action workflows pass with a ✅
  • Approve Pull Request
  • Merge the PR. Please use "Squash and merge" or "Rebase and merge".

A wide forest table kept a fixed layout and spilled off the page when
saved to docx. Setting the flextable layout to autofit lets Word reflow
the table to the page width while the plot column keeps its size.
@Melkiades Melkiades marked this pull request as ready for review June 16, 2026 14:43
@github-actions

github-actions Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Unit Tests Summary

  1 files  247 suites   2m 46s ⏱️
247 tests 247 ✅ 0 💤 0 ❌
726 runs  726 ✅ 0 💤 0 ❌

Results for commit 43fa83b.

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Unit Test Performance Difference

Additional test case details
Test Suite $Status$ Time on main $±Time$ Test Case
add_grade_column 💚 $19.99$ $-2.14$ add_grade_column_works_on_a_standalone_tbl_hierarchical_rate_by_grade
gg_km 💚 $1.07$ $-1.03$ gg_km_works_and_handles_annotations_correctly
tbl_baseline_chg 💚 $31.00$ $-19.55$ tbl_baseline_chg_works
tbl_hierarchical_incidence_rate 💀 $0.00$ $-0.00$ add_overall_accepts_a_custom_col_label
tbl_hierarchical_incidence_rate 💀 $0.00$ $-0.00$ add_overall_appends_overall_columns
tbl_hierarchical_incidence_rate 💀 $0.00$ $-0.00$ add_overall_on_table_without_by_returns_unaltered
tbl_hierarchical_incidence_rate 💀 $0.00$ $-0.00$ add_overall_with_last_TRUE_places_overall_columns_at_the_end
tbl_hierarchical_incidence_rate 💀 $0.00$ $-0.00$ gather_ard_works_after_add_overall
tbl_hierarchical_incidence_rate 💀 $0.00$ $-0.00$ overall_row_FALSE_omits_the_overall_summary_row
tbl_hierarchical_incidence_rate 💀 $0.00$ $-0.00$ spanning_headers_are_applied_for_treatment_arms
tbl_hierarchical_incidence_rate 💀 $0.00$ $-0.00$ spanning_label_glue_template_customizes_treatment_arm_headers
tbl_hierarchical_incidence_rate 💔 $4.31$ $+13.14$ tbl_hierarchical_incidence_rate_runs_with_minimum_arguments
tbl_hierarchical_rate_and_count 💚 $24.65$ $-12.74$ tbl_hierarchical_rate_and_count_works
tbl_hierarchical_rate_by_grade 💔 $3.41$ $+13.89$ tbl_hierarchical_rate_by_grade_works
tbl_rmpt 💚 $15.65$ $-1.02$ tbl_rmpt_works_with_default_parameters_Example_1_
tbl_roche_subgroups 💚 $23.60$ $-18.97$ tbl_roche_subgroups_time_to_event_NULL_works
tbl_roche_summary 💚 $3.47$ $-1.28$ tbl_roche_summary_works
tbl_shift 💔 $5.85$ $+1.94$ tbl_shift_strata_location_
tbl_survfit_quantiles 💀 $0.00$ $-0.00$ tbl_survfit_quantiles_censoring_asterisks_logic
tbl_survfit_quantiles 💚 $7.43$ $-2.68$ tbl_survfit_quantiles_works
tbl_with_pools 💚 $12.93$ $-3.80$ tbl_with_pools_validates_inputs_correctly

Results for commit 36e6f07

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

badge

Code Coverage Summary

Filename                                 Stmts    Miss  Cover    Missing
-------------------------------------  -------  ------  -------  ------------------------------------------------------------------------------------------------
R/add_blank_rows.R                          63       0  100.00%
R/add_difference_row.R                     101       0  100.00%
R/add_forest_utils.R                        97      10  89.69%   76-79, 94-100
R/add_forest.R                             143       0  100.00%
R/add_hierarchical_count_row.R              33       0  100.00%
R/adjust_stat_columns_wrap.R                29       1  96.55%   59
R/annotate_gg_km.R                         135       0  100.00%
R/annotate_gg_pkc.R                         92       0  100.00%
R/annotate_gg.R                             81       0  100.00%
R/ard_tabulate_abnormal_by_baseline.R       65       0  100.00%
R/crane-package.R                            2       2  0.00%    26-27
R/deprecated.R                              21      21  0.00%    18-54
R/df_add_poolings.R                         41       0  100.00%
R/get_cox_pairwise_df.R                    163       0  100.00%
R/gg_km_utils.R                             35      14  60.00%   20-37
R/gg_km.R                                  143      37  74.13%   55-58, 75, 102, 176-181, 184-187, 197-199, 204-205, 239-241, 248-251, 255, 266-270, 283, 285-287
R/gg_lineplot.R                             94       0  100.00%
R/gg_mmrm_lineplot.R                       102       1  99.02%   106
R/gg_pkc_lineplot.R                         98       0  100.00%
R/gg_utils.R                               221       0  100.00%
R/label_roche.R                             72       0  100.00%
R/modify_header_rm_md.R                     18       2  88.89%   35-36
R/modify_zero_recode.R                      20       1  95.00%   64
R/reverse_difference_ci.R                   33       0  100.00%
R/tbl_baseline_chg.R                       188       0  100.00%
R/tbl_coxph.R                               90       1  98.89%   229
R/tbl_hierarchical_incidence_rate.R        209       0  100.00%
R/tbl_hierarchical_rate_and_count.R        339      13  96.17%   343, 425, 446-456
R/tbl_hierarchical_rate_by_grade.R         317       3  99.05%   169-171
R/tbl_listing.R                             35       0  100.00%
R/tbl_mmrm.R                               254       1  99.61%   393
R/tbl_null_report.R                          9       0  100.00%
R/tbl_rmpt.R                               157      12  92.36%   299-304, 316-321
R/tbl_roche_subgroups.R                    155       0  100.00%
R/tbl_roche_summary.R                       64       0  100.00%
R/tbl_shift.R                              116       0  100.00%
R/tbl_survfit_quantiles.R                  132       1  99.24%   295
R/tbl_survfit_times.R                       92       0  100.00%
R/tbl_with_pools.R                          64       0  100.00%
R/theme_gtsummary_roche.R                   84       1  98.81%   61
R/utils.R                                   42       0  100.00%
TOTAL                                     4249     121  97.15%

Diff against main

Filename                               Stmts    Miss  Cover
-----------------------------------  -------  ------  --------
R/add_forest.R                            +4       0  +100.00%
R/tbl_hierarchical_incidence_rate.R      -82      -4  +1.37%
R/tbl_survfit_quantiles.R                -22      +1  -0.76%
TOTAL                                   -100      -3  +0.00%

Results for commit: 43fa83b

Minimum allowed coverage is 80%

♻️ This comment has been updated with latest results

@Melkiades

Copy link
Copy Markdown
Contributor Author

Did UAT. This does not solve the issue as it was already done from the theme. I need to understand better how to have the vertical lines connecting and the nanoplots be represented the same in html (native output from print) and docx export

`layout = "autofit"` alone emits tblW = 0% (Word "AutoFit to Contents"),
which sizes columns to their natural width; the forest table (fixed 2.5in
plot column plus non-wrapping stat columns, ~11.8in total) still overflows
the page. Pair it with width = 1 to emit tblW = 100% ("AutoFit to Window")
so Word scales the table to the page width while the plot images keep their
2.5in x 0.4in size.

Assert the 100% preferred width in tests, and add autofit/docx/reflow to the
spelling WORDLIST.
The forest-plot column has a fixed width and holds an image rendered to
exactly that width. Under the theme's autofit layout Word sizes the column
to its content's natural width, and the default 5pt horizontal cell padding
pushes the image past the column, expanding the table until it overflows the
page in docx. Zero the plot column's horizontal padding so the image fits its
cell content box exactly; the plot proportions are unchanged.

This supersedes the earlier autofit/100%-width attempts, which did not fix
the overflow.
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.

add_forest() flextable overflows the page in docx output

1 participant