| apa_flextable | R Documentation |
The apa_flextable function formats a data frame and converts it to a
flextable in APA style. See Details for the steps it takes.
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,
...
)
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: |
row_title_prefix |
text placed before each heading (e.g., |
row_title_sep |
text between |
row_title_border |
border drawn around each group of rows: a list of
|
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 |
col_keys |
columns to display, in order (default: all columns of
|
cwidth, cheight |
initial cell width and height in inches, passed to
|
header_align_vertical |
vertical alignment of header text when header
cells differ in height: |
separate_headers |
if |
apa_style |
if |
font_family |
font family (e.g., |
font_size |
font size, in points. If |
text_color |
color of the text: an R color name (e.g., |
border_color |
color of the rules, in the same forms as |
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 |
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: |
layout |
|
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 |
markdown |
if |
markdown_header, markdown_body |
whether the header or the body is
converted from markdown (both default to |
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 |
no_format_columns |
Column name or tidyselect function. selected columns are not formatted. Columns named in a |
auto_format_columns |
if |
column_formats |
a |
pretty_widths |
if |
add_breaks_between_spanners |
if |
note |
table note placed below the table, after "Note.". The note is single-spaced. If |
... |
passed to |
With a few minor details omitted, apa_flextable does roughly this:
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.
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.
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).
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, if
apa_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(), if pretty_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 as apa_loadings() and apa_chisq()).
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.
flextable::flextable
American Psychological Association. (2020). Publication manual of the American Psychological Association (7th ed.). \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1037/0000165-000")}
# 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")
# Widths for a landscape page, one column of a two-column page, and 10 cm
fit <- lm(mpg ~ wt + hp, data = mtcars)
apa_flextable(apa_parameters(fit), table_width = "landscape")
apa_flextable(apa_parameters(fit), table_width = "column")
apa_flextable(apa_parameters(fit), table_width = c(cm = 10))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.