Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
flextable is an R package designed for tabular reporting that enables researchers to transform statistical summaries and raw data frames into publication-quality tables. Maintained on CRAN by David Gohel and collaborators, flextable r provides an expressive grammar for formatting cell content, modifying borders, and controlling layouts across Microsoft Word (.docx), Microsoft PowerPoint (.pptx), HTML, PDF, RTF, and Typst documents.
What it is used for in research
In quantitative academic disciplines, preparing reproducible tables for manuscript submission or study reports often creates significant friction. While packages like ggplot2 streamline data visualization, standard statistical output in R—such as summary tables from linear regression models in R—traditionally required manual copy-pasting or complex LaTeX markup. flextable eliminates this bottleneck by generating fully styled, editable tables directly from R code.
Researchers commonly use flextable for:
- Clinical Study Reports and Baseline Tables: Formatting demographic and clinical baseline characteristics (Table 1) with spanning headers, aligned columns, and standard error annotations.
- Manuscript Submission to Word-First Journals: Unlike web-centric table generators,
flextableprovides native Microsoft Word (.docx) support, rendering clean vector borders and editable text rather than embedded images. - Automated Document Rendering: Seamless integration into Quarto and R Markdown workflows, allowing continuous compilation of reports as underlying datasets update.
- Model Summaries and Cross-Tabulations: Direct ingestion of statistical objects from R functions or companion packages like creating Table 1 and regression summaries with gtsummary via
as_flextable().
How to install or access it
The stable release of flextable is available from the Comprehensive R Archive Network (CRAN). Install the package and its core dependencies from the official CRAN repository using standard R package management:
# Install stable release from CRAN
install.packages("flextable")
# Install optional companion packages for document compilation and model tidying
install.packages(c("officer", "broom"))
To verify the installation in an interactive R session, load the library and check the installed version:
library(flextable)
packageVersion("flextable")
CRAN builds provide pre-compiled binaries for Windows and macOS, along with source packages for Linux distributions. flextable requires R version 3.5.0 or higher.
Step-by-step instructions
This walk-through demonstrates how to build, format, and export a publication-ready regression summary table to Microsoft Word (.docx) following academic reporting conventions.
-
Step 1: Fit a statistical model and tidy coefficients.
Fit a standard linear model usinglm()and extract tidy parameter estimates withbroom::tidy():library(flextable) library(broom) # Fit an example regression model predicting fuel economy fit <- lm(mpg ~ wt + hp + qsec, data = mtcars) reg_df <- tidy(fit) # Rename columns for presentation names(reg_df) <- c("Parameter", "Estimate", "Std_Error", "Statistic", "P_Value") -
Step 2: Initialize the flextable object and customize headers.
Pass the data frame toflextable()and assign descriptive header labels usingset_header_labels():ft <- flextable(reg_df) |> set_header_labels( Parameter = "Model Term", Estimate = "Coefficient (β)", Std_Error = "SE", Statistic = "t-Statistic", P_Value = "p-Value" ) -
Step 3: Format numerical precision and apply conditional styling.
Apply specific decimal formatting across columns and highlight statistically significant terms when evaluating null and alternative hypotheses:ft <- ft |> colformat_double(j = c("Estimate", "Std_Error", "Statistic"), digits = 3) |> colformat_double(j = "P_Value", digits = 4) |> bold(i = ~ P_Value < 0.05, j = "Parameter") |> color(i = ~ P_Value < 0.05, j = "P_Value", color = "#b30000") -
Step 4: Apply publication theming and alignment.
Apply the APA-styletheme_booktabs()theme, align numeric columns to the right, and add a formal table note in the footer:ft <- ft |> theme_booktabs() |> align(j = "Parameter", align = "left", part = "all") |> align(j = c("Estimate", "Std_Error", "Statistic", "P_Value"), align = "right", part = "all") |> add_footer_lines("Note: Bold terms denote significance at p < 0.05. Model N = 32.") |> fontsize(size = 9, part = "footer") |> italic(part = "footer") -
Step 5: Configure dimensions and export directly to Word.
Usefit_to_width()to keep the table within standard 6.5-inch margins and export to a Word document viasave_as_docx():# Scale table to fit 6.5-inch portrait page margins ft <- fit_to_width(ft, max_width = 6.5) # Save to docx save_as_docx("Table 1: Linear Regression Summary" = ft, path = "regression_summary.docx")
Output and how to read it
A flextable object is divided into three distinct logical structural parts:
- Header: Contains column labels and multi-tier spanning banners. Commands targeting
part = "header"apply exclusively to this section. - Body: Contains the underlying observational or summary data. Row selectors (
i) and column selectors (j) default to modifying the body. - Footer: Holds table notes, source citations, variable definitions, and significance thresholds. Added via
add_footer_lines()oradd_footer_row().
When viewed in the RStudio Viewer pane or knitted to HTML, flextable renders an interactive HTML widget. When compiled into Microsoft Word (.docx) or PowerPoint (.pptx), the output translates into native OpenXML table objects. In Word documents, each cell contains fully editable native text, preserving typography and column widths without rasterization.
Troubleshooting
When compiling research documents, analysts frequently encounter layout hurdles specific to Word or PDF rendering. Below are proven remedies documented in the official flextable user manual:
- Table overflowing page margins in Word or PDF: Calling
autofit()computes column dimensions based on text length without constraint, which can push wide tables outside printable margins. Usefit_to_width(ft, max_width = 6.5)for standard 8.5 x 11 inch portrait pages, or specify individual column widths usingwidth(j = "Parameter", width = 2.0). - Table splitting awkwardly across page breaks: In long reports, Word may cut rows in half across page transitions. Add
paginate(ft, init = TRUE, hdr_ftr = TRUE)before exporting to force header row repetition at the top of each new page and prevent individual row splits. - LaTeX or PDF rendering errors: When rendering to PDF via R Markdown or Quarto, ensure that system TeX packages are updated. For users managing complex tabular layouts in TeX environments, consult our guide on LaTeX table vertical centering. Alternatively, use Quarto with the Typst engine for high-speed PDF compilation.
- Special character escaping issues: Do not use HTML named character entities such as Greek letters in cell strings; pass literal UTF-8 Unicode characters (such as α, β, or ±) directly to preserve rendering across Word, PDF, and HTML targets.
Free alternatives
Several free and open-source packages in the R ecosystem provide specialized tabular reporting features. The table below summarizes key differences based on official documentation:
| Package | Primary Focus | Key Output Formats | Strengths | Official Source |
|---|---|---|---|---|
| flextable | Cross-platform tabular reporting | Word (.docx), PowerPoint (.pptx), HTML, PDF, RTF, Typst | Unmatched Word and PowerPoint formatting control; automatic pagination; formula selectors | CRAN (David Gohel / ArData) |
| gt | Grammar of tables | HTML, LaTeX, RTF, Word (basic) | Intuitive layered grammar; excellent HTML and Quarto interactive tables; publication themes | CRAN (Posit / Rich Iannone) |
| kableExtra | knitr table enhancement | HTML, LaTeX (PDF) | Deep LaTeX package integration; lightweight syntax; ideal for Sweave and knitr PDF pipelines | CRAN (Hao Zhu) |
| huxtable | Multi-engine document tables | HTML, LaTeX, Word (.docx), Excel (.xlsx), RTF | Native Excel export support; clean cell-level styling API; quick interactive console preview | CRAN (David Hugh-Jones) |
FAQ
How do you export a flextable directly to Microsoft Word (.docx)?
Use the save_as_docx() function from flextable. You can export one or multiple tables in a single call by providing named arguments: save_as_docx("Table Title" = ft, path = "output.docx"). Alternatively, within an R Markdown or Quarto document, returning the flextable object inside a code chunk automatically embeds it into the Word output when rendering with output: word_document.
What is the difference between autofit() and fit_to_width()?
autofit() evaluates font metrics using the gdtools package and expands column widths so that cell contents do not wrap, which may exceed page boundaries. In contrast, fit_to_width(ft, max_width = 6.5) scales table columns proportionally so that the entire table width exactly matches the specified maximum page margin width in inches.
Can flextable be used in Quarto documents?
Yes. flextable is fully compatible with Quarto across HTML, PDF (LaTeX and Typst), Word (.docx), and PowerPoint (.pptx) rendering engines. Quarto automatically captures flextable print calls without requiring additional chunk options.
How do you format p-values and decimal precision?
Use data-aware formatting functions such as colformat_double(), colformat_int(), and colformat_num(). For example, colformat_double(ft, j = "p_val", digits = 4) formats numeric columns to four decimal places without altering the underlying raw numeric data.
Sources
- Comprehensive R Archive Network (CRAN). Package flextable: Functions for Tabular Reporting (Version 0.10.4). Checked 2026-11-10. https://cran.r-project.org/package=flextable
- ArData & David Gohel. Using the flextable R package (Official Documentation Book). Checked 2026-11-10. https://ardata-fr.github.io/flextable-book/
- David Gohel. flextable: Reference Manual and Model Conversion API. Checked 2026-11-10. https://davidgohel.github.io/flextable/







