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

# The workbooks behind the screenshots go to a temporary folder. The screenshots are
# made from the same code by data-raw/vignette-screenshots.R with Microsoft Excel and
# are stored in man/figures/; they are shown at half their pixel width, their size in Excel.
fig_dir <- normalizePath("../man/figures", mustWork = FALSE)
out_dir <- Sys.getenv("TSG_VIGNETTE_OUT", file.path(tempdir(), "facade"))
dir.create(out_dir, showWarnings = FALSE, recursive = TRUE)
knitr::opts_knit$set(root.dir = out_dir)

shot <- function(name) {
  path <- file.path(fig_dir, paste0("facade-", name, ".png"))
  if (!file.exists(path)) return(invisible())
  header <- readBin(path, "raw", 24)
  width <- sum(as.integer(header[17:20]) * 256^(3:0))
  knitr::asis_output(sprintf(
    '<div style="overflow-x:auto"><img src="%s" width="%d" alt="Excel sheet: %s" style="max-width:none;height:auto"></div>',
    knitr::image_uri(path), width %/% 2, name
  ))
}

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

## ----eval=FALSE---------------------------------------------------------------
# tbl <- person_record |>
#   generate_frequency(sex)
# 
# write_xlsx(tbl, path = "booktabs.xlsx", facade = "booktabs")
# write_html(tbl, path = "booktabs.html", facade = "booktabs")
# write_docx(tbl, path = "booktabs.docx", facade = "booktabs")
# 
# # The default style is used when you leave out `facade`
# write_xlsx(tbl, path = "default.xlsx")

## ----include = FALSE----------------------------------------------------------
gallery_tbl <- person_record |>
  generate_crosstab(marital_status, sex) |>
  add_table_title("Marital status by sex") |>
  add_source_note("Source: person_record")

facades <- c("default", "yolo", "apa", "booktabs", "striped", "gridded", "minimal",
             "financial", "nyt", "jama", "dark", "pastel", "census")

for (f in facades) {
  write_xlsx(gallery_tbl, paste0("facade-", f, ".xlsx"), facade = f)
}

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

## -----------------------------------------------------------------------------
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.fgFill = "#003366",
    header.fontColour = "#FFFFFF",
    header.textDecoration = "bold",
    # Data cell font size
    body.fontSize = 11
  ) |>
  write_xlsx(path = "styled-table.xlsx")

## ----echo = FALSE-------------------------------------------------------------
shot("tweaks")

## ----eval=FALSE---------------------------------------------------------------
# person_record |>
#   generate_crosstab(marital_status, sex) |>
#   add_facade(
#     border_outer.border = c("top", "bottom"),
#     border_outer.borderStyle = "medium",
#     border_header.border = "bottom",
#     border_header.borderStyle = "thin",
#     body.bandFill = "#F2F2F2"
#   ) |>
#   write_html(path = "rules.html")

## ----eval=FALSE---------------------------------------------------------------
# styled_tbl <- person_record |>
#   generate_frequency(sex) |>
#   add_table_title("Distribution by Sex") |>
#   add_source_note("Source: Survey 2024") |>
#   add_facade(
#     header.fgFill = "#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---------------------------------------------------------------
# tbl <- person_record |>
#   generate_frequency(sex)
# 
# write_xlsx(tbl, path = "custom-styled.xlsx", facade = "my-style.yaml")
# write_docx(tbl, path = "custom-styled.docx", facade = "my-style.yaml")

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

do.call(
  add_facade,
  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)

