---
title: "Code lists for medications"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{a04_codelists_for_medications}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r,  message=FALSE, warning=FALSE,echo=FALSE}
library(DBI)
library(dplyr)
library(CodelistGenerator)
```

## Creating a codelists for acetaminophen
For this vignette we are going to create codelists for identifying a acetaminophen, looking in the drug domain.

## Connecting to a database with the OMOP CDM vocabularies
```{r, eval=FALSE}
# postgres database connection details
serverDbi <- Sys.getenv("server")
user <- Sys.getenv("user")
password <- Sys.getenv("password")
port <- Sys.getenv("port")
host <- Sys.getenv("host")

db <- DBI::dbConnect(RPostgres::Postgres(),
  dbname = serverDbi,
  port = port,
  host = host,
  user = user,
  password = password
)

# name of vocabulary schema
vocabularyDatabaseSchema <- "vocabulary"
```

## Codelist from "acetaminophen" and its descendants
First we can follow the approach of identifying high-level codes and include all their descendants.

```{r, eval=FALSE}
library(dplyr)
library(CodelistGenerator)
```

```{r, eval=FALSE}
acetaminophen1 <- getCandidateCodes(
  cdm = cdm,
  keywords = "acetaminophen",
  domains = "drug",
  standardConcept = "standard",
  includeDescendants = TRUE
)
```

```{r,  message=FALSE, warning=FALSE, echo=FALSE}
acetaminophen1 <- readRDS(system.file("medData01.RData", 
                                            package = "CodelistGenerator"))
```

```{r,  message=FALSE, warning=FALSE }  
acetaminophen1 %>% dplyr::glimpse()
```

