apply_role_overrides: Force roles onto a spec by column name or name pattern

View source: R/spec.r

apply_role_overridesR Documentation

Force roles onto a spec by column name or name pattern

Description

Used by infer_spec() and by synthesize_file() (where it also applies to a reviewed spec read from disk, so identifiers named in the call always win). Names match the internal or the on-disk column name, ignoring case; patterns are regular expressions (perl, case-insensitive) matched against the on-disk name. The options cellsynth.id, cellsynth.id_pattern, cellsynth.text and cellsynth.text_pattern add session-wide defaults, e.g. options(cellsynth.id_pattern = "^(pat|subj)") once per session.

Usage

apply_role_overrides(
  spec,
  id = NULL,
  text = NULL,
  roles = NULL,
  id_pattern = NULL,
  text_pattern = NULL,
  warn_unknown = TRUE
)

Arguments

spec

A spec data.frame.

id, text

Column names to force to id / text.

roles

Named character vector, c(column = "role"), for any role.

id_pattern, text_pattern

Regular expressions on column names.

warn_unknown

Warn about names in id/text/roles that match no column.

Value

The spec with roles replaced and reason set to "user: ...".

Examples

df <- data.frame(PatKey = sprintf("%04d", 1:60), Visit_Note = "seen",
                 hb = round(seq(110, 160, length.out = 60)))
path <- file.path(tempdir(), "overrides.csv")
utils::write.csv(df, path, row.names = FALSE)
spec <- infer_spec(read_source(path))
spec <- apply_role_overrides(spec, id = "patkey", text_pattern = "note$")
spec[, c("orig_name", "role", "reason")]

cellsynth documentation built on Oct. 31, 2026, 1:07 a.m.