Skip to contents

Scope

A mapping declares correspondences between two independently configured reference catalogues.

Catalogue definitions and lookup behaviour remain in the existing reference configurations.

One survey, two catalogues

The survey model already expects station codes such as 001 and observer codes such as ALICE.

A second catalogue represents the same stations and observers using technical IDs.

The model stays unchanged: the mapping supplies the reference codes it already expects.

All example files live together under examples/survey/ in the installed package.

Download the complete example from Package resources.

Load the catalogues once

Both catalogues are bundled with the package; this example requires no network access.

Cache paths are omitted, so each source receives a temporary cache for this R session.

# Load the reference catalogue and mapping functions.
library(form.model)
# Locate the survey files bundled with the installed package.
resources_directory <- system.file("examples", "survey", package = "form.model")
# Read the source catalogue configuration, which uses technical IDs.
source_references_configuration <- read_reference_configuration(
  file.path(resources_directory, "references-source.yaml"))
# Read the target configuration, which supplies the survey model's codes.
target_references_configuration <- read_reference_configuration(
  file.path(resources_directory, "references.yaml"))
# Bind both configurations to the names used in mapping.yaml.
references_configurations <- list(
  source = source_references_configuration,
  target = target_references_configuration)
# Load the source station and observer tables into a temporary cache.
source_tables <- get_reference_tables(
  c("stations", "observers"), source_references_configuration$catalogues$code_lists$source)
# Load the target station and observer tables into a separate temporary cache.
target_tables <- get_reference_tables(
  c("stations", "observers"), target_references_configuration$catalogues$code_lists$source)

The two station tables describe the same sites with different identifiers.

# Display the source stations and their technical IDs.
knitr::kable(source_tables$data$stations, caption = "Source stations")
Source stations
id name
station-north North station
station-south South station
station-legacy North station (former identifier)
station-unmapped East station
station-conflict Station awaiting identification
station-absent Retired station
# Display the corresponding target stations and their survey codes.
knitr::kable(target_tables$data$stations, caption = "Target stations")
Target stations
code name
001 North station
002 South station
ST01 Survey station one
ST02 Survey station two

The observer tables share names rather than technical IDs.

# Display the source observers, including the deliberate data issues.
knitr::kable(source_tables$data$observers, caption = "Source observers")
Source observers
id name
person-alice Example Alice
person-bob Bob Example
person-unlisted Casey Newcomer
person-no-name
person-duplicate Survey Observer One
person-duplicate Survey Observer Two
person-alice-formatted Example, Alice
# Display the target observer names and codes used by the survey model.
knitr::kable(target_tables$data$observers, caption = "Target observers")
Target observers
code name
ALICE Alice Example
BOB Bob Example
observer-1 Survey Observer One
observer-2 Survey Observer Two

Missing names, duplicate IDs and unmatched entries are deliberate examples of unresolved source data.

Read and prepare the mapping

The resource article presents the mapping used below.

The station mapping uses a CSV; the observer rebound reads the source record’s name property.

The second observer alternative accepts a supplied name directly, without looking up a source record.

The CSV includes duplicate and conflicting pairs so their different outcomes can be demonstrated.

Preparation reuses the verified caches populated above and builds the required indexes.

# Read the mapping declarations and resolve their lookup defaults and file paths.
mapping_configuration <- read_mapping_configuration(file.path(resources_directory, "mapping.yaml"))
# Prepare lookup indexes and correspondences using the cached catalogue data.
mapping_references <- prepare_mapping_references(mapping_configuration, references_configurations, offline = TRUE)

Each statement below displays the source value, target value, resolution status and stage.

Successful correspondences

DIRECT through a CSV

station-north maps to target code 001; the leading zeros are preserved because codes remain text.

# Resolve the station through the CSV and display the four summary columns.
map_reference_values(mapping_references, "stations", "station-north")[, 1:4]
##    source_value target_value   status  stage
## 1 station-north          001 resolved target

REBOUND through a source property

person-alice finds a source observer and reads Example Alice.

The target name lookup normalizes the word order to match Alice Example and returns code ALICE.

# Find the source observer by ID, then match its name in the target catalogue.
map_reference_values(mapping_references, "observers", "person-alice", source_lookup = "id")[, 1:4]
##   source_value target_value   status  stage
## 1 person-alice        ALICE resolved target

DIRECT with unchanged input

Supplying Alice Example directly needs only the target name lookup; no source record is read.

