Experimental lifecycle Function converts a gtsummary object to a flextable object. A user can use this function if they wish to add customized formatting available via the flextable functions. The flextable output is particularly useful when combined with R markdown with Word output, since the gt package does not support Word.

as_flextable(x, ...)

# S3 method for gtsummary
as_flextable(
  x,
  include = everything(),
  return_calls = FALSE,
  strip_md_bold = TRUE,
  ...
)

Arguments

x

Object created by a function from the gtsummary package (e.g. tbl_summary or tbl_regression)

...

Not used

include

Commands to include in output. Input may be a vector of quoted or unquoted names. tidyselect and gtsummary select helper functions are also accepted. Default is everything().

return_calls

Logical. Default is FALSE. If TRUE, the calls are returned as a list of expressions.

strip_md_bold

When TRUE, all double asterisk (markdown language for bold weight) in column labels and spanning headers are removed. Default is TRUE

Value

A flextable object

Details

The as_flextable() takes the data frame that will be printed and converts it to a flextable and formats the table with the following flextable functions.

  1. flextable::flextable()

  2. flextable::set_header_labels() to set column labels

  3. flextable::add_header_row(), if applicable, to set spanning column header

  4. flextable::align() to set column alignment

  5. flextable::padding() to indent variable levels

  6. flextable::fontsize() to set font size

  7. flextable::autofit() to estimate the column widths

  8. flextable::footnote() to add table footnotes and source notes

  9. flextable::bold() to bold cells in data frame

  10. flextable::italic() to italicize cells in data frame

Any one of these commands may be omitted using the include= argument.

Pro tip: Use the flextable::width() function for exacting control over column width after calling as_flextable().

See also

Other gtsummary output types: as_gt(), as_kable_extra(), as_kable(), as_tibble.gtsummary()

Author

Daniel D. Sjoberg

Examples

trial %>% select(trt, age, grade) %>% tbl_summary(by = trt) %>% add_p() %>% as_flextable()
#> a flextable object. #> col_keys: `label`, `stat_1`, `stat_2`, `p.value` #> header has 1 row(s) #> body has 6 row(s) #> original dataset sample: #> label stat_1 stat_2 p.value #> 1 Age, yrs 46 (37, 59) 48 (39, 56) 0.7 #> 2 Unknown 7 4 <NA> #> 3 Grade <NA> <NA> 0.9 #> 4 I 35 (36%) 33 (32%) <NA> #> 5 II 32 (33%) 36 (35%) <NA>