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

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

## ----eval = TRUE, echo = FALSE------------------------------------------------
library(CDMConnector)
if (Sys.getenv("EUNOMIA_DATA_FOLDER") == "") Sys.setenv("EUNOMIA_DATA_FOLDER" = tempdir())
if (!dir.exists(Sys.getenv("EUNOMIA_DATA_FOLDER"))) dir.create(Sys.getenv("EUNOMIA_DATA_FOLDER"))
if (!eunomia_is_available()) downloadEunomiaData()

## ----warning = FALSE, message = FALSE-----------------------------------------
library(CDMConnector)
library(CodelistGenerator)
library(PatientProfiles)
library(dplyr)
library(ggplot2)

con <- DBI::dbConnect(duckdb::duckdb(),
                       dbdir = CDMConnector::eunomia_dir())
cdm <- CDMConnector::cdm_from_con(con,
                                   cdm_schem = "main",
                                   write_schema = "main")

cdm <- generateConceptCohortSet(
  cdm = cdm,
  name = "ankle_sprain",
  conceptSet = list("ankle_sprain" = 81151),
  end = "event_end_date",
  limit = "all",
  overwrite = TRUE
)

cdm$ankle_sprain

cdm$ankle_sprain %>%
  addTableIntersectFlag(
    tableName = "condition_occurrence",
    window = c(-30, -1)
  ) |> 
  tally()

## -----------------------------------------------------------------------------
cdm$ankle_sprain |> 
  addTableIntersectFlag(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date", 
    targetStartDate = "drug_exposure_start_date",
    targetEndDate = "drug_exposure_end_date",
    window = c(-30, -1)
  ) |> 
  glimpse()

## -----------------------------------------------------------------------------
cdm$ankle_sprain |> 
  addTableIntersectFlag(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date",
    window = c(-30, -1)
  ) |> 
  glimpse()

## -----------------------------------------------------------------------------
cdm$ankle_sprain |> 
  addTableIntersectCount(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date", 
    window = c(-180, -1)
  ) |> 
  glimpse()

cdm$ankle_sprain |> 
  addTableIntersectDate(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date", 
    order = "last",
    window = c(-180, -1)
  ) |> 
  glimpse()


cdm$ankle_sprain |> 
  addTableIntersectDate(
    tableName = "drug_exposure",
    indexDate = "cohort_start_date", 
    order = "last",
    window = c(-180, -1)
  ) |> 
  glimpse()

## -----------------------------------------------------------------------------
acetaminophen_cs <- getDrugIngredientCodes(cdm = cdm, 
                                  name = c("acetaminophen"))

cdm$acetaminophen_records <- cdm$drug_exposure |> 
  filter(drug_concept_id %in% !!acetaminophen_cs[[1]]) |> 
  compute()

cdm$ankle_sprain |> 
  addTableIntersectFlag(
    tableName = "acetaminophen_records",
    indexDate = "cohort_start_date", 
    targetStartDate = "drug_exposure_start_date",
    targetEndDate = "drug_exposure_end_date",
    window = c(-Inf, Inf)
  ) |> 
  glimpse()

## -----------------------------------------------------------------------------
cdm$proc_or_meas <- union_all(cdm$procedure_occurrence |> 
  select("person_id", 
         "record_date" = "procedure_date"),
cdm$measurement |> 
  select("person_id", 
         "record_date" = "measurement_date")) |> 
  compute()

cdm$ankle_sprain |> 
  addTableIntersectFlag(
    tableName = "proc_or_meas",
    indexDate = "cohort_start_date", 
    targetStartDate = "record_date",
    targetEndDate = "record_date",
    window = c(0, 0)
  ) |> 
  glimpse()