# Send the supplied name directly to the target observer lookup.
map_reference_values(mapping_references, "observers", "Alice Example", source_lookup = "name")[, 1:4]
##    source_value target_value   status  stage
## 1 Alice Example        ALICE resolved target

Catalogue aliases and matching

A source alias applies during rebound

The source ID alias former-alice resolves to person-alice before the name is read.

# Resolve the former source ID through its catalogue alias before reading the name.
map_reference_values(mapping_references, "observers", "former-alice", source_lookup = "id")[, 1:4]
##   source_value target_value   status  stage
## 1 former-alice        ALICE resolved target

A target alias applies after a CSV correspondence

station-legacy maps to OLD-STATION; the target catalogue alias then resolves it to 001.

# Translate the CSV value OLD-STATION through the target catalogue's code alias.
map_reference_values(mapping_references, "stations", "station-legacy")[, 1:4]
##     source_value target_value   status  stage
## 1 station-legacy          001 resolved target

CSV matching does not apply source aliases or case folding

former-north is a source alias, but only exact CSV keys are accepted here.

# Show that a source alias and an uppercase variant are not exact CSV keys.
map_reference_values(mapping_references, "stations", c("former-north", "STATION-NORTH"))[, 1:4]
##    source_value target_value   status          stage
## 1  former-north         <NA> no_match correspondence
## 2 STATION-NORTH         <NA> no_match correspondence

Unchanged input does not apply source aliases

A. Example is an alias in the source catalogue only; sending it directly to the target produces no_match.

# Show that an alias belonging only to the source is unknown to the target lookup.
map_reference_values(mapping_references, "observers", "A. Example", source_lookup = "name")[, 1:4]
##   source_value target_value   status  stage
## 1   A. Example         <NA> no_match target

Catalogue lookups retain their configured case handling

The source ID lookup ignores case, so PERSON-BOB resolves to observer BOB.

# Resolve the uppercase ID using the source lookup's ignore_case setting.
map_reference_values(mapping_references, "observers", "PERSON-BOB", source_lookup = "id")[, 1:4]
##   source_value target_value   status  stage
## 1   PERSON-BOB          BOB resolved target

Normalized names work after rebound

person-alice-formatted supplies Example, Alice; the target normalizes this to match Alice Example.

# Match the source name Example, Alice after normalizing punctuation and word order.
map_reference_values(mapping_references, "observers", "person-alice-formatted", source_lookup = "id")[, 1:4]
##             source_value target_value   status  stage
## 1 person-alice-formatted        ALICE resolved target

Name normalization is not fuzzy matching

Word order and punctuation normalize, but a misspelling such as Alic Example remains unmatched.

# Compare a normalized match with a misspelling that remains unresolved.
map_reference_values(mapping_references, "observers", c("Example, Alice", "Alic Example"), source_lookup = "name")[, 1:4]
##     source_value target_value   status  stage
## 1 Example, Alice        ALICE resolved target
## 2   Alic Example         <NA> no_match target

Unresolved inputs

Missing input

NULL, NA and empty text produce missing_input before either catalogue is queried.

# Show the missing_input result for each supported representation of absent input.
map_reference_values(mapping_references, "stations", list(NULL, NA_character_, ""))[, 1:4]
##   source_value target_value        status stage
## 1         <NA>         <NA> missing_input input
## 2         <NA>         <NA> missing_input input
## 3                      <NA> missing_input input

Missing source record

person-absent has no source observer record, so rebound stops with missing_source_record.

# Report a missing source record without attempting a target lookup.
map_reference_values(mapping_references, "observers", "person-absent", source_lookup = "id")[, 1:4]
##    source_value target_value                status  stage
## 1 person-absent         <NA> missing_source_record source

Missing source property

person-no-name exists but has no name to submit to the target, producing missing_property.

# Report that the source observer exists but its required name is empty.
map_reference_values(mapping_references, "observers", "person-no-name", source_lookup = "id")[, 1:4]
##     source_value target_value           status  stage
## 1 person-no-name         <NA> missing_property source

No CSV correspondence

station-unmapped exists in the source catalogue but has no CSV correspondence.

# Report that the supplied station ID has no entry in the correspondence CSV.
map_reference_values(mapping_references, "stations", "station-unmapped")[, 1:4]
##       source_value target_value   status          stage
## 1 station-unmapped         <NA> no_match correspondence

No automatic fallback to the input

Although 001 is a valid target code, it has no source key in this CSV and remains unresolved.

