Reproducibility, Validation & Environment Management
renv, double programming, unit tests and a qualified R environment.
Learning objectives
- 1Use renv to create a reproducible, pinned package environment for a study.
- 2Design a robust double-programming QC workflow with diffdf.
- 3Write automated unit tests for derivation functions using testthat.
- 4Understand what 'validated' or 'qualified' means for R in a regulated setting.
Key SAS® → R comparison
| Task | SAS® | R |
|---|---|---|
| Environment pinning | SAS 9.4 + hotfix level | renv.lock + renv::restore() |
| Dataset QC | PROC COMPARE + manual review | diffdf + structured reports + CI checks |
| Function testing | Ad-hoc checks in driver | testthat::test_that() with expectations |
| Qualification | IQ/OQ/PQ documentation | Similar rigor: package validation reports + locked env |
Code examples
/* No equivalent — everyone uses same SAS version */# At project start
renv::init()
# After adding packages
renv::snapshot()
# Months later on any machine
renv::restore() # exact same versions as original runNote: The renv.lock file becomes part of the study archive — reviewers can see exactly which versions of admiral, dplyr, etc. were used.
proc compare base=adam_prod compare=adam_qc;
id usubjid paramcd;
var aval chg;
run;# Production programmer
adsl <- build_adsl(...)
# Independent QC programmer (different person)
qc_adsl <- build_adsl_qc(...)
diffdf(adsl, qc_adsl,
keys = c("USUBJID"),
tolerance = 1e-6)Note: diffdf produces clear, actionable reports (missing rows, value differences, type mismatches) and integrates beautifully into automated validation pipelines.
/* Usually manual or %assert macro */test_that("derive_var_trtdurd handles missing dates correctly", {
input <- tibble(USUBJID = "001", TRTSDT = as.Date("2020-01-01"), TRTEDT = NA)
output <- derive_var_trtdurd(input)
expect_true(is.na(output$TRTDURD))
})Note: Small, focused tests for your most critical derivation functions give you fast feedback and serve as living documentation for the next programmer.
Explanation
The reproducibility story in R used to be the biggest objection from regulators and quality groups. renv largely closes that gap by giving you a one-file record of every package version used on a study — something SAS never provided natively.
Validation discipline translates directly. You still perform independent double programming for critical datasets. The tooling (diffdf + testthat) is simply more modern and produces better audit artifacts than the classic SAS approach.
Many sponsors now run formal qualification packages for the specific versions of R and key packages they intend to use in submissions — exactly analogous to the IQ/OQ/PQ work done for SAS. Once the environment is locked and qualified, the process is actually more reproducible than traditional SAS because the lockfile removes version ambiguity.