## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>"
)
library(reproducr)
cert_file <- tempfile() # shared across all chunks

## ----certify-examples---------------------------------------------------------
model <- lm(mpg ~ wt + cyl, data = mtcars)

certify(
  outputs = list(
    coefs       = coef(model),
    r_squared   = summary(model)$r.squared,
    sigma       = sigma(model),
    n_obs       = nrow(mtcars),
    n_complete  = sum(complete.cases(mtcars)),
    group_means = aggregate(mpg ~ cyl, data = mtcars, FUN = mean)
  ),
  tag = "baseline-v1",
  script = "analysis.R",
  file = cert_file
)

## ----tags---------------------------------------------------------------------
certify(
  outputs = list(coefs = coef(model)),
  tag     = "pre-peer-review",
  file    = cert_file
)

certify(
  outputs = list(coefs = coef(model)),
  tag     = "post-revision",
  file    = cert_file
)

## ----duplicate-tag, warning = TRUE--------------------------------------------
certify(
  outputs = list(coefs = coef(model)),
  tag     = "baseline-v1",
  file    = cert_file
)

## ----list-certs---------------------------------------------------------------
list_certs(file = cert_file)

## ----drift-basic--------------------------------------------------------------
model2 <- lm(mpg ~ wt + cyl, data = mtcars)

result <- check_drift(
  outputs = list(
    coefs       = coef(model2),
    r_squared   = summary(model2)$r.squared,
    sigma       = sigma(model2),
    n_obs       = nrow(mtcars),
    n_complete  = sum(complete.cases(mtcars)),
    group_means = aggregate(mpg ~ cyl, data = mtcars, FUN = mean)
  ),
  against = "baseline-v1",
  file = cert_file
)

## ----statuses-----------------------------------------------------------------
certify(
  outputs = list(
    stays_same  = 42L,
    will_change = coef(lm(mpg ~ wt, data = mtcars)),
    will_vanish = "this output disappears next run"
  ),
  tag = "four-statuses",
  file = cert_file
)

demo_result <- check_drift(
  outputs = list(
    stays_same  = 42L,
    will_change = coef(lm(mpg ~ hp, data = mtcars)),
    brand_new   = "this output is new"
  ),
  against = "four-statuses",
  file = cert_file
)

print(demo_result)

## ----latest-------------------------------------------------------------------
certify(outputs = list(x = 1L), tag = "run-1", file = cert_file)
certify(outputs = list(x = 1L), tag = "run-2", file = cert_file)
certify(outputs = list(x = 1L), tag = "run-3", file = cert_file)

check_drift(outputs = list(x = 1L), against = "latest", file = cert_file)

## ----drift-programmatic, eval = FALSE-----------------------------------------
# result <- check_drift(outputs = current_outputs, against = "latest")
# 
# n_drifted <- sum(result$status == "drifted")
# if (n_drifted > 0L) {
#   drifted_names <- result$output[result$status == "drifted"]
#   stop(sprintf(
#     "%d output(s) have drifted since last certification: %s",
#     n_drifted,
#     paste(drifted_names, collapse = ", ")
#   ))
# }

## ----cleanup, include = FALSE-------------------------------------------------
unlink(paste0(cert_file, ".rds"))

