The apa_flextable function formats a data frame and converts it to a
flextable in APA style. See Details for the steps it takes.
Usage
apa_flextable(
data,
row_title_column = NULL,
row_title_align = "left",
row_title_prefix = "",
row_title_sep = " ",
row_title_border = list(color = "gray20", style = "solid", width = 1),
left_column_padding = 20,
col_keys = colnames(data),
cwidth = 0.75,
cheight = 0.25,
header_align_vertical = c("top", "middle", "bottom"),
separate_headers = TRUE,
apa_style = TRUE,
font_family = NULL,
font_size = NULL,
text_color = "black",
border_color = "black",
border_width = 0.5,
line_spacing = NULL,
horizontal_padding = 3,
table_align = "left",
layout = "autofit",
table_width = NULL,
markdown = TRUE,
markdown_header = markdown,
markdown_body = markdown,
no_markdown_columns = NULL,
no_markdown_columns_header = NULL,
no_format_columns = NULL,
auto_format_columns = TRUE,
column_formats = NULL,
pretty_widths = TRUE,
add_breaks_between_spanners = TRUE,
note = NULL,
...
)Arguments
- data
data.frame or tibble
- row_title_column
column whose values become heading rows that group the rows below them (unquoted or quoted name, or a tidyselect helper). The column itself is not displayed. Rows should already be sorted by it.
- row_title_align
alignment of the heading rows:
"left"(default),"center", or"right"- row_title_prefix
text placed before each heading (e.g.,
"Group"gives "Group A")- row_title_sep
text between
row_title_prefixand the heading (default: a space)- row_title_border
border drawn around each group of rows: a list of
color,style, andwidth(in points), as inofficer::fp_border()- left_column_padding
left padding of the first column's body rows, in points, which indents them under left-aligned heading rows (only used with
row_title_columnandrow_title_align = "left")- col_keys
columns to display, in order (default: all columns of
data), passed toflextable::flextable()- cwidth, cheight
initial cell width and height in inches, passed to
flextable::flextable(). Withpretty_widths = TRUE(default), the widths are then fitted to the contents andtable_width.- header_align_vertical
vertical alignment of header text when header cells differ in height:
"top"(default),"middle", or"bottom"- separate_headers
if
TRUE(default), column names with an underscore become two header rows: the part before it is a column spanner over the columns that share it (e.g.,Model 1_BandModel 1_SEshare a "Model 1" spanner)- apa_style
if
TRUE(default),apa_style()is applied (fonts, borders, spacing, and markdown)- font_family
font family (e.g.,
"Times New Roman"or"Arial"). IfNULL(default), the font set withapa7_defaults(font_family = )(Times New Roman unless changed).- font_size
font size, in points. If
NULL(default), the size set withapa7_defaults(font_size = )(12 unless changed).- text_color
color of the text: an R color name (e.g.,
"black", the default) or a hex code (e.g.,"#333333")- border_color
color of the rules, in the same forms as
text_color- border_width
width of the rules, in points (default: 0.5)
- line_spacing
spacing between lines of the header and body (e.g., 2 for double spacing or 1 for single spacing). If
NULL(default), the spacing set withapa7_defaults(line_spacing = )(2 unless changed). Table notes are single-spaced.- horizontal_padding
space to the left and right of each cell's contents, in points (default: 3)
- table_align
alignment of the table on the page:
"left"(default),"center", or"right"- layout
"autofit"(default) lets the column widths adjust to the contents in Word;"fixed"keeps the widths as set (seeflextable::set_table_properties())- 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 = ).- markdown
if
TRUE(default), text in the header and body is converted from markdown (e.g.,*p*becomes an italic p)- markdown_header, markdown_body
whether the header or the body is converted from markdown (both default to
markdown)- no_markdown_columns
body columns whose text is shown as it is, not converted from markdown (unquoted or quoted names, or tidyselect helpers)
- no_markdown_columns_header
column headers that are not treated as markdown. If
NULL(default), every header is treated as markdown. (Inapa_style(), the default isno_markdown_columns. Here it differs because numeric columns are added tono_markdown_columns, and their headers still need markdown.)- no_format_columns
Column name or tidyselect function. selected columns are not formatted. Columns named in a
"no_format_columns"attribute ofdata(set by functions such asapa_loadings()) are not formatted either. Likewise, a rule is drawn above the first body row whose first-column text matches a"rule_above"attribute ofdata.- auto_format_columns
if
TRUE(default), columns whose names have a format incolumn_formats(e.g.,p,SE, orR2) are formatted and given APA headers withapa_format_columns()- column_formats
a
column_formats()object. IfNULL(default), the column formats set withapa7_defaults().- pretty_widths
if
TRUE(default), column widths are fitted to the contents and totable_widthwithpretty_widths()- add_breaks_between_spanners
if
TRUE(default), a narrow empty column separates adjacent column spanners (seeseparate_headers), so that their rules do not touch- note
table note placed below the table, after "Note.". The note is single-spaced. If
TRUE, the note is written byapa_note()from what a table function such asapa_anova(),apa_parameters(), orapa_loadings()recorded. A character string is used as the note (markdown is allowed), andNULLorFALSE(default) adds no note. Use this for documents that are not made with apaquarto. With apaquarto, use theapa-notechunk option instead (e.g.,#| apa-note: !expr apa7::apa_note(tbl)), so that the note is placed and styled by apaquarto and is not added twice.- ...
passed to
apa_style(). Every argument ofapa_style()is also an argument ofapa_flextable(), so this is rarely needed.
Details
With a few minor details omitted, apa_flextable does roughly this:
Adds a break column between adjacent column spanners, if
add_breaks_between_spanners = TRUEandseparate_headers = TRUE. A break column is a narrow blank column without borders, so the rules under adjacent spanners do not touch.If
auto_format_columns = TRUE, turns columns of whole numbers (e.g., degrees of freedom) into integers so that they have no decimals. Then formats the columns withapa_format_columns(). Columns named inno_format_columns, or in a"no_format_columns"attribute of the data (set by functions such asapa_loadings()), are left as they are.Makes the flextable. With
row_title_column, it first restructures the data withflextable::as_grouped_data(), and each row title gets a rule above it (row_title_border).Separates the headers into spanners and deckered heads, if
separate_headers = TRUE.Left-aligns the first column and centers the columns of numbers.
Applies
apa_style()to style the table and convert markdown, ifapa_style = TRUE.Aligns the row titles (
row_title_align) and indents the rows under them (left_column_padding).Sets the column widths with
pretty_widths(), ifpretty_widths = TRUE.Lines up numbers padded by
align_chr()on their decimal points.Draws a rule above the rows named in a
"rule_above"attribute of the data (set by functions such asapa_loadings()andapa_chisq()).Adds the note, if
noteis given. It is added last so that a long note does not widen the columns.
For a step-by-step version of this process, see the article Building Tables by Hand.
References
American Psychological Association. (2020). Publication manual of the American Psychological Association (7th ed.). doi:10.1037/0000165-000
Examples
# Means and SDs, with a heading row for each transmission type
d <- mtcars
d$am <- factor(d$am, levels = 0:1, labels = c("Automatic", "Manual"))
d[, c("am", "mpg", "hp", "wt")] |>
tidyr::pivot_longer(-am, names_to = "Variable") |>
dplyr::summarise(Mean = mean(value), SD = sd(value), .by = c(am, Variable)) |>
dplyr::arrange(am) |>
apa_flextable(row_title_column = am, row_title_align = "center")
Variable
Mean
SD
Automatic
Mpg
17.15
3.83
Hp
160.26
53.91
Wt
3.77
0.78
Manual
Mpg
24.39
6.17
Hp
126.85
84.06
Wt
2.41
0.62
# Widths for a landscape page, one column of a two-column page, and 10 cm
# \donttest{
fit <- lm(mpg ~ wt + hp, data = mtcars)
apa_flextable(apa_parameters(fit), table_width = "landscape")
Variable
B
SE
β
t(29)
p
Constant
37.23
1.60
.00
23.28
<.001
Wt
−3.88
0.63
−.63
−6.13
<.001
Hp
−0.03
0.01
−.36
−3.52
.002
apa_flextable(apa_parameters(fit), table_width = "column")
Variable
B
SE
β
t(29)
p
Constant
37.23
1.60
.00
23.28
<.001
Wt
−3.88
0.63
−.63
−6.13
<.001
Hp
−0.03
0.01
−.36
−3.52
.002
apa_flextable(apa_parameters(fit), table_width = c(cm = 10))
Variable
B
SE
β
t(29)
p
Constant
37.23
1.60
.00
23.28
<.001
Wt
−3.88
0.63
−.63
−6.13
<.001
Hp
−0.03
0.01
−.36
−3.52
.002
# }