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
Calluse_model_printer() once in a setup chunk to register as_flextable() as the knitr print method for all supported model classes:
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
| Column | Header label |
|---|---|
term | (blank) |
estimate | Estimate |
std.error | Standard Error |
statistic | t value |
p.value | Pr(>|t|) |
signif | (blank, optional) |
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
| Column | Header label |
|---|---|
term | (blank) |
estimate | Estimate |
std.error | Standard Error |
statistic | z value |
p.value | Pr(>|z|) |
signif | (blank, optional) |
- 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
| 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. |
| 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) |
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.
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 |
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:
withinss— within-cluster sum of squaressize— number of observations- Cluster centers for each variable
- 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:
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 checkgetOption("show.signif.stars"). When it is TRUE:
- A
signifcolumn is added with"***","**","*",".", or" "codes. - A footer note with the significance legend is appended.
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() |