Skip to content

Refactor validation into step-by-step, user-facing data checks with CSV support #7

Description

@koito19960406

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

  1. File format check: readable? required columns present? Auto-detect adminXPcod column, identify indicator columns (numeric).
  2. 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).
  3. Data type validation: indicator columns numeric? NA percentage? Per-column summary (type, NA%, range).
  4. Geometry compatibility (custom shapefile only): valid geometries? Proper admin hierarchy? Skip for off-the-shelf global shapes.
  5. Cross-reference: admin levels in data match shapefile layers?
  6. 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

  • All 6 validation steps implemented as pure functions
  • CSV upload supported alongside Excel
  • Auto-generate metadata from bare CSV (only adminXPcod + numeric cols needed)
  • Shiny UI shows step-by-step progress with pass/warn/fail per step
  • ID mismatch report shows specific unmatched IDs
  • Existing validate_metadata/validate_read_shp still work (backward compat)
  • Unit tests for each validation step
  • Integration test with sample Ukraine data

Metadata

Metadata

Assignees

No one assigned

    Labels

    data-qualityData validation and quality checksenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions