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

## ----setup--------------------------------------------------------------------
library(tsg)
library(dplyr)

## -----------------------------------------------------------------------------
person_record |>
  group_by(sex) |>
  generate_frequency(marital_status, group_as_list = TRUE)

## -----------------------------------------------------------------------------
person_record |>
  filter(age >= 15) |> 
  group_by(sex, employed) |>
  generate_frequency(marital_status, group_as_list = TRUE)

## -----------------------------------------------------------------------------
person_record |>
  filter(age >= 15) |> 
  group_by(sex) |>
  generate_crosstab(marital_status, employed, group_as_list = TRUE)

## -----------------------------------------------------------------------------
person_record |>
  group_by(sex) |>
  generate_frequency(marital_status, group_as_hierarchy = TRUE)

## -----------------------------------------------------------------------------
person_record |>
  group_by(sex) |>
  generate_frequency(
    marital_status,
    group_as_list      = TRUE,
    group_as_hierarchy = TRUE
  )

## -----------------------------------------------------------------------------
person_record |>
  filter(age >= 15) |> 
  group_by(sex, employed) |>
  generate_frequency(
    marital_status,
    group_as_list      = TRUE,
    group_as_hierarchy = TRUE
  )

## -----------------------------------------------------------------------------
person_record |>
  group_by(sex) |>
  generate_frequency(
    marital_status,
    group_as_hierarchy    = TRUE,
    label_group_hierarchy = "Grand Total"
  )

## -----------------------------------------------------------------------------
person_record |>
  filter(age >= 15) |>
  group_by(sex, employed) |>
  generate_frequency(
    marital_status,
    group_as_list         = TRUE,
    group_as_hierarchy    = TRUE,
    label_group_hierarchy = c(sex = "All sexes", employed = "All workers")
  )

## -----------------------------------------------------------------------------
person_record |>
  filter(age >= 15) |> 
  group_by(sex) |>
  generate_crosstab(
    marital_status,
    employed,
    group_as_list      = TRUE,
    group_as_hierarchy = TRUE
  )

## -----------------------------------------------------------------------------
person_record |>
  generate_crosstab(
    sex,
    seeing,
    hearing,
    walking,
    remembering,
    self_caring,
    communicating,
    multiple_columns        = TRUE,
    multiple_columns_filter = 2L
  )

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_crosstab(
#     sex,
#     seeing,
#     hearing,
#     multiple_columns        = TRUE,
#     multiple_columns_filter = 3L   # "A lot of difficulty"
#   )

## -----------------------------------------------------------------------------
person_record |>
  group_by(marital_status) |>
  generate_crosstab(
    sex,
    seeing,
    hearing,
    walking,
    multiple_columns        = TRUE,
    multiple_columns_filter = 2L,
    calculate_per_group     = TRUE
  )

## -----------------------------------------------------------------------------
person_record |>
  group_by(marital_status) |>
  generate_crosstab(
    sex,
    seeing,
    hearing,
    walking,
    multiple_columns        = TRUE,
    multiple_columns_filter = 2L,
    group_as_list           = TRUE
  )

## -----------------------------------------------------------------------------
person_record |>
  generate_crosstab(
    age,
    marital_status,
    sex,
    multiple_columns      = TRUE,
    multiple_columns_type = "stacked"
  )

## -----------------------------------------------------------------------------
person_record |>
  generate_crosstab(
    age,
    marital_status,
    sex,
    multiple_columns      = TRUE,
    multiple_columns_type = "stacked",
    label_separator       = " | ",
    add_percent           = FALSE
  )

## -----------------------------------------------------------------------------
person_record |>
  generate_crosstab(
    age,
    marital_status,
    sex,
    seeing,
    multiple_columns      = TRUE,
    multiple_columns_type = "stacked",
    add_percent           = FALSE
  )

## -----------------------------------------------------------------------------
person_record |>
  filter(age >= 15) |>
  group_by(employed) |>
  generate_crosstab(
    marital_status,
    sex,
    seeing,
    multiple_columns      = TRUE,
    multiple_columns_type = "stacked",
    calculate_per_group   = TRUE,
    add_percent           = FALSE
  )

## ----eval=FALSE---------------------------------------------------------------
# tables <- list(
#   "Sex"            = person_record |> generate_frequency(sex),
#   "Marital Status" = person_record |> generate_frequency(marital_status),
#   "Marital × Sex"  = person_record |>
#     generate_crosstab(marital_status, sex) |>
#     add_table_title("Marital Status by Sex") |>
#     add_table_subtitle("Row percentages") |>
#     add_footnote("Missing values are excluded from the denominator.")
# )
# 
# write_xlsx(tables, path = "report.xlsx")

## ----eval=FALSE---------------------------------------------------------------
# write_xlsx(tables, path = "multi-sheet-indexed.xlsx", include_table_list = TRUE)

## ----eval=FALSE---------------------------------------------------------------
# write_xlsx(tables, path = "output-tables/", separate_files = TRUE)

## ----eval=FALSE---------------------------------------------------------------
# # 1. Build tables
# freq_sex <- person_record |>
#   generate_frequency(sex) |>
#   add_table_title("Distribution by Sex") |>
#   add_source_note("Source: person_record dataset")
# 
# crosstab_marital_sex <- person_record |>
#   generate_crosstab(marital_status, sex) |>
#   add_table_title("Marital Status by Sex") |>
#   add_table_subtitle("Row percentages") |>
#   add_footnote("Missing values are excluded from the denominator.")
# 
# difficulties_wide <- person_record |>
#   generate_crosstab(
#     sex,
#     seeing, hearing, walking, remembering, self_caring, communicating,
#     multiple_columns        = TRUE,
#     multiple_columns_filter = 2L   # count "Some difficulty" responses
#   ) |>
#   add_table_title("Functional Difficulties by Sex (Some difficulty)")
# 
# # 2. Combine into a named list
# workbook_tables <- list(
#   "1. Sex"           = freq_sex,
#   "2. Marital x Sex" = crosstab_marital_sex,
#   "3. Difficulties"  = difficulties_wide
# )
# 
# # 3. Export with a style and an index sheet
# write_xlsx(
#   workbook_tables,
#   path               = "report.xlsx",
#   facade             = get_tsg_facade("yolo"),
#   include_table_list = TRUE
# )

## -----------------------------------------------------------------------------
person_record |>
  generate_frequency(sex) |>
  add_table_title("Sex distribution") |>
  add_footnote("Source: National Survey 2023.", placement = "left") |>
  add_footnote("Weighted estimates.", placement = "right")

## -----------------------------------------------------------------------------
person_record |>
  generate_crosstab(marital_status, sex) |>
  add_footnote(
    "Counts exclude respondents with unknown marital status.",
    locations = c("frequency_1", "frequency_2")
  )

## -----------------------------------------------------------------------------
person_record |>
  generate_frequency(sex) |>
  add_footnote("Source: National Survey 2023.") |>
  add_footnote("Weighted estimates.", placement = "right") |>
  add_footnote("Counts may not sum to total due to rounding.",
               locations = "frequency")