# Show that an absent CSV key does not fall back to a direct target code lookup.
map_reference_values(mapping_references, "stations", "001")[, 1:4]
##   source_value target_value   status          stage
## 1          001         <NA> no_match correspondence

No target match after a CSV correspondence

station-absent has a correspondence to 999, but that code is absent from the target catalogue.

# Report that the CSV's target value 999 is absent from the target catalogue.
map_reference_values(mapping_references, "stations", "station-absent")[, 1:4]
##     source_value target_value   status  stage
## 1 station-absent         <NA> no_match target

No target match after rebound

person-unlisted supplies Casey Newcomer, who has no target observer record.

# Report that the source observer's name has no target match.
map_reference_values(mapping_references, "observers", "person-unlisted", source_lookup = "id")[, 1:4]
##      source_value target_value   status  stage
## 1 person-unlisted         <NA> no_match target

Ambiguous source records

Two source observers share person-duplicate; rebound returns ambiguous and retains both source row indexes.

# Retain both source row indexes when a technical ID identifies two observers.
map_reference_values(mapping_references, "observers", "person-duplicate", source_lookup = "id")[
  c("source_value", "target_value", "status", "stage", "source_rows")]
##       source_value target_value    status  stage source_rows
## 1 person-duplicate         <NA> ambiguous source        5, 6

Conflicting CSV correspondences

station-conflict has two different target values; both are retained without selecting either one.

# Display both conflicting correspondence values without choosing a target.
map_reference_values(mapping_references, "stations", "station-conflict")[
  c("source_value", "target_value", "status", "stage", "correspondence_values")]
##       source_value target_value    status          stage correspondence_values
## 1 station-conflict         <NA> ambiguous correspondence              001, 002

Selection, reuse and provenance

Identical pairs do not create ambiguity; repeated inputs retain their order

The repeated station-north,001 pair represents one correspondence, and every input still gets its own result row.

# Preserve input order and repeated result rows while deduplicating identical CSV pairs.
map_reference_values(mapping_references, "stations", c("station-south", "station-north", "station-north"))[, 1:4]
##    source_value target_value   status  stage
## 1 station-south          002 resolved target
## 2 station-north          001 resolved target
## 3 station-north          001 resolved target

Alternatives require explicit selection

An observer input could be an ID or a name, so omitting the selector produces a configuration error.

# Display the selector error as text so the remaining examples can continue.
tryCatch(map_reference_values(mapping_references, "observers", "person-alice"), error = conditionMessage)
## [1] "Mapping selectors must identify exactly one alternative for observers."

Prepare only the required target

Preparing only stations needs the target station data and CSV; source rows and observer data are unnecessary.

# Prepare only the target station table and its correspondence CSV.
mapping_station_references <- prepare_mapping_references(
  mapping_configuration, references_configurations, targets = "stations", offline = TRUE)
# Resolve a station using that smaller prepared selection.
map_reference_values(mapping_station_references, "stations", "station-north")[, 1:4]
##    source_value target_value   status  stage
## 1 station-north          001 resolved target

Unmapped source records do not prevent preparation; resolution considers only the supplied input values.

Preserve provenance

Results identify the selected mapping, target catalogue revision and correspondence checksum.

# Retain a complete mapping result, including its provenance attribute.
result <- map_reference_values(mapping_references, "stations", "station-north")
# Extract the provenance recorded during preparation and resolution.
provenance <- attr(result, "provenance")
# Display the selected source table, lookup, correspondence file and target property.
provenance$mapping
## $source
## $source$table
## [1] "stations"
## 
## $source$lookup
## [1] "id"
## 
## 
## $file
## [1] "/tmp/RtmpQGdfYQ/temp_libpath82e256c0e1d/form.model/examples/survey/correspondences/stations.csv"
## 
## $lookup
## [1] "code"
## 
## $property
## [1] "code"
# Display the target table identity and its published data version.
provenance$target_catalogue$table[c("id", "version")]
##         id version
## 3 stations   1.1.0
# Display the checksum identifying the correspondence file used for this result.
provenance$correspondence$sha256
## [1] "5511e117e66d622ce64057ae7fb66a6a340d9668f13e9842ce42f0b52e156567"

Clean up temporary caches

Remove all temporary caches created by the example after the last mapping statement.

# Visit both source and target configurations.
for (references_configuration in references_configurations) {
  # Visit each catalogue configured on that side of the mapping.
  for (catalogue in references_configuration$catalogues) {
    # Remove its temporary cache after all examples have finished.
    unlink(catalogue$source$cache, recursive = TRUE)
  }
}

