insightsengineering / insightsengineering/crane
add_forest() flextable overflows the page in docx output
- Dominant language
- R
- Stars
- 15
- Forks
- 7
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 5
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
```r
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.
Contributor guide
Assessment
This issue has not been assessed yet.