## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  warning   = FALSE,
  message   = FALSE
)
library(TernTables)
options(tibble.width = Inf)  # show all columns in printed tibbles
# Output directory for exported .docx files.
# Override by setting options(TernTables.vignette_outdir = "/your/path") before rendering.
out_dir <- getOption("TernTables.vignette_outdir", default = tempdir())

## ----load-data----------------------------------------------------------------
data(tern_colon)

## ----ternP-run, eval = FALSE--------------------------------------------------
# # Load a messy CSV shipped with the package
# path <- system.file("extdata/csv", "tern_colon_messy.csv",
#                     package = "TernTables")
# raw    <- readr::read_csv(path, show_col_types = FALSE)
# result <- ternP(raw)
# # The print method fires automatically, summarising every transformation applied.

## ----ternP-access, eval = FALSE-----------------------------------------------
# result$clean_data    # Cleaned, analysis-ready tibble
# result$sparse_rows   # Rows with >50% NA (retained, not removed — review these)
# result$feedback      # Named list; NULL elements mean no action was taken

## ----ternP-doc, eval = FALSE--------------------------------------------------
# write_cleaning_doc(result,
#                    filename = file.path(out_dir, "cleaning_summary.docx"))

## ----ternP-handoff, eval = FALSE----------------------------------------------
# tbl <- ternG(result$clean_data,
#              exclude_vars = c("ID"),
#              group_var    = "Recurrence")

## ----ternD-example, results = "hide"------------------------------------------
tbl_descriptive <- ternD(
  data             = tern_colon,
  exclude_vars     = c("ID"),
  output_docx      = file.path(out_dir, "Tern_descriptive.docx"),
  methods_filename = file.path(out_dir, "TernTables_methods.docx"),
  open_doc         = FALSE,
  category_start = c(
    "Patient Demographics"  = "Age (yr)",
    "Surgical Findings"     = "Colonic Obstruction",
    "Tumor Characteristics" = "Positive Lymph Nodes (n)",
    "Outcomes"              = "Recurrence"
  )
)
tbl_descriptive

## ----ternD-figure, echo=FALSE, fig.align="center", out.width="45%"------------
knitr::include_graphics("figures/tern_descriptive.png")

## ----ternG-2group, results = "hide"-------------------------------------------
tbl_2group <- ternG(
  data             = tern_colon,
  exclude_vars     = c("ID"),
  group_var        = "Recurrence",
  output_docx      = file.path(out_dir, "Tern_2_group.docx"),
  methods_filename = file.path(out_dir, "TernTables_methods.docx"),
  open_doc         = FALSE,
  OR_col           = TRUE,
  insert_subheads  = TRUE,
  category_start   = c(
    "Patient Demographics"  = "Age (yr)",
    "Surgical Findings"     = "Colonic Obstruction",
    "Tumor Characteristics" = "Positive Lymph Nodes (n)",
    "Treatment Details"     = "Treatment Arm"
  )
)
tbl_2group

## ----ternG-3group, results = "hide"-------------------------------------------
tbl_3group <- ternG(
  data               = tern_colon,
  exclude_vars       = c("ID"),
  group_var          = "Treatment_Arm",
  group_order        = c("Observation", "Levamisole", "Levamisole + 5FU"),
  output_docx        = file.path(out_dir, "Tern_3_group.docx"),
  methods_filename   = file.path(out_dir, "TernTables_methods.docx"),
  open_doc           = FALSE,
  consider_normality = "ROBUST",
  post_hoc           = TRUE,
  category_start     = c(
    "Patient Demographics"  = "Age (yr)",
    "Surgical Findings"     = "Colonic Obstruction",
    "Tumor Characteristics" = "Positive Lymph Nodes (n)",
    "Outcomes"              = "Recurrence"
  )
)
tbl_3group

## ----ternG-fdr, eval = FALSE--------------------------------------------------
# tbl_fdr <- ternG(
#   data             = tern_colon,
#   exclude_vars     = c("ID"),
#   group_var        = "Recurrence",
#   p_adjust         = TRUE,
#   p_adjust_display = "both",   # show raw AND FDR-corrected P values
#   methods_doc      = FALSE
# )

