## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----eval = FALSE-------------------------------------------------------------
# install.packages("coreval")
# install.packages(c("xml2", "writexl"))  # optional: define.xml and Excel output

## ----setup--------------------------------------------------------------------
library(coreval)

## ----step2--------------------------------------------------------------------
ae <- data.frame(
  STUDYID = "DEMO", DOMAIN = "AE", USUBJID = c("S1", "S1", "S2"),
  AESEQ = c(1, 2, 1), AETERM = c("Headache", "Nausea", "Rash"),
  AESTDTC = c("2024-01-10", "2024-02-30", "2024-01-12"),
  AEENDTC = c("2024-01-12", "2024-02-01", "")
)

result <- check_dataset(ae, standard = "SDTMIG", version = "3.4")
result

## ----step3-build, include = FALSE---------------------------------------------
dir <- tempfile("coreval_demo_")
dir.create(dir)
dm <- data.frame(
  STUDYID = "DEMO", DOMAIN = "DM", USUBJID = c("S1", "S2", "S3"),
  RFSTDTC = c("2024-01-05", "2024-01-06", ""),
  AGE = c(34, 61, 47), AGEU = c("YEARS", "YEARS", ""),
  SEX = c("M", "F", "F")
)
haven::write_xpt(dm, file.path(dir, "dm.xpt"))
haven::write_xpt(ae, file.path(dir, "ae.xpt"))

## ----step3, eval = FALSE------------------------------------------------------
# result <- check_study("path/to/sdtm", standard = "SDTMIG", version = "3.4")

## ----step3-run, include = FALSE-----------------------------------------------
result <- check_study(dir, standard = "SDTMIG", version = "3.4")

## ----step4, eval = FALSE------------------------------------------------------
# write_findings(result, "issues.xlsx")

## ----tables-------------------------------------------------------------------
# what is wrong: dataset, row, subject, variable, value
f <- result$findings
head(f[!is.na(f$Record), c("Dataset", "Record", "USUBJID", "SEQ", "Variable", "Value")])

# what could not be checked, and why
head(unique(result$skipped$reason), 3)

## ----only-errors--------------------------------------------------------------
errors <- filter_findings(result, triage = "wrong value")
errors

## ----standard, eval = FALSE---------------------------------------------------
# check_study("path/to/sdtm", standard = "SDTMIG", version = "3.3")
# check_study("path/to/send", standard = "SENDIG", version = "3.1")

## ----ct, eval = FALSE---------------------------------------------------------
# list_ct_packages("sdtm")   # every release you can choose from
# check_study("path/to/sdtm", standard = "SDTMIG", ct_package = "sdtmct-2026-03-27")

## ----guard, eval = FALSE------------------------------------------------------
# r <- check_dataset(ae, standard = "SDTMIG", version = "3.4")
# bad <- filter_findings(r, triage = "wrong value")
# if (nrow(bad$findings) > 0) {
#   print(bad)
#   stop("AE has values that break CDISC rules")
# }

## ----rule---------------------------------------------------------------------
rule <- list_rules(id = "CORE-000718")
rule$issue        # what it checks
rule$legacy_ids   # the same rule's id in CDISC's Conformance Rules or FDA lists

## ----all, eval = FALSE--------------------------------------------------------
# print(result, n = Inf)   # every problem in the report
# result$findings          # every finding, as a table

## ----reuse, eval = FALSE------------------------------------------------------
# study <- read_study("path/to/sdtm")
# r1 <- check_study(study, standard = "SDTMIG", version = "3.4")

## ----usdm, eval = FALSE-------------------------------------------------------
# check_study("path/to/usdm", usdm = TRUE)

## ----cleanup, include = FALSE-------------------------------------------------
unlink(dir, recursive = TRUE)

