Convert recodeflow metadata to a MockData specification
Source:R/mock_spec_recodeflow.R
mock_spec_from_recodeflow.Rdmock_spec_from_recodeflow() adapts recodeflow-style variables and
variable_details metadata into the normalized v0.4 mock_spec shape. It
returns a validated specification; it does not generate data.
Usage
mock_spec_from_recodeflow(
variables,
variable_details = NULL,
databaseStart = NULL,
role = "enabled",
exclude_derived = TRUE,
spec_version = .mock_spec_version,
model_hint = "auto"
)Arguments
- variables
Data frame or CSV path for recodeflow-style
variablesmetadata.- variable_details
Data frame, CSV path, or
NULLfor recodeflow-stylevariable_detailsmetadata.- databaseStart
Optional database/cycle token used to filter metadata by exact comma-separated
databaseStartvalues.- role
Character vector of role tokens to include. Defaults to
"enabled". UseNULLto skip role filtering.- exclude_derived
Logical. If
TRUE, exclude variables identified byDerivedVar::orFunc::rows invariable_details.- spec_version
Character version of the specification shape.
- model_hint
Backend hint for the returned specification.
Details
This adapter preserves recodeflow semantics instead of treating metadata as a
generic table. It uses exact role and databaseStart token matching, parses
valid ranges from recStart, classifies missing codes from recEnd values
that begin with NA:: (expanding an integer range such as [997,999] into
its individual codes, with the row's proportion split equally), preserves
categorical levels and proportions, carries
garbage_* settings into garbage_rules, and builds a survival date
(mock_spec_survival()) from any date row that sets anchor, reading
censored_by, followup_min, followup_max, event_prop, distribution,
shape and rate. A date row with survival parameters but no anchor is
an error.
By default, variables identified by DerivedVar:: or Func:: rows are
excluded because they should be evaluated after raw mock variables are
generated. Set exclude_derived = FALSE only when you want those rows to
appear in the adapter input and fail or be handled by later formula support.
CSV path inputs are read with stringsAsFactors = FALSE,
check.names = FALSE, and na.strings = c("", "NA") so path-based inputs
preserve recodeflow column names and treat blank metadata cells like missing
values. The adapter normalizes rType = "numeric" to "double" to match
the v0.4 mock_spec type vocabulary.
Examples
variables <- data.frame(
variable = "age",
variableType = "Continuous",
rType = "integer",
role = "enabled",
distribution = "uniform"
)
details <- data.frame(
variable = "age",
recStart = "[18, 85]",
recEnd = "copy",
proportion = 1
)
spec <- mock_spec_from_recodeflow(variables, details)
validate_mock_spec(spec)
#> MockData mock_spec validation result: valid
variables_file <- tempfile(fileext = ".csv")
details_file <- tempfile(fileext = ".csv")
write.csv(variables, variables_file, row.names = FALSE)
write.csv(details, details_file, row.names = FALSE)
spec_from_files <- mock_spec_from_recodeflow(variables_file, details_file)