## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>"
)
library(reproducr)

# Shared setup: a script with a mix of risk levels
script <- tempfile(fileext = ".R")
writeLines(c(
  "set.seed(237)",
  "x   <- dplyr::filter(mtcars, cyl == 4)",
  "y   <- dplyr::summarise(x, mean_mpg = mean(mpg), n = dplyr::n())",
  "z   <- stats::rnorm(10)",
  "out <- base::sort(unique(x$gear))"
), script)

report <- audit_script(script, renv = FALSE, verbose = FALSE)
risks <- risk_score(report)

cert_file <- tempfile()
model <- lm(mpg ~ wt, data = mtcars)
certify(list(coefs = coef(model)), tag = "v1", file = cert_file)
drift <- check_drift(list(coefs = coef(model)),
  against = "v1",
  file = cert_file
)

## ----verdict-demo-------------------------------------------------------------
# No risks — REPRODUCIBLE
clean_script <- tempfile(fileext = ".R")
writeLines("x <- 1 + 1", clean_script)
clean_report <- audit_script(clean_script, renv = FALSE, verbose = FALSE)
clean_risks <- risk_score(clean_report)

cat(repro_report(clean_report, clean_risks, format = "text", style = "minimal"))

## ----minimal-text-------------------------------------------------------------
cat(repro_report(report, risks, format = "text", style = "minimal"))

## ----minimal-with-drift-------------------------------------------------------
cat(repro_report(report, risks,
  drift = drift,
  format = "text", style = "minimal"
))

## ----academic-----------------------------------------------------------------
cat(repro_report(report, risks, format = "text", style = "academic"))

## ----pharma-text--------------------------------------------------------------
cat(repro_report(report, risks,
  drift = drift,
  format = "text", style = "pharma"
))

## ----write-md, results = "hide"-----------------------------------------------
md_file <- tempfile(fileext = ".md")
repro_report(report, risks,
  format      = "md",
  style       = "minimal",
  output_file = md_file
)

# Inspect the raw Markdown
cat(readLines(md_file, warn = FALSE), sep = "\n")

## ----write-html, results = "hide"---------------------------------------------
html_file <- tempfile(fileext = ".html")
repro_report(report, risks,
  drift = drift,
  format = "html",
  style = "pharma",
  output_file = html_file
)

# The file is self-contained — open it in a browser
# browseURL(html_file)

## ----all-combinations, eval = FALSE-------------------------------------------
# styles <- c("minimal", "academic", "pharma")
# formats <- c("text", "md", "html")
# 
# for (sty in styles) {
#   for (fmt in formats) {
#     if (fmt == "text") {
#       repro_report(report, risks, format = fmt, style = sty)
#     } else {
#       out <- tempfile(fileext = paste0(".", fmt))
#       repro_report(report, risks,
#         format = fmt, style = sty,
#         output_file = out
#       )
#       message("Written: ", out)
#     }
#   }
# }

## ----badge-colours------------------------------------------------------------
# Reproducible — clean script, no risks
clean_badge <- repro_badge(clean_report, clean_risks, output = "markdown")
cat(clean_badge, "\n")

# Unknown — no risks supplied
unknown_badge <- repro_badge(report, output = "markdown")
cat(unknown_badge, "\n")

# With risks — colour depends on highest severity
risk_badge <- repro_badge(report, risks, output = "markdown")
cat(risk_badge, "\n")

## ----badge-readme-------------------------------------------------------------
readme <- tempfile(fileext = ".md")
writeLines(c(
  "# myanalysis",
  "",
  "Analysis of the relationship between engine size and fuel efficiency.",
  "",
  "## Installation"
), readme)

# Insert badge at the top
repro_badge(report, risks, output = "README", readme_path = readme)

# See the result
cat(readLines(readme, warn = FALSE), sep = "\n")

## ----template-path, eval = FALSE----------------------------------------------
# system.file("templates", "github_actions_audit.yml", package = "reproducr")

## ----cleanup, include = FALSE-------------------------------------------------
unlink(c(script, clean_script, md_file, html_file, readme))
unlink(paste0(cert_file, ".rds"))

