Compute field-level diffs between two roller state tibbles
Source:R/roller-diff.R
diff_roller_state.RdPure function: takes two flattened roller tibbles (as produced by
flatten_roles() or parse_roles_bulk()) and returns a long-format
changelog recording every field-level mutation. Detects three
categories of change: role additions, role removals, and
field-level modifications on continuing roles.
Arguments
- old_state
Tibble. Previous roller state from
read_state()or an earlierflatten_roles()call.NULLor zero-row tibble treats all current roles as additions.- new_state
Tibble. Current roller state from
brreg_download()orflatten_roles().- timestamp
Character or POSIXct. Timestamp for changelog entries (typically the CDC event time or sync time).
- update_id
Integer or character. Identifier for the sync batch, used as
update_idin the changelog.
Value
A tibble matching the changelog schema: timestamp,
org_nr, registry (always "roller"), change_type
("entry", "exit", "change"), field, value_from,
value_to, update_id.
Composite key
Each role assignment is identified by
(org_nr, role_group_code, role_code, holder_id) where holder_id
is derived from person_id for person-held roles and
entity:{entity_org_nr} for entity-held roles (auditors,
accountants). Roles with neither are keyed as unknown:{row_index}
within their respective state — these are rare and produce
conservative add/remove pairs rather than false modifications.
NA handling
For additions, fields that are NA in the new state are excluded
from the changelog (no value to report). For removals, fields that
are NA in the old state are excluded. For modifications, a change
from NA to a non-NA value (or vice versa) is recorded.
See also
brreg_sync() for automated sync with changelog persistence,
brreg_roles() for fetching current roles,
brreg_changes() for querying stored changelogs.
Other tidybrreg data management functions:
brreg_annotation_summary(),
brreg_annotations(),
brreg_status(),
brreg_sync(),
brreg_sync_status(),
read_changelog()
Examples
if (FALSE) { # interactive() && curl::has_internet()
# Detect board changes for a single company
old <- brreg_roles("923609016")
Sys.sleep(1)
new <- brreg_roles("923609016")
diff_roller_state(old, new)
# Bootstrap: NULL old state treats all roles as entries
roles <- brreg_roles("923609016")
diff_roller_state(NULL, roles)
}