Skip to contents

pretty_widths() sets each column's width to fit its contents and then scales the widths so that the table has the width given by table_width.

Usage

pretty_widths(
  x,
  min_width = 0.05,
  unit = c("in", "cm", "mm"),
  table_width = NULL
)

Arguments

x

flextable

min_width

minimum width of columns

unit

unit of the column widths: in, cm, or mm. A table_width greater than 1 (without a name) is read in this unit.

table_width

width of the table, in the same forms in every apa7 function: a proportion from 0 to 1 of the default width (the full width of a portrait page unless changed with apa7_defaults(table_width = )), "page" (or "portrait") for the full width of a portrait page, "landscape" for a landscape page, "column" for one column of a two-column page, or a named number: a proportion of one of these widths (e.g., c(landscape = .8)) or a width in inches, cm, or mm (e.g., c(inches = 5)). With 0, the columns are only as wide as their contents need. If NULL (default), the width set by the code chunk's apa7-width option in Quarto (e.g., #| apa7-width: landscape, or #| apa7-width: {landscape: .8} for a named number) or apa7.width option in R Markdown, or "page" for a chunk with apaquarto's apa-twocolumn: true; otherwise, the table_width set with apa7_defaults() (1 unless changed). The widths of the pages and the column are set with the page_width, landscape_width, and column_width arguments of apa7_defaults(). A number greater than 1 is read as inches, with a warning to write it as c(inches = ).

Value

flextable::flextable

Details

The widths are first measured with flextable::dim_pretty(), and no column is narrower than min_width. If the table width is 0, these widths are kept. Otherwise, they are scaled up or down in proportion so that they add up to the table width, with two exceptions:

  • Break columns (from add_break_columns()) are kept at min_width.

  • List columns (from add_list_column()) keep their measured width, and the column to the right of each one (the text of the list) gets the extra width instead.

The table's layout is then set to "fixed", so that Word keeps the widths.

If table_width is NULL (default), the width is taken from the code chunk's options or from apa7_defaults() (see the table_width argument). With the defaults, the table is as wide as the text on a portrait page.

Examples

ft <- flextable::flextable(head(mtcars[, 1:4]))
# Widths fitted to the contents
pretty_widths(ft)

mpg

cyl

disp

hp

21.0

6

160

110

21.0

6

160

110

22.8

4

108

93

21.4

6

258

110

18.7

8

360

175

18.1

6

225

105

# Widths stretched to a total of 4 inches pretty_widths(ft, table_width = c(inches = 4))

mpg

cyl

disp

hp

21.0

6

160

110

21.0

6

160

110

22.8

4

108

93

21.4

6

258

110

18.7

8

360

175

18.1

6

225

105