Configuration resources

See Package resources for the schema, fields and defaults.

Correspondence sources

Property from a source record

Rebound follows three steps:

  1. Find the source record using the effective source lookup.
  2. Read the column named by source.property and submit its value to the target lookup.
  3. Return the target record’s configured property.
  • Both lookups retain their configured aliases, case handling and normalized-name matching.
  • A missing or ambiguous source record remains unresolved before property extraction.

Explicit correspondence file

The correspondence file contract defines the CSV format and paths.

Resolution follows two steps:

  1. Match the supplied input exactly and case-sensitively against source_value.
  2. Submit the corresponding target_value to the target lookup, using that lookup’s matching behaviour.

The source lookup identifies the input column; it does not load a source record or apply source aliases.

Repeated correspondences:

  • Identical duplicate pairs represent one correspondence.
  • Several source values may share a target.
  • Conflicting targets for one source value remain ambiguous.

If no correspondence exists: the input remains unresolved, without falling back to the input or another value.

Unchanged input

Without source.property or file, submit the supplied value directly to the target lookup.

  • No source catalogue data is needed.
  • No source aliases are resolved.

Catalogue matching

Catalogue lookups reuse reference_index() and reference_match_rows() in this order:

  1. Exact matching.
  2. Catalogue aliases.
  3. Configured normalized-name matching.

Keep these distinctions in mind:

  • Correspondence files remain separate from catalogue aliases.
  • Multiple matching records remain candidates, even when their requested property values agree.
  • Name normalization supports comparison; it does not prove identity.

Validation boundaries

Validation happens in stages:

  1. YAML structure: check the configuration, lookup defaults and duplicate mapping selections without loading data.
  2. Declarations: check configuration names, catalogue IDs, table IDs and lookups against reference configurations.
  3. Loaded resources: check actual columns and values; check correspondence-file existence and CSV structure.

Within a target table, the source table, source lookup, target lookup and returned property identify an alternative.

Duplicate alternatives and ambiguous selections are rejected; their position in the list never decides the result.

During resolution:

  • Rebound needs both catalogues’ data; CSV and unchanged-input mappings need target data only.
  • Check coverage only for encountered inputs and reuse lookups for repeated values within the same mapping.
  • Distinguish missing input, missing source records, missing properties, no match and ambiguity.
  • Retain source and target values, the selected mapping, catalogue revisions and correspondence-file checksum.

The caller decides whether an unresolved result blocks a field, row or export.

No correspondence silently substitutes zero, false or an unknown code.

R API

From configuration to results

  1. Read with read_mapping_configuration(path): validate YAML, apply lookup defaults and make file paths absolute.
  2. Check declarations with resolve_mapping_lookups(): verify configuration, catalogue, table and lookup selections.
  3. Prepare with prepare_mapping_references(): load selected mappings and their dependencies, and build indexes.
  4. Resolve with map_reference_values(): translate encountered values for one selected mapping.

Reading the YAML does not open correspondence files or retrieve catalogue data.

Control preparation and selection

  • Omit targets to prepare all mappings, or supply only the target tables needed by the current dataset.
  • Preparation shares retrieved tables and lookup indexes; only rebound loads source data.
  • Use source_table, source_lookup, target_lookup and property to select one alternative when needed.
  • Selection must identify exactly one alternative before any values are resolved.

Read the result

The result contains one row per input, preserving order and duplicates; repeated inputs share resolution work.

The status column explains the outcome:

Status Meaning
resolved A single target value was resolved.
missing_input The input is NULL, NA or empty text.
missing_source_record The source lookup found no record.
missing_property A required source or target property is empty.
no_match There is no CSV correspondence or no target match.
ambiguous Multiple records or conflicting CSV target values remain.

The stage column locates it: input, source, correspondence or target.

Only resolved rows receive a target_value. Numeric and logical inputs are rejected rather than converted.

Additional details remain available:

  • source_rows and target_rows: matching row indexes.
  • target_candidates: candidate target values.
  • correspondence_values: values obtained before the target lookup.
  • The provenance attribute: selected mapping, YAML checksum, catalogue revisions and correspondence checksum.

Ambiguous source rows remain unresolved even when their extracted properties agree.

Handle errors separately

Invalid declarations, malformed files, missing columns and retrieval failures raise errors before row resolution.

The caller handles these errors and decides the consequences of unresolved row statuses.