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.
as_flextable() is a generic function. For supported model classes, it extracts the relevant statistics and produces a ready-to-use flextable with appropriate column labels, formatting, and footer notes. All methods require the broom or broom.mixed package.
Enabling automatic printing in R Markdown
Call use_model_printer() once in a setup chunk to register as_flextable() as the knitr print method for all supported model classes:
# In a setup chunk
flextable::use_model_printer()
After this call, any supported model object printed in a chunk renders as a flextable automatically. Supported classes include lm, glm, lme, merMod, gls, nlme, brmsfit, glmmTMB, glmmadmb, gam, kmeans, and pam.
Linear models — lm
as_flextable.lm() uses broom::tidy() and broom::glance() to produce a coefficient table with a footer showing model fit statistics.
Requires: broom
library(flextable)
if (require("broom")) {
lmod <- lm(
rating ~ complaints + privileges + learning + raises + critical,
data = attitude
)
ft <- as_flextable(lmod)
ft
}
Columns produced:
| Column | Header label |
|---|
term | (blank) |
estimate | Estimate |
std.error | Standard Error |
statistic | t value |
p.value | Pr(>|t|) |
signif | (blank, optional) |
The signif column is added only when getOption("show.signif.stars") is TRUE. Remove it with options(show.signif.stars = FALSE).
Footer lines added automatically:
- Residual standard error and degrees of freedom
- Multiple R-squared and Adjusted R-squared
- F-statistic, degrees of freedom, and p-value
Generalized linear models — glm
as_flextable.glm() uses broom::tidy() and summary() to produce a coefficient table.
Requires: broom
if (require("broom")) {
dat <- attitude
dat$high.rating <- (dat$rating > 70)
probit.model <- glm(
high.rating ~ learning + critical + advance,
data = dat,
family = binomial(link = "probit")
)
ft <- as_flextable(probit.model)
ft
}
Columns produced:
| Column | Header label |
|---|
term | (blank) |
estimate | Estimate |
std.error | Standard Error |
statistic | z value |
p.value | Pr(>|z|) |
signif | (blank, optional) |
Footer lines added automatically:
- Dispersion parameter
- Null deviance and degrees of freedom
- Residual deviance and degrees of freedom
Mixed models — merMod, lme, gls, nlme
as_flextable.merMod() handles objects from lme4 (merMod), nlme (lme, gls, nlme), brms (brmsfit), glmmTMB, and glmmadmb. The same method is registered for all of these classes.
Requires: broom.mixed
if (require("broom.mixed") && require("nlme")) {
m1 <- lme(distance ~ age, data = Orthodont)
ft <- as_flextable(m1)
ft
}
as_flextable(x, add.random = TRUE, ...)
| Parameter | Description |
|---|
x | A mixed model object. |
add.random | If TRUE (default), random effects are appended to the table as a grouped section below fixed effects. |
Columns produced:
| Column | Header label |
|---|
effect | (group label row) |
group | (shown for random effects) |
term | (blank) |
estimate | Estimate |
std.error | Standard Error |
df | (degrees of freedom) |
statistic | (blank) |
p.value | p-value (if available) |
Fixed and random effects are separated into grouped rows using as_grouped_data(). If the model does not produce p-values, those columns are omitted.
Footer lines added automatically:
- Square root of estimated residual variance (sigma)
- Log-likelihood
- AIC and BIC
Hypothesis tests — htest
as_flextable.htest() converts any object of class htest (e.g. from t.test(), chisq.test(), wilcox.test()) into a single-row flextable.
if (require("stats")) {
M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))
dimnames(M) <- list(
gender = c("F", "M"),
party = c("Democrat", "Independent", "Republican")
)
ft_1 <- as_flextable(chisq.test(M))
ft_1
}
The function extracts estimate, statistic, p.value, and parameter from the htest object. Additional fields included when present:
| Field | Condition |
|---|
conf.low, conf.high | When conf.int is non-null |
method | Always included when present |
alternative | Always included when present |
For Welch two-sample t-tests, the difference of means is prepended as estimate.
When show.signif.stars is TRUE, significance codes are appended to the p-value cell and a footer note is added.
Clustering — kmeans and pam
kmeans
as_flextable.kmeans() uses tabulator() internally to lay cluster statistics across columns:
if (require("stats")) {
cl <- kmeans(scale(mtcars[1:7]), 5)
ft <- as_flextable(cl)
ft
}
as_flextable(x, digits = 4, ...)
Each cluster becomes a column. Rows show per-cluster statistics:
withinss — within-cluster sum of squares
size — number of observations
- Cluster centers for each variable
Footer lines:
- Total sum of squares
- Total within-cluster sum of squares
- Between-cluster sum of squares
- BSS/TSS ratio
- Number of iterations
pam
as_flextable.pam() works similarly for cluster::pam() objects:
if (require("cluster")) {
dat <- as.data.frame(scale(mtcars[1:7]))
cl <- pam(dat, 3)
ft <- as_flextable(cl)
ft
}
as_flextable(x, digits = 4, ...)
Each cluster column shows statistics such as size, max.diss, avg.diss, diameter, separation, and avg.width. Medoid rows are flagged with * in the row label. The footer shows the average silhouette width.
Significance stars
All model methods check getOption("show.signif.stars"). When it is TRUE:
- A
signif column is added with "***", "**", "*", ".", or " " codes.
- A footer note with the significance legend is appended.
To suppress significance stars globally:
options(show.signif.stars = FALSE)
Supported model classes
| Class | Source package | Method |
|---|
lm | stats | as_flextable.lm() |
glm | stats | as_flextable.glm() |
merMod | lme4 | as_flextable.merMod() |
lme | nlme | as_flextable.lme() (alias for merMod) |
gls | nlme | as_flextable.gls() (alias for merMod) |
nlme | nlme | as_flextable.nlme() (alias for merMod) |
brmsfit | brms | as_flextable.brmsfit() (alias for merMod) |
glmmTMB | glmmTMB | as_flextable.glmmTMB() (alias for merMod) |
glmmadmb | glmmADMB | as_flextable.glmmadmb() (alias for merMod) |
htest | stats | as_flextable.htest() |
kmeans | stats | as_flextable.kmeans() |
pam | cluster | as_flextable.pam() |