---
title: "Results schema of the CohortMethod package"
author: "Martijn J. Schuemie"
date: "`r Sys.Date()`"
output:
  html_document:
    number_sections: yes
    toc: yes
  pdf_document:
    number_sections: yes
    toc: yes
vignette: >
  %\VignetteIndexEntry{Results schema}
  %\VignetteEncoding{UTF-8}      
  %\VignetteEngine{knitr::rmarkdown}
---


```{r setup, include=FALSE}
library(dplyr)
library(knitr)
```

# Introduction

This document describes the data model of the output of the CohortMethod package, generated by the `exportToCsv()` function. This vignette assumes you are already familiar with the `CohortMethod` package, and have read all other vignettes.

## Fields with minimum values

Some fields contain patient counts or fractions that can easily be converted to patient counts.
To prevent identifiability, these fields are subject to a minimum value.
When the value falls below this minimum, it is replaced with the negative value of the minimum.
For example, if the minimum subject count is 5, and the actual count is 2, the value stored in the data model will be -5, which could be represented as '\<5' to the user.
Note that the value 0 is permissible, as it identifies no persons.
These fields are identified below as having Min. count = 'Yes'.

# Tables

In this section you will find the list of tables and their fields. 

```{r echo=FALSE, results="asis", warning=FALSE, message=FALSE}
specifications <- readr::read_csv(system.file(
  "csv",
  "resultsDataModelSpecification.csv",
  package = "CohortMethod"
)) |>
  SqlRender::snakeCaseToCamelCaseNames()
tables <- split(specifications, specifications$tableName)

table <- tables[[1]]
for (table in tables) {
  header <- sprintf("## Table %s", table$tableName[1])

  table <- table |>
    select(Field = "columnName", Type = "dataType", Key = "primaryKey", "Min. count" = "minCellCount", Deprecated = deprecated, Description = "description") |>
    kable(format = "simple", linesep = "", booktabs = TRUE, longtable = TRUE)

  writeLines("")
  writeLines(header)
  writeLines(table)
}
```
