Skip to main content
flextable provides several functions to control and inspect table dimensions.

autofit()

Computes and applies optimised column widths and row heights based on the minimum size required to display each cell’s content on one line.
autofit(
  x,
  add_w = 0.1,
  add_h = 0.1,
  part = c("body", "header"),
  unit = "in",
  hspans = "none"
)
autofit() is not the same as the Microsoft Word Autofit feature. It calculates minimum content widths and applies them as fixed widths. If you need a percentage-based responsive layout, use set_table_properties(layout = "autofit") instead.When autofit() is used with rotated text, rotation is ignored in the size calculation. Use dim_pretty() and width() manually in that case.
x
flextable
required
A flextable object.
add_w
number
default:"0.1"
Extra width to add to each column, in the unit specified by unit.
add_h
number
default:"0.1"
Extra height to add to each row, in the unit specified by unit.
part
string | character vector
default:"c(\"body\", \"header\")"
Which parts to include in the size calculation: "body", "header", "footer", or "all".
unit
string
default:"\"in\""
Unit for add_w and add_h. One of "in" (inches), "cm", or "mm".
hspans
string
default:"\"none\""
How to treat horizontally spanned (merged) cells in the width calculation:
  • "none" — merged cells contribute 0 width (do not influence column widths).
  • "divided" — merged cell width is divided equally among the spanned columns.
  • "included" — merged cell widths are included as-is in the calculation.

Example

ft <- flextable(head(mtcars))
ft <- autofit(ft)
ft

dim_pretty()

Returns the minimum estimated column widths and row heights without applying them to the table. Use this to inspect sizes before manually calling width() or height().
dim_pretty(x, part = "all", unit = "in", hspans = "none")
x
flextable
required
A flextable object.
part
string
default:"\"all\""
Which parts to include: "body", "header", "footer", or "all".
unit
string
default:"\"in\""
Unit for returned values. One of "in" (inches), "cm", or "mm".
hspans
string
default:"\"none\""
How to treat horizontally spanned cells. Same values as for autofit().

Return value

A list with two elements:
  • widths — numeric vector of minimum column widths.
  • heights — numeric vector of minimum row heights.

Example

ft <- flextable(head(mtcars))
dim_pretty(ft)

fit_to_width()

Shrinks the table to fit within a maximum total width by iteratively decreasing font sizes until the table fits.
fit_to_width(x, max_width, inc = 1L, max_iter = 20, unit = "in")
x
flextable
required
A flextable object.
max_width
number
required
Maximum total width the table should occupy.
inc
integer
default:"1"
Amount to decrease the font size (in points) at each iteration.
max_iter
integer
default:"20"
Maximum number of shrink iterations before stopping.
unit
string
default:"\"in\""
Unit for max_width. One of "in" (inches), "cm", or "mm".

Example

ft <- qflextable(head(mtcars))
ft <- width(ft, width = 1)
ft <- fit_to_width(ft, max_width = 4)
ft

flextable_dim()

Returns the overall width, height, and aspect ratio of the table as a named list.
flextable_dim(x, unit = "in")
x
flextable
required
A flextable object.
unit
string
default:"\"in\""
Unit for returned values. One of "in" (inches), "cm", or "mm".

Return value

A named list with:
  • widths — total table width.
  • heights — total table height.
  • aspect_ratioheights / widths.

Example

ft <- flextable(head(iris))
flextable_dim(ft)
ft <- autofit(ft)
flextable_dim(ft)

See also