R flextable
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 formattingFrom its SKILL.md
npx -y skills add arthurgailes/r-package-skills --skill r-flextableAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 23 stars23 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
3.3 KB, as published. Nobody here has run it
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 referencereferences/overview.md- Core concepts and workflowreferences/formatting.md- Styling and appearancereferences/layout.md- Structure and mergingreferences/selectors.md- Targeting cells for formattingreferences/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