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

## ----eval = requireNamespace("duckdb", quietly = TRUE)------------------------
library(rwevalidate)

con <- example_cdm()

out <- validate_cohort(
  cdm_schema   = "main",
  cohort_table = "cohort",
  cohort_id    = 1,
  con          = con,
  vocab_schema = "main",
  output_dir   = tempfile("rwe_demo_"),
  render_html  = FALSE
)

out$report$check_summary
cdm_disconnect(con)

## -----------------------------------------------------------------------------
# library(rwevalidate)
# 
# con <- cdm_connect(
#   host       = "localhost",
#   port       = 5432,
#   dbname     = "your_db",
#   user       = "your_user",
#   password   = "your_password",
#   cdm_schema = "mimic_cdm"
# )

## -----------------------------------------------------------------------------
# DBI::dbExecute(con, "
#   CREATE TABLE results.my_cohort AS
#   SELECT person_id AS subject_id, 1 AS cohort_definition_id,
#          MIN(condition_start_date) AS cohort_start_date,
#          MAX(condition_end_date)   AS cohort_end_date
#   FROM mimic_cdm.condition_occurrence
#   WHERE condition_concept_id IN (
#     SELECT descendant_concept_id FROM vocab.concept_ancestor
#     WHERE ancestor_concept_id = 316139)
#   GROUP BY person_id")

## -----------------------------------------------------------------------------
# results <- validate_cohort(
#   cdm_schema    = "mimic_cdm",
#   cohort_table  = "results.my_cohort",
#   cohort_id     = 1,
#   concept_ids   = 316139,   # enables Module 1 (concept coverage)
#   comparator_id = 2,        # enables Module 4 (covariate feasibility)
#   vocab_schema  = "vocab",
#   output_dir    = "./validation_report",
#   con           = con
# )

## -----------------------------------------------------------------------------
# results$report$check_summary   # one row per check: pass / warn / fail
# results$flags                  # all WARN:/FAIL: messages

## -----------------------------------------------------------------------------
# run_attrition(con, "mimic_cdm", "results.my_cohort", cohort_id = 1,
#               vocab_schema = "vocab")
# 
# run_density(con, "mimic_cdm", "results.my_cohort", cohort_id = 1)
# 
# run_concepts(con, "mimic_cdm", "results.my_cohort", cohort_id = 1,
#              concept_ids = 316139, vocab_schema = "vocab")
# 
# run_covariates(con, "mimic_cdm", "results.my_cohort", cohort_id = 1,
#                comparator_id = 2, vocab_schema = "vocab")

## -----------------------------------------------------------------------------
# cdm_disconnect(con)

