Skip to contents

The apa_flextable function performs a number of formatting operations on the data before and after the data are sent to flextable. See Details.

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 = 12,
  text_color = "black",
  border_color = "black",
  border_width = 0.5,
  line_spacing = 2,
  horizontal_padding = 3,
  table_align = "left",
  layout = "autofit",
  table_width = 1,
  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,
  ...
)

Arguments

data

data.frame or tibble

row_title_column

Column name or tidyselect function. column to group rows

row_title_align

alignment of row title ("left", "center", "right")

row_title_prefix

text to be added to each title

row_title_sep

separator for prefix

row_title_border

list of flextable styles

left_column_padding

Number of points the left column is padded (only relevant when there is a row_title_column and row_title_align = "left")

col_keys

column keys passed to flextable (defaults data column names)

cwidth

initial cell width in inches

cheight

initial cell height in inches

header_align_vertical

vertical alignment of headers. Can be "top", "middle", or "bottom"

separate_headers

separate header rows (default: TRUE)

apa_style

apply apa_style function (default: TRUE)

font_family

font family

font_size

font size

text_color

text color

border_color

border color

border_width

border width in pixels

line_spacing

spacing between lines

horizontal_padding

horizontal padding (in pixels)

table_align

table alignment ("left", "center", "right")

layout

table layout ("autofit", "fixed")

table_width

table width (in pixels, 0 for auto)

markdown

apply markdown formatting to header and body

markdown_header

apply markdown formatting to header

markdown_body

apply markdown formatting to body

no_markdown_columns

body columns that should not be treated as markdown

no_markdown_columns_header

column headers that should not be treated as markdown

no_format_columns

Column name or tidyselect function. selected columns are not formatted

auto_format_columns

if true, will attempt to format some columns automatically

column_formats

a column_formats object

pretty_widths

apply pretty_widths function

add_breaks_between_spanners

add breaks between spanners if TRUE

...

arguments passed to apa_style

Value

flextable::flextable

Details

Roughly speaking, apa_flextable performs these operations by default:

  1. Apply as_grouped_data and restructure row titles, if row_title is specified.

  2. Format data with apa_format_columns if auto_format_columns = TRUE

  3. Separate headers into multiple header rows if separate_headers = TRUE

  4. Apply flextable::flextable

  5. Apply flextable::surround to make borders to separate row groups, if any.

  6. Apply the apa_style function (table formatting and markdown conversion) if apa_style = TRUE

  7. Apply pretty_widths if pretty_widths = TRUE

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
library(flextable)
mtcars %>%
  dplyr::select(vs, am, gear, carb) |>
  tidyr::pivot_longer(-vs,  names_to = "Variable") |>
  dplyr::summarise(Mean = round(mean(value), 2),
                   SD = round(sd(value), 2),
                   .by = c(Variable,vs)) |>
  dplyr::mutate(vs = factor(vs, levels = 0:1, labels = c("Automatic", "Manual"))) |>
  apa_flextable(row_title_column= vs,  row_title_align = "center") |>
  align(j = 2:3, align = "center")

Variable

Mean

SD

Automatic

Am

0.33

0.49

Gear

3.56

0.86

Carb

3.61

1.54

Manual

Am

0.50

0.52

Gear

3.86

0.53

Carb

1.79

1.05