
Generate tidyverse Code for Automatic Discrepancy Cleaning and Crosswalks
Source:R/codegen.R
sv_auto_clean_rules.RdInspects discrepancy detection output or pairwise column discrepancies (exact matches,
fuzzy near-matches, and missing levels), automatically synthesizing clean recoding rules
using dplyr::case_when(). Handles casing differences, leading/trailing whitespace
discrepancies, and fuzzy string distance variations (Levenshtein distance <= 2).
Usage
sv_auto_clean_rules(
discrepancy_result,
df_name = "df",
col = "status",
default = col,
pipe = FALSE,
lazy = FALSE,
df_is_code = FALSE
)
sv_code_auto_clean(
discrepancy_result,
df_name = "df",
col = "status",
default = col,
pipe = FALSE,
lazy = FALSE,
df_is_code = FALSE
)Arguments
- discrepancy_result
A discrepancy result list (such as returned by
sv_detect_discrepancies()) or an object containingfuzzy_matches,only_in_x,only_in_y, andexact_matches.- df_name
Name of the data frame to mutate. Defaults to
"df". Can beNULLfor piped or standalone expressions.- col
Column name being recoded. Defaults to
"status". IfNULL, attempts to extract fromdiscrepancy_result$from_color defaults to"status".- default
Fallback value for unmatched records. If equal to
col, preserves unmatched records via.default = <col>. Set toNULLto omit the default clause.- pipe
Logical; if TRUE, emits piped syntax
df %>% dplyr::mutate(...).- lazy
Logical; TRUE when
df_nameis an arrow or duckdb table. The code then ends indplyr::collect()(the one collect of a lazy pipeline), the recode closes withTRUE ~ <col>(arrow cannot take.default = <col>), anddf_namemay be an expression such asdplyr::tbl(con, "visits")whendf_is_codeis TRUE. The matching of values is done in R on the values compared, so the generated code uses notrimws()ortolower().- df_is_code
Logical; TRUE when
df_nameis R code (such asdplyr::tbl(con, "visits")) to use as written. FALSE (the default) treats it as a table name and backtick-quotes it when it is not syntactic, as invisits (2020). Only used whenlazyis TRUE.
Examples
disc <- sv_detect_discrepancies(c("male", "Female"), c("Male", "Female"))
sv_auto_clean_rules(disc, df_name = "patients", col = "gender")
#> [1] "dplyr::mutate(patients, gender = dplyr::case_when(\n gender == \"male\" ~ \"Male\",\n .default = gender\n ))"