Skip to content

add_forest() flextable overflows the page in docx output #270

Description

@Melkiades

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions