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, ormm. Atable_widthgreater 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 ininches,cm, ormm(e.g.,c(inches = 5)). With 0, the columns are only as wide as their contents need. IfNULL(default), the width set by the code chunk'sapa7-widthoption in Quarto (e.g.,#| apa7-width: landscape, or#| apa7-width: {landscape: .8}for a named number) orapa7.widthoption in R Markdown, or"page"for a chunk with apaquarto'sapa-twocolumn: true; otherwise, thetable_widthset withapa7_defaults()(1 unless changed). The widths of the pages and the column are set with thepage_width,landscape_width, andcolumn_widtharguments ofapa7_defaults(). A number greater than 1 is read as inches, with a warning to write it asc(inches = ).
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 atmin_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