## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width  = 7,
  fig.height = 5,
  fig.align  = "center",
  eval = TRUE
)

## ----install, eval = FALSE----------------------------------------------------
# # install.packages("pak")
# pak::pak("drpakhare/stepssurvey")

## ----install-devtools, eval = FALSE-------------------------------------------
# # install.packages("devtools")
# devtools::install_github("drpakhare/stepssurvey")

## ----library------------------------------------------------------------------
library(stepssurvey)

## ----generate-----------------------------------------------------------------
raw <- generate_test_data(n = 3000, seed = 42)
dim(raw)
names(raw)

## ----import, eval = FALSE-----------------------------------------------------
# # CSV
# raw <- import_steps_data("data/raw/steps_survey_2024.csv")
# 
# # Excel
# raw <- import_steps_data("data/raw/steps_survey_2024.xlsx")
# 
# # Stata (.dta) -- common for STEPS exports
# raw <- import_steps_data("data/raw/steps_survey_2024.dta")
# 
# # SPSS (.sav)
# raw <- import_steps_data("data/raw/steps_survey_2024.sav")

## ----detect-------------------------------------------------------------------
cols <- detect_steps_columns(raw)

## ----detect-inspect-----------------------------------------------------------
# Which column was matched for fasting glucose?
cols$fasting_glucose

# Which column for SBP reading 1?
cols$sbp1

# How many columns were detected?
sum(!sapply(cols, is.null))

## ----override, eval = FALSE---------------------------------------------------
# cols$fasting_glucose <- "my_custom_glucose_variable"

## ----mapping-template, eval = FALSE-------------------------------------------
# # Copy the template to your working directory
# file.copy(
#   system.file("templates", "column_mapping_template.xlsx", package = "stepssurvey"),
#   file.path(tempdir(), "my_mapping.xlsx")
# )

## ----mapping-read, eval = FALSE-----------------------------------------------
# cols <- read_column_mapping("my_mapping.xlsx", data = raw)

## ----mapping-pipeline, eval = FALSE-------------------------------------------
# result <- run_steps_pipeline(
#   "my_data.dta",
#   country_name = "My Country",
#   survey_year  = 2024,
#   mapping_file = "my_mapping.xlsx"
# )

## ----clean--------------------------------------------------------------------
clean <- clean_steps_data(raw, cols, age_min = 18, age_max = 69)
dim(clean)

## ----thresholds, eval = FALSE-------------------------------------------------
# clean <- clean_steps_data(raw, cols,
#   bp_sbp_threshold          = 130,   # SBP threshold (default 140)
#   bp_dbp_threshold          = 80,    # DBP threshold (default 90)
#   bmi_overweight             = 25.0,  # BMI overweight (default 25)
#   bmi_obese                  = 30.0,  # BMI obese (default 30)
#   glucose_threshold          = 7.0,   # Raised glucose mmol/L (default 7.0)
#   glucose_impaired_threshold = 6.1,   # Impaired glucose mmol/L (default 6.1)
#   chol_threshold             = 5.0    # Raised cholesterol mmol/L (default 5.0)
# )

## ----clean-inspect------------------------------------------------------------
# BMI categories
table(clean$bmi_category, clean$sex)

# Blood pressure staging
table(clean$bp_stage)

# Physical activity levels
table(clean$pa_category, clean$sex)

## ----quality, eval = FALSE----------------------------------------------------
# quality <- steps_data_quality(clean)
# names(quality)
# # [1] "digit_preference" "completeness" "plausibility" "weights"

## ----digit-plot, eval = FALSE-------------------------------------------------
# plot_digit_preference(quality, measure = "sbp")

## ----completeness-plot, eval = FALSE------------------------------------------
# plot_completeness(quality)

## ----weights-plot, eval = FALSE-----------------------------------------------
# plot_weights(quality)

## ----design-------------------------------------------------------------------
designs <- setup_survey_design(clean)

## ----indicators---------------------------------------------------------------
result <- compute_all_indicators(designs)

## ----key----------------------------------------------------------------------
result$key_indicators

## ----domain, eval = FALSE-----------------------------------------------------
# tob  <- compute_tobacco_indicators(designs$step1)
# alc  <- compute_alcohol_indicators(designs$step1)
# diet <- compute_diet_pa_indicators(designs$step1)
# anth <- compute_anthropometry_indicators(designs$step2)
# bp   <- compute_bp_indicators(designs$step2)
# bio  <- compute_biochemical_indicators(designs$step3)

## ----tob-example--------------------------------------------------------------
tob <- compute_tobacco_indicators(designs$step1)
names(tob)

# Overall prevalence of current tobacco use
tob$current_tobacco_total

# Prevalence by sex
tob$current_tobacco_by_sex

## ----custom-------------------------------------------------------------------
# Weighted proportion with 95% CI (raised_bp is a Step 2 variable)
svyprop(~raised_bp, designs$step2)

# Stratified by sex
svyprop(~raised_bp, designs$step2, by = ~sex)

# Weighted mean with 95% CI
svymn(~mean_sbp, designs$step2, by = ~sex)

## ----tables-------------------------------------------------------------------
tables <- build_steps_tables(result$results)
names(tables)

## ----table-show, eval = FALSE-------------------------------------------------
# # Display the raised blood pressure table
# tables$raised_bp
# 
# # Export to Word
# flextable::save_as_docx(tables$raised_bp, path = file.path(tempdir(), "bp_table.docx"))

## ----detailed-tables, eval = FALSE--------------------------------------------
# # Step 1: Compute raw results from the table registry
# computed <- compute_all_tables(designs)
# 
# # Step 2: Format into flextable objects with WHO styling
# detailed <- build_all_tables(computed)
# names(detailed)  # e.g. "T_current_smokers", "M_bp_mean", "B_glucose_raised"

