Skip to contents

`margot_make_tables` is a wrapper function for `table1::table1()` that simplifies the creation of summary tables. It allows for custom variable labelling, formatting, factor conversion, and additional table options. The function also performs data checks and provides user-friendly CLI alerts upon completion.

Usage

margot_make_tables(
  data,
  vars,
  by,
  labels = NULL,
  factor_vars = NULL,
  table1_opts = list(),
  format = "html",
  kable_opts = list()
)

Arguments

data

A data frame containing the dataset.

vars

A character vector of variable names to include on the left-hand side of the table.

by

A character vector of variable names to stratify the table by. Supports multiple variables for interactions.

labels

A named character vector for custom variable labels. Names should correspond to variable names in `vars`.

factor_vars

An optional character vector of variable names in `vars` to convert to factors for frequency tables.

table1_opts

A list of additional options to pass to `table1::table1()`. For example, `list(overall = FALSE, transpose = TRUE)`.

format

A character string specifying the output format. Options are `"html"` (default) and `"latex"`.

kable_opts

A list of additional options to pass to `kable_styling()` when `format = "latex"`.

Value

A `table1` object or a LaTeX-formatted table generated by `kableExtra`.

Examples

if (FALSE) { # \dontrun{
# Define variables
vars_list <- c(
  "age",
  "agreeableness",
  "belong",
  "born_nz",
  "conscientiousness",
  "education_level_coarsen",
  "employed",
  "eth_cat",
  "extraversion",
  "honesty_humility",
  "kessler_latent_anxiety",
  "kessler_latent_depression",
  "hours_children",
  "hours_commute",
  "hours_exercise",
  "hours_housework",
  "household_inc",
  "male",
  "neuroticism",
  "nz_dep2018",
  "nzsei_13_l",
  "openness",
  "parent",
  "partner",
  "political_conservative",
  "religion_identification_level",
  "rural_gch_2018_l",
  "rwa",
  "sample_frame_opt_in",
  "sdo",
  "short_form_health",
  "support"
)

# Define labels
var_labels <- c(
  "sdo" = "Social Dominance Orientation",
  "born_nz" = "Born NZ",
  "rural_gch_2018_l" = "Rural GCH 2018 Levels",
  "eth_cat" = "Ethnicity",
  "rwa" = "Right Wing Authoritarianism",
  "support" = "Social Support (perceived)"
)

# Create the summary table in LaTeX format
summary_tables_latex <- margot_make_tables(
  data = dat_long_amelia,
  vars = vars_list,
  by = "wave",
  labels = var_labels,
  factor_vars = c("rural_gch_2018_l", "eth_cat"),
  table1_opts = list(overall = FALSE, transpose = TRUE),
  format = "latex",
  kable_opts = list(
    booktabs = TRUE,
    longtable = TRUE,
    font_size = 10,
    latex_options = c("hold_position", "repeat_header", "striped", "longtable")
  )
)
} # }