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

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

## -----------------------------------------------------------------------------
person_record |>
  generate_frequency(sex)

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_frequency(sex) |>
#   write_xlsx(path = "table-sex.xlsx")

## ----echo=FALSE, out.width="80%", eval=file.exists("../man/figures/output-formats-t1.png")----
# knitr::include_graphics("../man/figures/output-formats-t1.png")

## -----------------------------------------------------------------------------
person_record |>
  generate_crosstab(marital_status, sex) |>
  add_table_title("Marital Status by Sex") |>
  add_table_subtitle("National Sample Survey, 2024") |>
  add_source_note("Source: person_record dataset.") |>
  add_footnote("Missing values are excluded from the denominator.")

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_crosstab(marital_status, sex) |>
#   add_table_title("Marital Status by Sex") |>
#   add_table_subtitle("National Sample Survey, 2024") |>
#   add_source_note("Source: person_record dataset.") |>
#   add_footnote("Missing values are excluded from the denominator.") |>
#   write_xlsx(path = "table-marital-sex.xlsx")

## ----echo=FALSE, out.width="80%", eval=file.exists("../man/figures/output-formats-t7.png")----
# knitr::include_graphics("../man/figures/output-formats-t7.png")

## -----------------------------------------------------------------------------
person_record |>
  generate_frequency(sex, add_cumulative = TRUE, add_cumulative_percent = TRUE)

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_frequency(sex, add_cumulative = TRUE, add_cumulative_percent = TRUE) |>
#   write_xlsx(path = "table-sex-cumulative.xlsx")

## ----echo=FALSE, out.width="80%", eval=file.exists("../man/figures/output-formats-t2.png")----
# knitr::include_graphics("../man/figures/output-formats-t2.png")

## -----------------------------------------------------------------------------
person_record |>
  group_by(sex) |>
  generate_frequency(employed)

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   group_by(sex) |>
#   generate_frequency(employed) |>
#   write_xlsx(path = "table-employed-by-sex.xlsx")

## ----echo=FALSE, out.width="80%", eval=file.exists("../man/figures/output-formats-t3.png")----
# knitr::include_graphics("../man/figures/output-formats-t3.png")

## -----------------------------------------------------------------------------
person_record |>
  generate_crosstab(employed, sex)

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_crosstab(employed, sex) |>
#   write_xlsx(path = "crosstab-employed-sex.xlsx")

## ----echo=FALSE, out.width="80%", eval=file.exists("../man/figures/output-formats-t4.png")----
# knitr::include_graphics("../man/figures/output-formats-t4.png")

## -----------------------------------------------------------------------------
person_record |>
  generate_crosstab(employed, sex, percent_by_column = TRUE)

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_crosstab(employed, sex, percent_by_column = TRUE) |>
#   write_xlsx(path = "crosstab-column-pct.xlsx")

## ----echo=FALSE, out.width="80%", eval=file.exists("../man/figures/output-formats-t5.png")----
# knitr::include_graphics("../man/figures/output-formats-t5.png")

## ----eval=FALSE---------------------------------------------------------------
# tables <- list(
#   "Sex"        = person_record |> generate_frequency(sex),
#   "Employment" = person_record |> generate_frequency(employed),
#   "Crosstab"   = person_record |> generate_crosstab(employed, sex)
# )
# 
# write_xlsx(tables, path = "multi-sheet.xlsx")

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

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

## ----eval=FALSE---------------------------------------------------------------
# generate_template("table-list-template.xlsx", template = "table-list")

## ----eval=FALSE---------------------------------------------------------------
# table_ref <- tibble::tibble(
#   table_id     = c("table_sex", "table_emp", "table_ct"),
#   table_number = 1:3,
#   table_name   = c("Sex", "Employment", "Employment × Sex"),
#   title        = c(
#     "Distribution by Sex",
#     "Employment Status",
#     "Employment Status by Sex"
#   ),
#   subtitle     = c(NA, NA, "Cross-tabulation"),
#   footnotes    = NA,
#   source_note  = "Source: person_record dataset."
# )

## ----eval=FALSE---------------------------------------------------------------
# tables <- list(
#   table_sex = person_record |> generate_frequency(sex),
#   table_emp = person_record |> generate_frequency(employed),
#   table_ct  = person_record |> generate_crosstab(employed, sex)
# )
# 
# write_xlsx(
#   tables,
#   path                 = "report.xlsx",
#   include_table_list   = TRUE,
#   table_list_reference = table_ref
# )

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_frequency(sex) |>
#   add_table_title("Distribution by Sex") |>
#   write_html(path = "table-sex.html")

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_crosstab(marital_status, sex) |>
#   add_table_title("Marital Status by Sex") |>
#   write_html(path = "crosstab.html")

## ----eval=FALSE---------------------------------------------------------------
# tables <- list(
#   "Sex"           = person_record |> generate_frequency(sex),
#   "Marital Status"= person_record |> generate_frequency(marital_status),
#   "Sex × Marital" = person_record |> generate_crosstab(sex, marital_status)
# )
# 
# write_html(tables, path = "report.html", include_table_list = TRUE)

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

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_frequency(sex) |>
#   add_table_title("Distribution by Sex") |>
#   write_pdf(path = "table-sex.pdf")

## ----eval=FALSE---------------------------------------------------------------
# write_pdf(tables, path = "pdf-output/")

## ----eval=FALSE---------------------------------------------------------------
# write_pdf(tables, path = "report.pdf", separate_files = FALSE)

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_frequency(sex) |>
#   add_table_title("Distribution by Sex") |>
#   add_source_note("Source: person_record dataset") |>
#   write_docx(path = "table-sex.docx")

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_crosstab(marital_status, sex) |>
#   add_table_title("Marital Status by Sex") |>
#   add_footnote("Missing values excluded from the denominator.") |>
#   write_docx(path = "crosstab.docx")

## ----eval=FALSE---------------------------------------------------------------
# tables <- list(
#   "Sex"           = person_record |>
#     generate_frequency(sex) |>
#     add_table_title("Distribution by Sex"),
#   "Marital Status"= person_record |>
#     generate_frequency(marital_status) |>
#     add_table_title("Distribution by Marital Status"),
#   "Sex × Marital" = person_record |>
#     generate_crosstab(sex, marital_status) |>
#     add_table_title("Sex by Marital Status")
# )
# 
# write_docx(tables, path = "report.docx")

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

