Prerequisites
Make sure flextable is installed before you begin. If you haven’t installed it yet, see the installation guide.Create a basic table
Pass anydata.frame to flextable(). The function creates a table with a header, body, and (empty) footer.
flextable() applies the theme_booktabs theme — horizontal rules above and below the header and at the bottom of the body, with text columns left-aligned and numeric columns right-aligned.
Apply a theme
Themes apply a consistent set of borders, colors, and text styles in one call. Apply a theme after you have added all header and footer rows, because themes only format elements that exist at the time they are called.| Theme | Description |
|---|---|
theme_vanilla() | Thick outer horizontal rules, thin inner rules, bold header |
theme_booktabs() | LaTeX-style booktabs rules, no vertical lines (default) |
theme_box() | Full grid with outer and inner borders |
theme_zebra() | Alternating row background colors |
theme_borderless() | No borders at all, bold header |
theme_apa() | American Psychological Association style, Times New Roman |
Format cells
Chain formatting verbs with the pipe operator. Each function acceptsi (row selector) and j (column selector) arguments to target specific cells.
Highlight specific cells
~ formula in i filters rows using the data’s column names — here, rows where mpg is less than 22.Add a footer line
Useadd_footer_lines() to append a note below the table body.
Save to Word
save_as_docx() writes one or more flextable objects to a .docx file.
Save to HTML
save_as_html() produces a self-contained HTML file. This is useful for sharing tables outside of an R Markdown workflow.
save_as_docx(), you can pass multiple named tables:
save_as_html() requires pandoc to be available. If you are working inside R Markdown or Quarto, the table is rendered automatically — no need to call save_as_html() explicitly.Put it all together
Here is a complete example combining everything above:Next steps
Table structure
Learn about header, body, and footer parts and how selectors work
Text formatting
Explore bold, italic, color, font size, and highlight functions
Themes
See all available themes with visual examples
Export & output
Save tables to Word, PowerPoint, PDF, and images