Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

as_flex_table

Convert gtsummary object to a flextable object


Description

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.

Usage

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

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_flex_table() functions converts the gtsummary object to a flextable, and prints it with the following styling 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

  11. flextable::border() to set all border widths to 1

  12. flextable::padding() to set consistent header padding

  13. flextable::valign() to ensure label column is top-left justified

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_flex_table().

Example Output

Example 1

Author(s)

Daniel D. Sjoberg

See Also

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

Examples

as_flex_table_ex1 <-
  trial %>%
  select(trt, age, grade) %>%
  tbl_summary(by = trt) %>%
  add_p() %>%
  as_flex_table()

gtsummary

Presentation-Ready Data Summary and Analytic Result Tables

v1.4.0
MIT + file LICENSE
Authors
Daniel D. Sjoberg [aut, cre] (<https://orcid.org/0000-0003-0862-2018>), Michael Curry [aut] (<https://orcid.org/0000-0002-0261-4044>), Margie Hannum [aut] (<https://orcid.org/0000-0002-2953-0449>), Joseph Larmarange [aut] (<https://orcid.org/0000-0001-7097-700X>), Karissa Whiting [aut] (<https://orcid.org/0000-0002-4683-1868>), Emily C. Zabor [aut] (<https://orcid.org/0000-0002-1402-4498>), Esther Drill [ctb] (<https://orcid.org/0000-0002-3315-4538>), Jessica Flynn [ctb] (<https://orcid.org/0000-0001-8310-6684>), Jessica Lavery [ctb] (<https://orcid.org/0000-0002-2746-5647>), Stephanie Lobaugh [ctb], Gustavo Zapata Wainberg [ctb] (<https://orcid.org/0000-0002-2524-3637>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.