---
title: "Concept sets"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{concept_set}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r setup}
library(omopgenerics)
```


## Codelist
A concept set can be represented as either a codelist or a concept set expression. A codelist is a named list, with each item of the list containing specific concept IDs. 
```{r}
condition_codes <- list(
  "diabetes" = c(201820, 4087682, 3655269),
  "asthma" = 317009
)
condition_codes <- newCodelist(condition_codes)

condition_codes
```

A codelist must be named
```{r}
condition_codes <- list(c(201820, 4087682, 3655269))
newCodelist(condition_codes)
```

And a codelist cannot have missing values
```{r}
condition_codes <- list(
  "diabetes" = c(201820, NA, 3655269),
  "asthma" = 317009
)
newCodelist(condition_codes)
```

## Concept set expression
A concept set expression provides a high-level definition of concepts that, when applied to a specific OMOP CDM vocabulary version (by making use of the concept hierarchies and relationships), will result in a codelist. 

```{r}
condition_cs <- list(
  "diabetes" = dplyr::tibble(
    "concept_id" = c(201820, 4087682),
    "excluded" = c(FALSE, FALSE),
    "descendants" = c(TRUE, FALSE),
    "mapped" = c(FALSE, FALSE)
  ),
  "asthma" = dplyr::tibble(
    "concept_id" = 317009,
    "excluded" = FALSE,
    "descendants" = FALSE,
    "mapped" = FALSE
  )
)
condition_cs <- newConceptSetExpression(condition_cs)

condition_cs
```

As with a codelist, a concept set expression must be a named list and cannot have missing elements.
```{r}
condition_cs <- list(
  dplyr::tibble(
    "concept_id" = c(201820, NA),
    "excluded" = c(FALSE, FALSE),
    "descendants" = c(TRUE, FALSE),
    "mapped" = c(FALSE, FALSE)
  )
)
newConceptSetExpression(condition_cs)
```

```{r}
condition_cs <- list(
  "diabetes" = dplyr::tibble(
    "concept_id" = c(201820, NA),
    "excluded" = c(FALSE, FALSE),
    "descendants" = c(TRUE, FALSE),
    "mapped" = c(FALSE, FALSE)
  ),
  "asthma" = dplyr::tibble(
    "concept_id" = 317009,
    "excluded" = FALSE,
    "descendants" = FALSE,
    "mapped" = FALSE
  )
)
newConceptSetExpression(condition_cs)
```

