Skip to contents

Adds every variable defined in the var_spec for dataset that is absent from x as a correctly typed NA column. Existing columns are unchanged. Equivalent to the scaffold step inside apply_spec(), exposed for standalone use.

Usage

scaffold_vars(x, spec, dataset)

Arguments

x

A data frame.

spec

A herald_spec object or a path to a spec file.

dataset

Character. The dataset name.

Value

The data frame with missing columns added (invisibly).

Examples

spec <- herald_spec(
  ds_spec = data.frame(dataset = "DM", label = "Demographics",
                       stringsAsFactors = FALSE),
  var_spec = data.frame(
    dataset   = "DM",
    variable  = c("STUDYID", "AGE"),
    data_type = c("text", "integer"),
    stringsAsFactors = FALSE
  )
)
dm <- data.frame(STUDYID = "S1", stringsAsFactors = FALSE)
result <- scaffold_vars(dm, spec, "DM")
#> Scaffolded 1 variable: `AGE`
names(result)   # "STUDYID" "AGE"
#> [1] "STUDYID" "AGE"    
is.na(result$AGE)  # TRUE
#> [1] TRUE