Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/davidgohel/flextable/llms.txt

Use this file to discover all available pages before exploring further.

This guide walks you through creating a basic table, applying a theme, formatting cells, and saving your output — all using real flextable functions.

Prerequisites

Make sure flextable is installed before you begin. If you haven’t installed it yet, see the installation guide.
library(flextable)

Create a basic table

Pass any data.frame to flextable(). The function creates a table with a header, body, and (empty) footer.
ft <- flextable(head(airquality))
ft
By default, 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.
Use head() or a small slice of your data while iterating, then swap in the full dataset when you are ready to export.

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.
ft <- flextable(head(airquality)) |>
  theme_vanilla()
ft
ThemeDescription
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 accepts i (row selector) and j (column selector) arguments to target specific cells.
1

Bold the header

ft <- flextable(head(mtcars)) |>
  bold(part = "header")
2

Change header text color

ft <- flextable(head(mtcars)) |>
  bold(part = "header") |>
  color(color = "#2B5797", part = "header")
3

Highlight specific cells

ft <- flextable(head(mtcars)) |>
  bold(part = "header") |>
  color(color = "#2B5797", part = "header") |>
  highlight(i = ~ mpg < 22, j = "mpg", color = "#ffe842")
The ~ formula in i filters rows using the data’s column names — here, rows where mpg is less than 22.
4

Fit column widths automatically

ft <- flextable(head(mtcars)) |>
  bold(part = "header") |>
  color(color = "#2B5797", part = "header") |>
  highlight(i = ~ mpg < 22, j = "mpg", color = "#ffe842") |>
  autofit()
autofit() adjusts each column’s width to fit its content.
Use add_footer_lines() to append a note below the table body.
ft <- flextable(head(mtcars)) |>
  theme_vanilla() |>
  add_footer_lines("Source: Motor Trend, 1974") |>
  autofit()

Save to Word

save_as_docx() writes one or more flextable objects to a .docx file.
ft <- flextable(head(mtcars)) |>
  theme_vanilla() |>
  autofit()

save_as_docx(ft, path = "my_table.docx")
To save multiple tables in one document, pass them as named arguments — the names become section headings:
ft1 <- flextable(head(iris)) |> theme_vanilla() |> autofit()
ft2 <- flextable(head(mtcars)) |> theme_vanilla() |> autofit()

save_as_docx(
  `Iris data` = ft1,
  `Motor Trend data` = ft2,
  path = "tables.docx"
)

Save to HTML

save_as_html() produces a self-contained HTML file. This is useful for sharing tables outside of an R Markdown workflow.
ft <- flextable(head(iris)) |>
  theme_vanilla() |>
  autofit()

save_as_html(ft, path = "my_table.html")
Like save_as_docx(), you can pass multiple named tables:
save_as_html(
  `Iris data` = ft1,
  `Motor Trend data` = ft2,
  path = "tables.html",
  title = "My 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:
library(flextable)

ft <- flextable(head(mtcars)) |>
  theme_vanilla() |>
  bold(part = "header") |>
  color(color = "#2B5797", part = "header") |>
  highlight(i = ~ mpg < 22, j = "mpg", color = "#ffe842") |>
  add_footer_lines("Source: Motor Trend, 1974") |>
  autofit()

# Save to Word
save_as_docx(ft, path = "mtcars_table.docx")

# Save to HTML
save_as_html(ft, path = "mtcars_table.html")

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

Build docs developers (and LLMs) love