## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(echo = TRUE, eval = TRUE, collapse = TRUE, comment = "#>")
# The whole demo lives in a throwaway deployment root under tempdir(); every
# chunk below runs from there, exactly as a real deployment runs from its
# own root.
demo_root <- file.path(tempdir(), "dq-demo")
dir.create(demo_root, showWarnings = FALSE)
knitr::opts_knit$set(root.dir = demo_root)

## ----delivery-----------------------------------------------------------------
writeLines(c(
  "Date,Amount,Currency,Amount,Status",
  "2026-07-01,100.00,AUD,15.00,settled",
  "2026-07-02,250.50,AUD,0.00,settled",
  "2026-07-03,80.25,NZD,8.10,pending"
), "orders.csv")

## ----generate-----------------------------------------------------------------
library(dqcheckr)
generate_global_config(config_dir = "config")
generate_dataset_config("orders.csv", config_dir = "config")

## ----show-config--------------------------------------------------------------
writeLines(readLines(file.path("config", "orders.yml")))

## ----edit---------------------------------------------------------------------
cfg_path <- file.path("config", "orders.yml")
lines <- readLines(cfg_path)
lines <- sub("^# rule_overrides:", "rule_overrides:", lines)
lines <- sub("^#   min_row_count: 1", "  min_row_count: 1", lines)
writeLines(lines, cfg_path)

## ----validate-clean-----------------------------------------------------------
validate_config("orders", config_dir = "config")

## ----validate-typo------------------------------------------------------------
writeLines(c(lines, 'delimitter: ";"'), cfg_path)
validate_config("orders", config_dir = "config")
writeLines(lines, cfg_path)   # undo

## ----run----------------------------------------------------------------------
result <- run_dq_check("orders", config_dir = "config", open_report = FALSE)
result$status

## ----list-runs----------------------------------------------------------------
runs <- list_runs("orders", config_dir = "config")
runs[, c("id", "run_timestamp", "overall_status",
         "check_pass_count", "check_warn_count", "check_fail_count")]

## ----list-snapshots-----------------------------------------------------------
list_snapshots(db_path = file.path("data", "snapshots.sqlite"))[
  , c("id", "dataset_name", "run_timestamp", "overall_status")]

## ----second-delivery----------------------------------------------------------
writeLines(c(
  "Date,Amount,Currency,Amount,Status",
  "2026-08-01,900.00,AUD,15.00,settled",
  "2026-08-02,1250.50,AUD,0.00,",
  "2026-08-03,880.25,NZD,8.10,pending"
), "orders.csv")
result2 <- run_dq_check("orders", config_dir = "config", open_report = FALSE)

## ----drift--------------------------------------------------------------------
cmp <- compare_snapshots("orders", config_dir = "config",
                         report = FALSE, open_report = FALSE)
names(cmp)

## ----spread-unset-------------------------------------------------------------
cmp$spread_changes[, c("Column", "numeric_sd_prev", "numeric_sd_curr",
                       "numeric_sd_shift_pct", "numeric_sd_exceeds")]

## ----spread-rule--------------------------------------------------------------
gcfg_path <- file.path("config", "dqcheckr.yml")
glines <- readLines(gcfg_path)
glines <- sub("^# default_rules:", "default_rules:", glines)
glines <- sub("^#   max_numeric_sd_shift_pct:.*",
              "  max_numeric_sd_shift_pct: 0.2", glines)
writeLines(glines, gcfg_path)

cmp2 <- compare_snapshots("orders", config_dir = "config",
                          report = FALSE, open_report = FALSE)
cmp2$spread_changes[, c("Column", "numeric_sd_shift_pct", "numeric_sd_exceeds")]

## ----fwf-packed---------------------------------------------------------------
writeLines(c("AB12XY0099QQWW2026",
             "CD34ZW0100RRTT2026"), "ledger.txt")
generate_dataset_config("ledger.txt", config_dir = "config")
validate_config("ledger", config_dir = "config")

## ----never-overwrite, error=TRUE----------------------------------------------
try({
generate_dataset_config("orders.csv", config_dir = "config")
})

## ----cleanup, include=FALSE---------------------------------------------------
# Remove the demo's contents but keep the directory itself: it is knitr's
# working directory for this document, and the session tempdir is removed on
# exit anyway.
unlink(list.files(demo_root, full.names = TRUE), recursive = TRUE)

