Skip to contents

Compiles 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.

Usage

compile_r_script(
  spec,
  file = NULL,
  include_header = TRUE,
  run_on_source = TRUE
)

Arguments

spec

A validated spec_ir object.

file

Optional file path; when provided, writes the compiled R script to file and 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 via Rscript.

Value

A character scalar containing the deterministic R script (invisibly when file is provided).

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).