Skip to main content

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 an S3 generic that converts various R objects into flextable objects. Each supported class has its own method with class-specific arguments.

Generic

as_flextable(x, ...)
x
object
required
The object to convert. See the methods below for supported classes.
...
any
Additional arguments passed to the specific method.

Return value

An object of class flextable.

Methods

as_flextable.data.frame()

Produces a compact summary of a data frame, showing the first rows and column types. If the data frame has only one row, a simplified vertical layout is used.
as_flextable.data.frame(
  x,
  max_row = 10,
  split_colnames = FALSE,
  short_strings = FALSE,
  short_size = 35,
  short_suffix = "...",
  do_autofit = TRUE,
  show_coltype = TRUE,
  color_coltype = "#999999",
  ...
)
max_row
integer
default:"10"
Maximum number of rows to display.
split_colnames
logical
default:"FALSE"
If TRUE, column names are split on non-alphanumeric characters.
short_strings
logical
default:"FALSE"
If TRUE, character columns are truncated to short_size characters.
short_size
integer
default:"35"
Maximum character width for string columns when short_strings = TRUE.
short_suffix
string
default:"..."
Suffix appended to truncated string values.
do_autofit
logical
default:"TRUE"
If TRUE, autofit() is applied before rendering.
show_coltype
logical
default:"TRUE"
If TRUE, column data types are shown below the column headers.
color_coltype
string
default:"#999999"
Color used to render the column type labels.
Example
as_flextable(mtcars)

as_flextable.table()

Converts a table object (produced by base::table()) into a flextable.
as_flextable(x, ...)
Example
tab <- table(mtcars$cyl, mtcars$gear)
as_flextable(tab)

as_flextable.lm()

Produces a formatted coefficient table from a linear model fitted with lm(). Requires the broom package. Significance stars respect the options(show.signif.stars) setting.
as_flextable(x, ...)
Example
if (require("broom")) {
  lmod <- lm(rating ~ complaints + privileges +
    learning + raises + critical, data = attitude)
  ft <- as_flextable(lmod)
  ft
}

as_flextable.glm()

Produces a formatted coefficient table from a generalized linear model fitted with glm(). Requires the broom package.
as_flextable(x, ...)
Example
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
}

as_flextable.merMod()

Produces a formatted table from a mixed model. Supported classes: merMod, lme, gls, nlme, brmsfit, glmmTMB, glmmadmb. Requires the broom.mixed package.
as_flextable(x, add.random = TRUE, ...)
add.random
logical
default:"TRUE"
If TRUE, random effects are included in the table alongside fixed effects.
Example
if (require("broom.mixed") && require("nlme")) {
  m1 <- lme(distance ~ age, data = Orthodont)
  ft <- as_flextable(m1)
  ft
}

as_flextable.htest()

Converts an htest object (from functions such as t.test(), chisq.test(), wilcox.test()) into a flextable.
as_flextable(x, ...)
Example
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
}

as_flextable.kmeans()

Converts a kmeans object into a formatted cluster summary table. Requires the broom package.
as_flextable(x, digits = 4, ...)
digits
integer
default:"4"
Number of decimal places for numeric columns.
Example
if (require("stats")) {
  cl <- kmeans(scale(mtcars[1:7]), 5)
  ft <- as_flextable(cl)
  ft
}

as_flextable.pam()

Converts a pam object (from the cluster package) into a formatted cluster summary table. Requires the broom package.
as_flextable(x, digits = 4, ...)
digits
integer
default:"4"
Number of decimal places for numeric columns.
Example
if (require("cluster")) {
  dat <- as.data.frame(scale(mtcars[1:7]))
  cl <- pam(dat, 3)
  ft <- as_flextable(cl)
  ft
}

as_flextable.tabular()

Converts a tabular object (from the tables package) into a flextable.
as_flextable(x, ...)

as_flextable.xtable()

Converts an xtable object (from the xtable package) into a flextable.
as_flextable(x, ...)

See also

Build docs developers (and LLMs) love