## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup, message = FALSE---------------------------------------------------
library(mlstats)
library(dplyr)
library(stringr)

## ----data---------------------------------------------------------------------
data("media_diary")
vars <- c("self_control", "wellbeing", "screen_time", "stress", "enjoyment")

## ----compute, warning = FALSE-------------------------------------------------
result <- mldesc(data = media_diary, group = "person", vars = vars)

## ----console------------------------------------------------------------------
result

## ----tt, warning = FALSE------------------------------------------------------
print(result, format = "tt")

## ----tt-custom, warning = FALSE-----------------------------------------------
print(result,
  format           = "tt",
  table_title      = "Daily diary study: descriptive statistics and multilevel correlations",
  correlation_note = "Within-person correlations above, between-person below the diagonal.",
  note_text        = "N = 100 persons, 14 daily observations each. Simulated data."
)

## ----gt-install, eval = FALSE-------------------------------------------------
# install.packages("gt")

## ----gt-basic-----------------------------------------------------------------
print(result, format = "gt")

## ----drop-cols, warning = FALSE-----------------------------------------------
result |>
  select(-n_obs, -range) |>
  print(format = "tt")

## ----replace-na, warning = FALSE----------------------------------------------
result |>
  mutate(across(everything(), ~ str_replace(as.character(.x), "^NA$", "–"))) |>
  print(format = "tt")

## ----rename, warning = FALSE--------------------------------------------------
result |>
  mutate(variable = case_when(
    variable == "Self control" ~ "Trait self-control",
    variable == "Wellbeing"    ~ "Daily wellbeing",
    variable == "Screen time"  ~ "Screen time (min)",
    variable == "Stress"       ~ "Perceived stress",
    variable == "Enjoyment"    ~ "Media enjoyment"
  )) |>
  print(format = "tt", table_title = "Study variables: descriptive statistics")

## ----combined, warning = FALSE------------------------------------------------
result |>
  select(-n_obs, -range) |>
  mutate(across(everything(), ~ str_replace(as.character(.x), "^NA$", "–"))) |>
  mutate(variable = case_when(
    variable == "Self control" ~ "Trait self-control",
    variable == "Wellbeing"    ~ "Daily wellbeing",
    variable == "Screen time"  ~ "Screen time (min)",
    variable == "Stress"       ~ "Perceived stress",
    variable == "Enjoyment"    ~ "Media enjoyment"
  )) |>
  print(
    format           = "tt",
    table_title      = "Descriptive statistics and multilevel correlations",
    correlation_note = "Within-person correlations above, between-person below the diagonal.",
    note_text        = "N = 100, T = 1,400 daily observations. Self-control was measured as a trait (between-person only)."
  )

## ----combined-gt--------------------------------------------------------------
result |>
  select(-n_obs, -range) |>
  mutate(across(everything(), ~ str_replace(as.character(.x), "^NA$", "–"))) |>
  mutate(
    variable = case_when(
      variable == "Self control" ~ "Trait self-control<sup>c</sup>",
      variable == "Wellbeing"    ~ "Daily wellbeing",
      variable == "Screen time"  ~ "Screen time (min)",
      variable == "Stress"       ~ "Perceived stress",
      variable == "Enjoyment"    ~ "Media enjoyment"
    )
  ) |>
  print(
    format           = "gt",
    table_title      = "Descriptive statistics and multilevel correlations",
    correlation_note = "Within-person correlations above, between-person below the diagonal.",
    note_text        = "<i>Note</i>. <i>N</i> = 100, <i>T</i> = 1,400 daily observations."
  ) |>
  gt::tab_source_note(
    source_note = gt::html(
      "<sup>c</sup> Self-control was measured as a stable trait; no within-person correlations are available."
    )
  ) |>
  gt::fmt_markdown(columns = variable)