## ----row-pct-example, eval = FALSE--------------------------------------------
# tbl_rowpct <- ternG(
#   data               = tern_colon,
#   vars               = c("Tumor_Differentiation", "Extent_of_Local_Spread",
#                          "Colonic_Obstruction"),
#   group_var          = "Recurrence",
#   percentage_compute = "row",
#   output_docx        = file.path(out_dir, "Tern_row_pct.docx"),
#   open_doc           = FALSE,
#   citation           = FALSE
# )

## ----show-p-example, eval = FALSE---------------------------------------------
# tbl_desc_grouped <- ternG(
#   data         = tern_colon,
#   exclude_vars = c("ID"),
#   group_var    = "Treatment_Arm",
#   show_p       = FALSE,
#   output_docx  = file.path(out_dir, "Tern_no_pval.docx"),
#   open_doc     = FALSE,
#   citation     = FALSE
# )

## ----show-missing-example, eval = FALSE---------------------------------------
# tbl_with_missing <- ternG(
#   data         = tern_colon,
#   exclude_vars = c("ID"),
#   group_var    = "Recurrence",
#   show_missing = TRUE,
#   output_docx  = file.path(out_dir, "Tern_missing.docx"),
#   open_doc     = FALSE,
#   citation     = FALSE
# )

## ----caption-example, eval = FALSE--------------------------------------------
# tbl_descriptive <- ternD(
#   data          = tern_colon,
#   exclude_vars  = c("ID"),
#   output_docx   = file.path(out_dir, "Tern_descriptive.docx"),
#   table_caption = "Table 1. Baseline patient characteristics."
# )

## ----footnote-example, eval = FALSE-------------------------------------------
# tbl_2group <- ternG(
#   data           = tern_colon,
#   exclude_vars   = c("ID"),
#   group_var      = "Recurrence",
#   OR_col         = TRUE,
#   output_docx    = file.path(out_dir, "Tern_2_group.docx"),
#   table_caption  = "Table 2. Characteristics by recurrence status.",
#   table_footnote = c(
#     "Abbreviations: OR, odds ratio; CI, confidence interval.",
#     "\u2020 P values from chi-square or Wilcoxon rank-sum test.",
#     "\u2021 ORs from unadjusted logistic regression."
#   )
# )

## ----font-example, eval = FALSE-----------------------------------------------
# options(TernTables.font_family = "Times New Roman")
# # All subsequent ternG/ternD/word_export calls use Times New Roman

## ----variable-footnote-example, eval = FALSE----------------------------------
# tbl <- ternG(
#   data              = tern_colon,
#   vars              = c("Age_Years", "Sex", "Colonic_Obstruction"),
#   group_var         = "Recurrence",
#   variable_footnote = c(
#     "Age (yr)"            = "Age at registration.",
#     "Colonic Obstruction" = "Defined as complete mechanical obstruction on imaging."
#   ),
#   open_doc          = FALSE,
#   citation          = FALSE
# )

## ----classify-normality-example, eval = FALSE---------------------------------
# norm_tbl <- classify_normality(tern_colon, group_var = "Recurrence")
# print(norm_tbl)

## ----methods-doc, eval = FALSE------------------------------------------------
# write_methods_doc(
#   tbl      = tbl_2group,
#   filename = file.path(out_dir, "Tern_methods.docx")
# )

## ----ternStyle-example, eval = FALSE------------------------------------------
# custom <- tibble::tibble(
#   Variable = c("Institution",            "Study period",
#                "N enrolled",             "Median follow-up (months)"),
#   Value    = c("Single centre",          "2010 \u2013 2022",
#                "47",                     "28.3 [18.1, 40.2]")
# )
# ternStyle(
#   tbl           = custom,
#   table_caption = "Table S1. Study overview.",
#   output_docx   = file.path(out_dir, "Tern_custom.docx"),
#   open_doc      = FALSE,
#   citation      = FALSE
# )

