Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ CLAUDE.md
# macOS
.DS_Store

# Generated documentation
man/*.Rd
# Generated documentation (man/*.Rd is committed so GitHub installs have help pages)
docs/

# Quarto/HTML output
Expand Down
14 changes: 10 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
`variable_details = NULL`, detail-level-only `databaseStart`, or another
unsupported variable) on metadata that sets `anchor`, `create_mock_data()`
stops and names the survival variables rather than dropping them.
- The same applies to formula variables. If the legacy generator is
selected for metadata that sets `mockFormula` on an enabled variable,
`create_mock_data()` stops and names the formula variables. Previously the
legacy generator ignored the formula and returned unrelated random values,
or dropped a `DerivedVar::` variable, without saying so.

## New features

Expand Down Expand Up @@ -109,10 +114,11 @@
an integer range now fails with a message naming the variable. (#58)
- The packaged minimal example corrects two metadata errors: BMI's low
garbage range had a stray parenthesis (`[-10;15])`), and height's
low-garbage proportion was 1.00 (every row) instead of 0.01. With these
and #58, every variable in the example except its survival dates now
generates through the v0.4 pipeline. Seeded output for the example
changes.
low-garbage proportion was 1.00 (every row) instead of 0.01; height's
high-garbage range had an infinite upper bound, `(2.1;inf]`, which
produced `NaN` values, and is now `(2.1;2.5]` (#60). With these, #58 and
the survival dates (#40), every variable in the example generates through
the v0.4 pipeline. Seeded output for the example changes.

## Deprecations

Expand Down
43 changes: 39 additions & 4 deletions R/create_mock_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@
names(spec$variables)[unsupported]
}

#' @noRd
.legacy_route_reasons <- function() {
paste0(
"It is used when validate = FALSE, when variable_details is NULL, when ",
"only variable_details has a databaseStart column, or when another ",
"variable uses a feature the v0.4 pipeline does not support. Run with ",
"verbose = TRUE to see which."
)
}

#' @noRd
.legacy_formula_variables <- function(enabled_vars, variable_details, databaseStart) {
# Enabled variables with a non-blank mockFormula for the requested
# database, using the same lookup as the v0.4 adapter's carve-out.
if (is.null(variable_details) || !"mockFormula" %in% names(variable_details)) {
return(character(0))
}
database <- if ("databaseStart" %in% names(variable_details)) databaseStart else NULL
Filter(function(variable) {
details <- .filter_recodeflow_details(variable_details, variable, database)
!.is_blank(suppressWarnings(.details_mock_formula(details)))
}, as.character(enabled_vars$variable))
}

#' @noRd
.has_survival_metadata <- function(variables) {
# A row sets anchor, or carries survival parameters (followup_min,
Expand Down Expand Up @@ -371,6 +395,20 @@ create_mock_data <- function(databaseStart,
enabled_vars <- variables
}

# Formula variables are computed only by the v0.4 pipeline; the legacy
# dispatcher ignores mockFormula and would return unrelated random values,
# or drop a DerivedVar:: variable, without saying so. Checked before the
# derived exclusion below, which would otherwise hide DerivedVar:: rows.
formula_vars <- .legacy_formula_variables(enabled_vars, variable_details, databaseStart)
if (length(formula_vars) > 0) {
stop(
"Formula variable(s) ", paste(formula_vars, collapse = ", "),
" (mockFormula set) are computed only by the v0.4 pipeline, but the ",
"legacy generator was selected. ", .legacy_route_reasons(),
call. = FALSE
)
}

# Exclude derived variables (identified by DerivedVar:: and Func:: patterns)
if (!is.null(variable_details)) {
derived_vars <- identify_derived_vars(enabled_vars, variable_details)
Expand Down Expand Up @@ -398,10 +436,7 @@ create_mock_data <- function(databaseStart,
stop(
"Survival date variable(s) ", paste(anchored, collapse = ", "),
" (anchor set) are generated only by the v0.4 pipeline, but the ",
"legacy generator was selected. It is used when validate = FALSE, ",
"when variable_details is NULL, when only variable_details has a ",
"databaseStart column, or when another variable uses a feature the ",
"v0.4 pipeline does not support. Run with verbose = TRUE to see which.",
"legacy generator was selected. ", .legacy_route_reasons(),
call. = FALSE
)
}
Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,18 @@ mock_data <- create_mock_data(
seed = 123
)

head(mock_data)
#> age smoking interview_date
#> 1 42 2 2001-08-05
#> 2 47 2 2002-11-27
#> 3 73 3 2002-11-14
#> 4 51 1 2005-01-14
#> 5 52 1 2001-02-25
#> 6 76 3 2004-07-25
head(mock_data[, c("age", "smoking", "BMI", "interview_date", "death_date")])
#> age smoking BMI interview_date death_date
#> 1 35 3 996.00000 2004-10-20 <NA>
#> 2 999 1 31.52719 2004-01-11 <NA>
#> 3 72 1 21.02576 2002-01-21 <NA>
#> 4 997 2 28.36755 2004-06-25 <NA>
#> 5 55 1 29.52888 2001-02-22 <NA>
#> 6 998 3 25.80269 2004-01-12 2005-01-11
```

The minimal example generates all of its variables; this shows five of them. Values such as 996 and 999 are missing codes declared by the metadata's `NA::` rows.

**What's in those CSV files?** See [inst/extdata/minimal-example/](inst/extdata/minimal-example/README.md) - just variable names, types, ranges, and optional mock-data parameters.

**When should you use metadata files?**
Expand Down
2 changes: 1 addition & 1 deletion inst/extdata/minimal-example/variables.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cchsflow_v0001","age","Age in years","Continuous","integer","enabled,predictor,table1",10,10,,"[;]",,"[;]","normal",50,15,,,,,,"","","","1.0.0","2025-11-09","Normal distribution (mean=50, sd=15)"
"cchsflow_v0002","smoking","Smoking status","Categorical","factor","enabled,predictor,table1",20,20,,"",,"","",,,,,,,,"","","","1.0.0","2025-11-09","Categorical variable with proportions"
"cchsflow_v0003","BMI","Body mass index","Continuous","double","enabled,outcome,table1",30,30,0.02,"[-10;15]",0.01,"[60;150]","normal",27.5,5.2,,,,,,"","","","1.0.0","2025-11-09","Normal distribution with two-sided contamination"
"cchsflow_v0004","height","Height in meters","Continuous","double","enabled,predictor",40,40,0.01,"[0;1.4)",0.01,"(2.1;inf]","normal",1.7,0.1,,,,,,"","","","1.0.0","2025-11-13","Height for BMI calculation"
"cchsflow_v0004","height","Height in meters","Continuous","double","enabled,predictor",40,40,0.01,"[0;1.4)",0.01,"(2.1;2.5]","normal",1.7,0.1,,,,,,"","","","1.0.0","2025-11-13","Height for BMI calculation"
"cchsflow_v0005","weight","Weight in kilograms","Continuous","double","enabled,predictor",50,50,,"[;]",,"[;]","normal",75,15,,,,,,"","","","1.0.0","2025-11-13","Weight for BMI calculation"
"cchsflow_v0006","BMI_derived","BMI calculated from height and weight","Continuous","double","enabled,outcome,table1",60,60,,"[;]",,"[;]","",,,,,,,,"","","","1.0.0","2025-11-13","Derived variable: BMI = weight / (height^2)"
"ices_v01","interview_date","Interview date (cohort entry)","Continuous","date","enabled,outcome",70,70,0,"[;]",0,"[;]","uniform",,,,,,,,"","","analysis","1.0.0","2025-11-09","Uniform date range (cohort entry)"
Expand Down
34 changes: 34 additions & 0 deletions man/MockData-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions man/add_garbage.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions man/apply_garbage.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading