R flextable
Overview
flextable creates publication-quality tables that render consistently across Word, PowerPoint, HTML, and PDF.
Core principle: Separate data from display. Build from a data.frame, then layer formatting declaratively using selectors.
References
Read references/API.md before writing code.
references/API.md - Complete function reference
references/overview.md - Core concepts and workflow
references/formatting.md - Styling and appearance
references/layout.md - Structure and merging
references/selectors.md - Targeting cells for formatting
references/output.md - Rendering to different formats
When to Use
Use flextable when:
- Output to Word/PowerPoint required
- Need cell merging, conditional formatting, mini charts
- Cross-references and auto-numbered captions
- Same table across multiple formats
Consider alternatives:
- HTML-only: gt or reactable
- LaTeX-heavy: kableExtra
- Interactive dashboards: DT
Quick Reference
| Task |
Function |
| Create table |
flextable(df) |
| Format numbers |
colformat_double(), colformat_int() |
| Bold/italic/color |
bold(), italic(), color() |
| Background color |
bg() |
| Merge cells |
merge_v(), merge_h(), merge_at() |
| Add header |
add_header_row() |
| Auto-size |
autofit() |
| Apply theme |
theme_vanilla(), theme_zebra() |
| Save to Word |
save_as_docx() |
| Save to PowerPoint |
save_as_pptx() |
Core Pattern
library(flextable)
# Create -> format -> output
ft <- flextable(head(mtcars, 5)) |>
colformat_double(digits = 1) |>
# Conditional formatting with formula selectors
color(i = ~ mpg > 20, j = "mpg", color = "darkgreen") |>
bold(i = ~ mpg > 20, j = "mpg") |>
# Spanning header
add_header_row(
values = c("Performance", "Engine", "Transmission"),
colwidths = c(2, 4, 5)
) |>
theme_vanilla() |>
autofit()
save_as_docx(ft, path = "table.docx")
Selectors (Key Concept)
Target cells for formatting:
# Row selector (formula)
bold(ft, i = ~ cyl == 6)
# Column selector
bg(ft, j = ~ mpg + hp, bg = "lightgray")
# Part selector
bold(ft, part = "header")
See references/selectors.md for details on multi-content cells, mini charts, output-specific notes.
Common Mistakes
| Mistake |
Fix |
| Theme doesn't format new header |
Apply theme AFTER add_header_row() |
autofit() overflows margins |
Use set_table_properties(layout = "autofit") |
height() has no effect |
Set hrule(rule = "exact") first |
| Formula selectors fail in header |
Use integer indices for header rows |
| Images missing in Word |
Use officedown::rdocx_document() |
| PDF ignores padding |
Use ft.tabcolsep chunk option |
add_header_row values wrong |
One value per span; colwidths sum to ncol |
Advanced
See references/ for:
- API.md: Complete function reference (240+ functions)
- selectors.md: Multi-content cells, mini charts, output-specific notes
- formatting.md: All formatting functions and options
- layout.md: Merging, sizing, headers/footers
- output.md: Rendering to all formats
1---2name: r-flextable3description: Use when code loads or uses flextable (library(flextable), flextable::), creating formatted tables for Word (.docx), PowerPoint (.pptx), HTML, or PDF in R, or needing merged cells and conditional formatting4---56# R flextable78## Overview910**flextable creates publication-quality tables that render consistently across Word, PowerPoint, HTML, and PDF.**1112Core principle: Separate data from display. Build from a data.frame, then layer formatting declaratively using selectors.1314## References1516Read `references/API.md` before writing code.1718- `references/API.md` - Complete function reference19- `references/overview.md` - Core concepts and workflow20- `references/formatting.md` - Styling and appearance21- `references/layout.md` - Structure and merging22- `references/selectors.md` - Targeting cells for formatting23- `references/output.md` - Rendering to different formats2425## When to Use2627**Use flextable when:**28- Output to Word/PowerPoint required29- Need cell merging, conditional formatting, mini charts30- Cross-references and auto-numbered captions31- Same table across multiple formats3233**Consider alternatives:**34- HTML-only: gt or reactable35- LaTeX-heavy: kableExtra36- Interactive dashboards: DT3738## Quick Reference3940| Task | Function |41|------|----------|42| Create table | `flextable(df)` |43| Format numbers | `colformat_double()`, `colformat_int()` |44| Bold/italic/color | `bold()`, `italic()`, `color()` |45| Background color | `bg()` |46| Merge cells | `merge_v()`, `merge_h()`, `merge_at()` |47| Add header | `add_header_row()` |48| Auto-size | `autofit()` |49| Apply theme | `theme_vanilla()`, `theme_zebra()` |50| Save to Word | `save_as_docx()` |51| Save to PowerPoint | `save_as_pptx()` |5253## Core Pattern5455```r56library(flextable)5758# Create -> format -> output59ft <- flextable(head(mtcars, 5)) |>60 colformat_double(digits = 1) |>61 # Conditional formatting with formula selectors62 color(i = ~ mpg > 20, j = "mpg", color = "darkgreen") |>63 bold(i = ~ mpg > 20, j = "mpg") |>64 # Spanning header65 add_header_row(66 values = c("Performance", "Engine", "Transmission"),67 colwidths = c(2, 4, 5)68 ) |>69 theme_vanilla() |>70 autofit()7172save_as_docx(ft, path = "table.docx")73```7475## Selectors (Key Concept)7677Target cells for formatting:7879```r80# Row selector (formula)81bold(ft, i = ~ cyl == 6)8283# Column selector84bg(ft, j = ~ mpg + hp, bg = "lightgray")8586# Part selector87bold(ft, part = "header")88```8990See `references/selectors.md` for details on multi-content cells, mini charts, output-specific notes.9192## Common Mistakes9394| Mistake | Fix |95|---------|-----|96| Theme doesn't format new header | Apply theme AFTER `add_header_row()` |97| `autofit()` overflows margins | Use `set_table_properties(layout = "autofit")` |98| `height()` has no effect | Set `hrule(rule = "exact")` first |99| Formula selectors fail in header | Use integer indices for header rows |100| Images missing in Word | Use `officedown::rdocx_document()` |101| PDF ignores padding | Use `ft.tabcolsep` chunk option |102| `add_header_row` values wrong | One value per span; colwidths sum to ncol |103104## Advanced105106See `references/` for:107- **API.md**: Complete function reference (240+ functions)108- **selectors.md**: Multi-content cells, mini charts, output-specific notes109- **formatting.md**: All formatting functions and options110- **layout.md**: Merging, sizing, headers/footers111- **output.md**: Rendering to all formats