Generate a self-contained, reproducible R script from a specification
Source:R/spec_builder.R
generate_r_script.RdEmbed the specification as an R list literal, so that the returned script reproduces the design without any external file. This turns a design built in the no-code application into a reproducible script; the application's Verify button runs that script in a clean R session and confirms that it reproduces the data bit-for-bit.
Arguments
- spec
A design specification (list), as produced by
build_spec().
Value
A length-one character string containing a runnable R script that loads pilotr,
embeds the specification, and simulates the data.
Details
Numbers are emitted at 17 significant digits rather than through deparse(), which prints
15 and so does not round-trip: deparse(1/3) reads back as a different double. Since the
point of the script is bit-for-bit reproduction, the embedded specification has to preserve
every coefficient exactly.
Examples
spec <- build_spec(list(name = "demo", seed = 1, design_kind = "between",
factor_name = "group", lev1 = "a", lev2 = "b", n_subject = 20,
intercept = 0, effect = 0.5, family = "gaussian", resp_name = "", sigma = 1))
cat(generate_r_script(spec))
#> # Reproducible simulation exported by pilotr.
#> # install.packages("pilotr") # once available; then run this script as-is.
#> library(pilotr)
#>
#> spec <- list(spec_version = "0.3", name = "demo", seed = 1L, units = list(subject = list(n = 20L)), factors = list(list(name = "group", levels = c("a", "b"), contrasts = list(effect = c(-0.5, 0.5)), between = "subject")), fixed = list(intercept = 0, coefficients = list(effect = 0.5)), response = list(family = "gaussian", name = "score", sigma = 1, round = 4L))
#>
#> data <- simulate_design(spec) # analysis-ready data frame
#> # write.csv(data, "data.csv", row.names = FALSE)
#> # pow <- power_mixed(spec, n_sims = 200) # simulation-based power + Type S/M