Skip to contents

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_prefix and the heading (default: a space)

row_title_border

border drawn around each group of rows: a list of color, style, and width (in points), as in officer::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_column and row_title_align = "left")

col_keys

columns to display, in order (default: all columns of data), passed to flextable::flextable()

cwidth, cheight

initial cell width and height in inches, passed to flextable::flextable(). With pretty_widths = TRUE (default), the widths are then fitted to the contents and table_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_B and Model 1_SE share 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"). If NULL (default), the font set with apa7_defaults(font_family = ) (Times New Roman unless changed).

font_size

font size, in points. If NULL (default), the size set with apa7_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 with apa7_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 (see flextable::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 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 = ).

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. (In apa_style(), the default is no_markdown_columns. Here it differs because numeric columns are added to no_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 of data (set by functions such as apa_loadings()) are not formatted either. Likewise, a rule is drawn above the first body row whose first-column text matches a "rule_above" attribute of data.

auto_format_columns

if TRUE (default), columns whose names have a format in column_formats (e.g., p, SE, or R2) are formatted and given APA headers with apa_format_columns()

column_formats

a column_formats() object. If NULL (default), the column formats set with apa7_defaults().

pretty_widths

if TRUE (default), column widths are fitted to the contents and to table_width with pretty_widths()

add_breaks_between_spanners

if TRUE (default), a narrow empty column separates adjacent column spanners (see separate_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 by apa_note() from what a table function such as apa_anova(), apa_parameters(), or apa_loadings() recorded. A character string is used as the note (markdown is allowed), and NULL or FALSE (default) adds no note. Use this for documents that are not made with apaquarto. With apaquarto, use the apa-note chunk 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 of apa_style() is also an argument of apa_flextable(), so this is rarely needed.

Value

flextable::flextable

Details

With a few minor details omitted, apa_flextable does roughly this:

  1. Adds a break column between adjacent column spanners, if add_breaks_between_spanners = TRUE and separate_headers = TRUE. A break column is a narrow blank column without borders, so the rules under adjacent spanners do not touch.

  2. 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 with apa_format_columns(). Columns named in no_format_columns, or in a "no_format_columns" attribute of the data (set by functions such as apa_loadings()), are left as they are.

  3. Makes the flextable. With row_title_column, it first restructures the data with flextable::as_grouped_data(), and each row title gets a rule above it (row_title_border).

  4. Separates the headers into spanners and deckered heads, if separate_headers = TRUE.

  5. Left-aligns the first column and centers the columns of numbers.

  6. Applies apa_style() to style the table and convert markdown, if apa_style = TRUE.

  7. Aligns the row titles (row_title_align) and indents the rows under them (left_column_padding).

  8. Sets the column widths with pretty_widths(), if pretty_widths = TRUE.

  9. Lines up numbers padded by align_chr() on their decimal points.

  10. Draws a rule above the rows named in a "rule_above" attribute of the data (set by functions such as apa_loadings() and apa_chisq()).

  11. Adds the note, if note is 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

# }