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

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

## ----eval=FALSE---------------------------------------------------------------
# # Bold "yolo" style
# person_record |>
#   generate_frequency(sex) |>
#   write_xlsx(path = "yolo.xlsx", facade = get_tsg_facade("yolo"))
# 
# # Explicitly use the default style (same as omitting the facade argument)
# person_record |>
#   generate_frequency(sex) |>
#   write_xlsx(path = "default.xlsx", facade = get_tsg_facade("default"))

## -----------------------------------------------------------------------------
person_record |>
  generate_frequency(sex) |>
  add_facade(
    table.offsetRow = 2,
    table.offsetCol = 1
  )

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_crosstab(marital_status, sex) |>
#   add_facade(
#     # Move the table down 2 rows and right 1 column (Excel only)
#     table.offsetRow        = 2,
#     table.offsetCol        = 1,
#     # Round numbers to 1 decimal place
#     table.decimalPrecision = 1,
#     # Make the last row (usually Total) bold
#     table.lastRowBold      = TRUE,
#     # Header background colour and white text
#     header.bgFill          = "#003366",
#     header.fontColour      = "#FFFFFF",
#     header.textDecoration  = "bold",
#     # Data cell font size
#     body.fontSize          = 11
#   ) |>
#   write_xlsx(path = "styled-table.xlsx")

## ----eval=FALSE---------------------------------------------------------------
# styled_tbl <- person_record |>
#   generate_frequency(sex) |>
#   add_table_title("Distribution by Sex") |>
#   add_source_note("Source: Survey 2024") |>
#   add_facade(
#     header.bgFill         = "#003366",
#     header.fontColour     = "#FFFFFF",
#     header.textDecoration = "bold",
#     body.fontSize         = 11,
#     table.lastRowBold     = TRUE
#   )
# 
# write_xlsx(styled_tbl, path = "table.xlsx")
# write_html(styled_tbl, path = "table.html")
# write_pdf(styled_tbl,  path = "table.pdf")
# write_docx(styled_tbl, path = "table.docx")

## ----eval=FALSE---------------------------------------------------------------
# generate_template("my-style.yaml", template = "facade")

## ----eval=FALSE---------------------------------------------------------------
# my_style <- get_tsg_facade("my-style.yaml")
# 
# person_record |>
#   generate_frequency(sex) |>
#   write_xlsx(path = "custom-styled.xlsx", facade = my_style)

## -----------------------------------------------------------------------------
overrides <- list(
  table.offsetRow   = 3,
  header.bgFill     = "#2E4057",
  header.fontColour = "#FFFFFF",
  body.fontSize     = 11
)

do.call(
  add_facade_alt,
  c(list(data = person_record |> generate_frequency(sex)), overrides)
)

## ----echo=FALSE---------------------------------------------------------------
props <- c(
  "fontName", "fontSize", "fontColour",
  "bgFill", "fgFill",
  "halign", "valign",
  "textDecoration", "wrapText", "indent",
  "border", "borderColour", "borderStyle",
  "numFmt", "height", "width"
)

sections <- c(
  "table", "title", "subtitle", "header", "spanner",
  "body", "col_first", "col_last", "row_group", "sub_group",
  "source_note", "footnotes"
)

has_prop <- function(section, prop) {
  if (prop == "width")  return(section %in% c("table", "col_first", "col_last", "row_group"))
  if (prop == "numFmt") return(section %in% c("body", "col_first", "col_last"))
  if (prop == "height") return(section != "table")
  TRUE
}

ref <- do.call(rbind, lapply(sections, function(s) {
  data.frame(
    Section  = s,
    Properties = paste(
      Filter(function(p) has_prop(s, p), props),
      collapse = ", "
    ),
    stringsAsFactors = FALSE
  )
}))

knitr::kable(ref, row.names = FALSE)

