Problem
Current validation (fct_validate_metadata.R, validators.R) has issues:
- Uses
testthat (a dev testing framework) for runtime validation
- Outputs go to R console via
rlang::inform/warn/abort, not to the Shiny UI
- All-or-nothing: either passes or fails with no step-by-step guidance
- No CSV support (only Excel .xlsx and RDS shapefiles)
- No ID mismatch detection between user data and shapefiles
- No preview of matched vs unmatched records
- No data type/range validation for indicator values
Context: metadata column burden
The current metadata sheet requires 14 columns. Analysis shows only 1 is truly user-provided (var_code = column names). The other 13 can be auto-generated with sensible defaults:
Minimum viable CSV: just adminXPcod + numeric indicator columns. The system should auto-generate the full metadata structure:
- var_name = var_code (column name)
- var_order = row number
- spatial_level = inferred from which adminXPcod is present
- pillar_group = 1, pillar_name = "Indicators"
- All fltr_* = FALSE, legend_revert_colours = FALSE
Vision
When users upload their own data (CSV or Excel) to supplement the global H3 database (#5), they need clear, step-by-step feedback:
Proposed validation steps
- File format check: readable? required columns present? Auto-detect adminXPcod column, identify indicator columns (numeric).
- ID matching: auto-detected ID column vs shapefile IDs. Report: X of Y IDs matched, Z unmatched (list them), W shapefile regions with no data (list them).
- Data type validation: indicator columns numeric? NA percentage? Per-column summary (type, NA%, range).
- Geometry compatibility (custom shapefile only): valid geometries? Proper admin hierarchy? Skip for off-the-shelf global shapes.
- Cross-reference: admin levels in data match shapefile layers?
- Dry-run PTI: auto-generate metadata, run one weight combo, confirm output.
Auto-generation function
New function generate_metadata_from_csv(df, shapefile):
- Detects adminXPcod columns -> determines spatial_level
- Remaining numeric columns -> var_code entries
- Builds the full 14-column metadata tibble with defaults
- Returns the list structure that mod_calc_pti2 expects
Architecture
- New
R/fct_validate_user_data.R: pure functions, each returning list(status = "pass"|"warn"|"fail", message = "...", details = tibble(...))
- New
R/fct_generate_metadata.R: auto-generate metadata from bare CSV
- New
R/mod_validate_upload.R: Shiny module rendering checklist UI
- Refactor existing validators to return structured results (backward-compatible via
return_structured parameter)
- New
tests/testthat/test-validate-user-data.R
Key principle
Validation is decoupled from ingestion. Validate first, show all results, ingest only when checks pass (or user acknowledges warnings).
Acceptance criteria
Problem
Current validation (fct_validate_metadata.R, validators.R) has issues:
testthat(a dev testing framework) for runtime validationrlang::inform/warn/abort, not to the Shiny UIContext: metadata column burden
The current metadata sheet requires 14 columns. Analysis shows only 1 is truly user-provided (var_code = column names). The other 13 can be auto-generated with sensible defaults:
Minimum viable CSV: just
adminXPcod+ numeric indicator columns. The system should auto-generate the full metadata structure:Vision
When users upload their own data (CSV or Excel) to supplement the global H3 database (#5), they need clear, step-by-step feedback:
Proposed validation steps
Auto-generation function
New function
generate_metadata_from_csv(df, shapefile):Architecture
R/fct_validate_user_data.R: pure functions, each returninglist(status = "pass"|"warn"|"fail", message = "...", details = tibble(...))R/fct_generate_metadata.R: auto-generate metadata from bare CSVR/mod_validate_upload.R: Shiny module rendering checklist UIreturn_structuredparameter)tests/testthat/test-validate-user-data.RKey principle
Validation is decoupled from ingestion. Validate first, show all results, ingest only when checks pass (or user acknowledges warnings).
Acceptance criteria