add_forest() flextable overflows the page in docx output

Open Beginner friendly
#270 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
r

Research direction

Start in the flextable branch of add_forest() and reproduce the reported .docx output using the example in the issue. Check that the table reflows within the page, the forest plots remain 2.5in x 0.4in, and all images are packaged correctly.

Written by the indexing model from the issue text.

Description

Description

add_forest(table_engine = "flextable") produces a flextable that is wider than the page and uses a fixed table layout, so when the table is saved to .docx Word renders the exact column widths and the table spills off the right edge. The forest plots become unreadable.

The images themselves are sized correctly (the ggplot column is 2.5in x 0.4in). The problem is the overall table width combined with the fixed layout.

Reproduction
library(crane)
library(gtsummary)

set.seed(1)
ft <- trial |>
  tbl_roche_subgroups(
    rsp = "response", by = "trt", subgroups = c("grade", "stage"),
    ~ glm(response ~ trt, data = .x) |>
      tbl_regression(show_single_row = trt, exponentiate = TRUE)
  ) |>
  add_forest(pvalue = starts_with("p.value"), table_engine = "flextable")

# saved to docx the table is ~11.8in wide on a 6.5in usable page

Measured on the rendered .docx:

Measure Value
Table width 11.79 in
Usable page width (Letter, 1in margins) 6.5 in
Overflow +5.29 in (181% of the page)
Table layout fixed

as_flex_table() already returns a fixed layout, and add_forest() keeps it fixed while widening the table, so Word cannot reflow it.

Proposed fix

Set layout = "autofit" at the end of the flextable branch of add_forest() so Word reflows the table to the page width. Verified: with autofit the table reflows, the plots stay at 2.5in x 0.4in, and all images package correctly.

Related

Related to #150 (fixed vs autofit layout for flextable). This issue is the concrete forest-plot symptom of that discussion.

Dominant language
R
Stars
15
Forks
7
Avg merge
1d 9h
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from insightsengineering/crane

All issues in insightsengineering/crane

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.