## ----registry, eval = FALSE---------------------------------------------------
# # Browse the full table registry
# registry <- steps_table_registry()
# 
# # Get all tables for a specific section
# bp_entries <- get_registry_by_section("Blood Pressure")
# 
# # Get all Step 2 tables
# step2_entries <- get_registry_by_step(2)
# 
# # List available sections
# list_registry_sections()

## ----plots, fig.width = 8, fig.height = 5-------------------------------------
plots <- build_steps_plots(
  indicators    = result$results,
  key_indicators = result$key_indicators,
  country_name  = "Exampleland",
  survey_year   = 2024
)
names(plots)

## ----overview, fig.width = 8, fig.height = 5----------------------------------
plots$overview

## ----dashboard, fig.width = 10, fig.height = 7, eval = FALSE------------------
# plots$sex_dashboard

## ----age-trend, fig.width = 8, fig.height = 4.5-------------------------------
plots$bp_by_age

## ----forest, fig.width = 8, fig.height = 6, eval = FALSE----------------------
# plots$forest
# # Or build standalone:
# build_forest_plot(result$key_indicators, "Exampleland", 2024)

## ----radar, fig.width = 7, fig.height = 7, eval = FALSE-----------------------
# plots$radar
# # Or build standalone:
# build_radar_plot(result$key_indicators, "Exampleland", 2024)

## ----save-plots, eval = FALSE-------------------------------------------------
# save_steps_plots(plots, output_dir = file.path(tempdir(), "figures"))
# # Creates:
# #   outputs/figures/01_overview_indicators.png
# #   outputs/figures/02_by_sex_dashboard.png
# #   outputs/figures/03_bp_by_age.png
# #   outputs/figures/04_obesity_by_age.png
# #   outputs/figures/05_forest_plot.png
# #   outputs/figures/06_radar_plot.png

## ----theme, fig.width = 6, fig.height = 3.5-----------------------------------
pal <- steps_colors()
str(pal)

library(ggplot2)
ggplot(clean, aes(x = age_group, fill = sex)) +
  geom_bar(position = "dodge") +
  scale_fill_manual(values = c(Male = pal$male, Female = pal$female)) +
  theme_steps() +
  labs(title = "Sample distribution by age and sex")

## ----reports, eval = FALSE----------------------------------------------------
# cfg <- steps_config(
#   data_path    = "data/raw/steps_survey_2024.csv",
#   country_name = "Exampleland",
#   survey_year  = 2024,
#   age_min      = 18,
#   age_max      = 69
# )
# 
# # Fact sheet -- one-page overview (HTML for sharing, Word for print)
# render_fact_sheet(cfg, output_dir = "outputs", format = "html")
# render_fact_sheet(cfg, output_dir = "outputs", format = "word")
# 
# # Summary report -- narrative with key findings, charts, recommendations
# render_country_report(cfg, output_dir = "outputs")
# 
# # Data book -- detailed WHO 3-panel tables by domain
# render_data_book(cfg, output_dir = "outputs")

## ----pipeline, eval = FALSE---------------------------------------------------
# out <- run_steps_pipeline(
#   data_path      = "data/raw/steps_survey_2024.csv",
#   country_name   = "Exampleland",
#   survey_year    = 2024,
#   age_min        = 18,
#   age_max        = 69,
#   output_dir     = "outputs",
#   render_reports = TRUE
# )
# 
# # Access any intermediate result
# out$raw_data
# out$clean_data
# out$design
# out$indicators
# out$key_indicators
# out$tables
# out$plots

## ----pipeline-mapping, eval = FALSE-------------------------------------------
# out <- run_steps_pipeline(
#   "my_data.dta",
#   country_name = "My Country",
#   survey_year  = 2024,
#   mapping_file = "my_mapping.xlsx"
# )

## ----custom-names, eval = FALSE-----------------------------------------------
# raw  <- import_steps_data("my_steps_data.csv")
# cols <- detect_steps_columns(raw)
# cols$fasting_glucose <- "blood_sugar_fasting"
# cols$sbp1 <- "systolic_bp_1"
# clean <- clean_steps_data(raw, cols)

## ----age-range, eval = FALSE--------------------------------------------------
# # Include up to age 79
# clean <- clean_steps_data(raw, cols, age_min = 18, age_max = 79)

## ----worked, fig.width = 8, fig.height = 5------------------------------------
library(stepssurvey)

# 1. Generate a realistic test dataset
raw <- generate_test_data(n = 3000, seed = 42)

# 2. Detect standard STEPS variable columns
cols <- detect_steps_columns(raw)

# 3. Clean data and derive all indicators
clean <- clean_steps_data(raw, cols, age_min = 18, age_max = 69)

# 4. Create the complex survey design
designs <- setup_survey_design(clean)

# 5. Compute all NCD risk factor indicators
result <- compute_all_indicators(designs)

# 6. View headline estimates
result$key_indicators

# 7. Build formatted tables
tables <- build_steps_tables(result$results)

# 8. Build visualisations
plots <- build_steps_plots(
  indicators     = result$results,
  key_indicators = result$key_indicators,
  country_name   = "Exampleland",
  survey_year    = 2024
)

# 9. Display the overview chart
plots$overview

## ----shiny, eval = FALSE------------------------------------------------------
# library(stepssurvey)
# run_app()

## ----cite, eval = FALSE-------------------------------------------------------
# citation("stepssurvey")

## ----session------------------------------------------------------------------
sessionInfo()

