Build a minimal executable metacore object from a spec data frame
Source:R/metacore_lite.R
mock_metacore.RdConstructs a `Metacore` object via [metacore::metacore()] from a `read_spec_df()`-compatible spec data frame plus user-supplied codelists, so that `codelist_var` layer code (i.e. [metatools::create_var_from_codelist()]) runs in demos/tests without a vendor P21 spec.
Codelist linking: a spec variable is linked to codelist `N` when the spec `codelist` column equals `N`, or when the variable's derivation text mentions `N` (word boundary, case-insensitive; e.g. "using the RACE codelist" links to `RACE`). Real decodes cannot be invented from the spec text: supply `codelists` (built by hand or with [codelist_from_data()]); variables whose derivation mentions a codelist but have no matching entry get no `code_id` and emit a warning.
When the spec covers exactly one dataset, the returned object is already subsetted with [metacore::select_dataset()] so it can be passed directly as `metacore = mc` to metatools; with multiple datasets the full object is returned and the caller subsets it.
Usage
mock_metacore(spec_df, codelists = list())Arguments
- spec_df
Spec data.frame compatible with [read_spec_df()] (columns dataset, variable, label, type, origin, derivation; optional length, codelist).
- codelists
Named list of codelists, each a data.frame with columns `code` and `decode` (e.g. `list(RACE = codelist_from_data("RACE", dm))`). The list name becomes the metacore `code_id`.
Value
A `Metacore` object (single-dataset specs: subsetted via `select_dataset()`; multi-dataset specs: full object). NULL with a warning if `metacore` is not installed.
Examples
if (FALSE) { # \dontrun{
# Requires the 'metacore' package:
mc <- mock_metacore(mock_spec_adsl(),
codelists = list(RACE = data.frame(code = 1:3, decode = c("ASIAN", "BLACK", "WHITE")))
)
} # }