
Deterministically compile a validated spec_ir into a standalone R script
Source: R/compile-r.R
compile_r_script.RdCompiles any schema- and statistically-validated spec_ir into a clean,
readable, self-contained, and executable R simulation script using audited
code templates. Never evaluates untrusted text or language-model strings;
all identifiers and parameters pass assert_valid_spec_ir() before emission,
and every compiled script passes verify_compiled_r_ast() static AST
verification before returning.
Arguments
- spec
A validated
spec_irobject.- file
Optional file path; when provided, writes the compiled R script to
fileand returns the script text invisibly.- include_header
Logical; whether to include a descriptive header block.
- run_on_source
Logical; if
TRUE(default), appends a top-level execution block (results <- run_simulation(cfg)) so the script runs standalone when sourced or executed viaRscript.
Details
Generated scripts use reproducible L'Ecuyer-CMRG random number generation,
exact Cholesky draws for correlated predictors and correlated random effects,
calibrated logistic MAR missingness, and zero do.call() or eval() calls.
Examples
spec <- new_spec_ir(
design_type = "monte_carlo_lm",
hierarchy = list(levels = 1L, n_obs = 120L),
simulation = list(n_reps = 20L, seed = 42L)
)
code <- compile_r_script(spec)
verify_compiled_r_ast(code)
#> ✔ AST safety verification PASSED (6 expressions, 62 unique whitelisted calls).