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

isMissingOrEmpty <- function(x) {
  length(x) == 0 || is.na(x[1]) || !nzchar(x[1])
}

readSummaryParquet <- function(path) {
  as.data.frame(nanoparquet::read_parquet(path), stringsAsFactors = FALSE)
}

exampleRoot <- system.file("example", "st", package = "CohortContrast")
if (isMissingOrEmpty(exampleRoot) && dir.exists("inst/example/st")) {
  exampleRoot <- normalizePath("inst/example/st")
}
studyPath <- file.path(exampleRoot, "lc500s")

if (isMissingOrEmpty(exampleRoot) || !dir.exists(studyPath)) {
  cat("Bundled summary example 'lc500s' is not available in this build.\n")
  knitr::knit_exit()
}

metadata <- jsonlite::fromJSON(file.path(studyPath, "metadata.json"), simplifyVector = FALSE)

conceptSummaries <- readSummaryParquet(file.path(studyPath, "concept_summaries.parquet"))
ordinalSummaries <- readSummaryParquet(file.path(studyPath, "ordinal_summaries.parquet"))
mappingTable <- readSummaryParquet(file.path(studyPath, "complementaryMappingTable.parquet"))

k2Summary <- readSummaryParquet(file.path(studyPath, "clustering_k2_summary.parquet"))
k3Summary <- readSummaryParquet(file.path(studyPath, "clustering_k3_summary.parquet"))
k4Summary <- readSummaryParquet(file.path(studyPath, "clustering_k4_summary.parquet"))
k5Summary <- readSummaryParquet(file.path(studyPath, "clustering_k5_summary.parquet"))

k2Overlap <- readSummaryParquet(file.path(studyPath, "clustering_k2_pairwise_overlap.parquet"))
k3Overlap <- readSummaryParquet(file.path(studyPath, "clustering_k3_pairwise_overlap.parquet"))
k4Overlap <- readSummaryParquet(file.path(studyPath, "clustering_k4_pairwise_overlap.parquet"))
k5Overlap <- readSummaryParquet(file.path(studyPath, "clustering_k5_pairwise_overlap.parquet"))

## -----------------------------------------------------------------------------
str(metadata, max.level = 2)

## -----------------------------------------------------------------------------
utils::head(conceptSummaries, 10)

## -----------------------------------------------------------------------------
utils::head(ordinalSummaries, 10)

## -----------------------------------------------------------------------------
utils::head(k2Summary, 10)

## -----------------------------------------------------------------------------
utils::head(k3Summary, 10)

## -----------------------------------------------------------------------------
utils::head(k4Summary, 10)

## -----------------------------------------------------------------------------
utils::head(k5Summary, 10)

## -----------------------------------------------------------------------------
utils::head(k2Overlap, 10)

## -----------------------------------------------------------------------------
utils::head(k3Overlap, 10)

## -----------------------------------------------------------------------------
utils::head(k4Overlap, 10)

## -----------------------------------------------------------------------------
utils::head(k5Overlap, 10)

## -----------------------------------------------------------------------------
utils::head(mappingTable, 10)

