7 Helpful Functions

7.1 P-value Formatting

pval_label_apa <- function(value,
                           breaks = c(.05, .01, .001),
                           symbols = c("*", "**", "***"),
                           decimals = 3,
                           leading = FALSE){
  
  value_apa = MOTE::apa(value = value,
                        decimals = decimals,
                        leading = leading)
  
  value_apa_min = MOTE::apa(value = breaks[3],
                            decimals = decimals,
                            leading = leading)
  
  dplyr::case_when(value <  breaks[3] ~ glue::glue("p < {value_apa_min} {symbols[3]}"),
                   value == breaks[3] ~ glue::glue("p = {value_apa_min} {symbols[3]}"),
                   value <=  breaks[2] ~ glue::glue("p = {value_apa} {symbols[2]}"),
                   value <=  breaks[1] ~ glue::glue("p = {value_apa} {symbols[1]}"),
                   value > breaks[1] ~ glue::glue("p = {value_apa}"))
}
c(0.23, 0.023, 0.0023, 0.00023) %>% pval_label_apa()
p = .230
p = .023 *
p = .002 **
p < .001 ***
c(0.05, 0.01, 0.001) %>% pval_label_apa()
p = .050 *
p = .010 **
p = .001 ***

7.2 APA Table Formatting

gt_apa <- function(x, title = " ", ...) {
  gt(x, ...) %>%
    tab_options(
      table.border.top.color = "white",
      heading.title.font.size = px(16),
      column_labels.border.top.width = 3,
      column_labels.border.top.color = "black",
      column_labels.border.bottom.width = 3,
      column_labels.border.bottom.color = "black",
      table_body.border.bottom.color = "black",
      table.border.bottom.color = "white",
      table.width = pct(100),
      table.background.color = "white"
    ) %>%
    cols_align(align="center") %>%
    tab_style(
      style = list(
        cell_borders(
          sides = c("top", "bottom"),
          color = "white",
          weight = px(1)
        ),
        cell_text(
          align="center"
        ),
        cell_fill(color = "white", alpha = NULL)
      ),
      locations = cells_body(
        columns = everything(),
        rows = everything()
      )
    ) %>%
    #title setup
    tab_header(
      title = html("<i>", title, "</i>")
    ) %>%
    opt_align_table_header(align = "left")
}