## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7,
                      fig.height = 4.5)
library(textileFlame)

## ----import-------------------------------------------------------------------
raw <- read.csv(system.file("extdata", "flame_example.csv",
                            package = "textileFlame"))
head(raw[, c("specimen", "treatment", "batch", "direction", "laundering",
             "afterflame", "char_length", "char_length_status")])

## ----flame-data---------------------------------------------------------------
fd <- flame_data(raw, control = "Untreated",
                 method_edition = "synthetic example")
fd

## ----status-------------------------------------------------------------------
table(fd$treatment, fd$char_length_status)

## ----summary------------------------------------------------------------------
s <- summarize_flame(fd, endpoint = "char_length")
s[s$direction == "warp" & s$laundering == "0",
  c("treatment", "n_specimens", "n_batches", "n_censored", "mean", "bound")]

## ----compare------------------------------------------------------------------
cmp <- compare_treatments(fd, endpoint = "afterflame")
cmp[, c("treatment", "direction", "laundering", "n_treated", "n_control",
        "difference", "conf_low", "pct_reduction", "status")]

## ----compare-specimen---------------------------------------------------------
cmp_spec <- suppressWarnings(
  compare_treatments(fd, endpoint = "afterflame", unit = "specimen")
)
cmp_spec[cmp_spec$direction == "warp" & cmp_spec$laundering == "0",
         c("treatment", "difference", "conf_low", "conf_high",
           "pct_reduction", "pct_low", "pct_high")]

## ----compare-censored---------------------------------------------------------
cmp_char <- compare_treatments(fd, endpoint = "char_length")
head(cmp_char[, c("treatment", "direction", "laundering", "status")], 4)

## ----compare-bound------------------------------------------------------------
cmp_bound <- compare_treatments(fd, endpoint = "char_length",
                                censored = "bound")
cmp_bound[1:4, c("treatment", "direction", "laundering", "difference",
                 "diff_bound", "pct_reduction", "pct_bound", "status")]

## ----criteria-----------------------------------------------------------------
crit <- flame_criteria(
  data.frame(endpoint = c("afterflame", "char_length"),
             statistic = c("mean", "individual"),
             max = c(5, 200)),
  source = "Illustrative values for this vignette only"
)
ev <- evaluate_criteria(fd, crit)
ev[ev$direction == "warp" & ev$laundering == "0",
   c("treatment", "endpoint", "statistic", "limit", "observed", "result")]

## ----plot-specimens-----------------------------------------------------------
plot_specimens(fd, endpoint = "char_length")

## ----plot-comparison----------------------------------------------------------
plot_comparison(cmp)

## ----report-------------------------------------------------------------------
txt <- flame_report(fd, comparisons = cmp, criteria = crit,
                    date = as.Date("2026-01-01"))
head(txt, 10)

