---
title: "Shiny diagnostics"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{ShinyDiagnostics}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true")

knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = NOT_CRAN
)
```

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

## Introduction: Run ShinyDiagnostics

In the previous vignettes we have seen how to run a phenotype diagnostics, create database and clinical descriptions, and cohort expectations. ShinyDiagnostics can help us to visualise all the results in an interactive shiny app. See an example of how to run it below:

```{r, message=FALSE, warning=FALSE, eval=FALSE}
library(CohortConstructor)
library(PhenotypeR)
library(dplyr)
library(readr)
library(here)
library(omock)

cdm <- mockCdmFromDataset(datasetName = "synpuf-1k_5.3", 
                          source = "duckdb")

# Create a code lists
codes <- list("user_of_warfarin" = c(1310149L, 40163554L),
              "user_of_acetaminophen" = c(1125315L, 1127078L, 1127433L, 40229134L, 
                                          40231925L, 40162522L, 19133768L),
              "user_of_morphine" = c(1110410L, 35605858L, 40169988L),
              "measurements_cohort" = c(40660437L, 2617206L, 4034850L,  2617239L, 
                                        4098179L))

# Instantiate cohorts with CohortConstructor
cdm$my_cohort <- conceptCohort(cdm = cdm,
                               conceptSet = codes, 
                               exit = "event_end_date",
                               overlap = "merge",
                               name = "my_cohort")

# Run PhenotypeDiagnostics including all diagnostics
result <- phenotypeDiagnostics(cdm$my_cohort)

# Generate expectations (see the vignette for more details)
chat <- chat("google_gemini")
expectations <- getCohortExpectations(chat = chat, 
                      phenotypes = result)

expectationsDir <- here()
write_csv(expectations, 
          file = here("expectations.csv"))

# Create database descriptions (see the vignette for more details)
databaseDescriptionDir <- here("database_descriptions")
downloadDatabaseDescriptionTemplate(directory = databaseDescriptionDir,
                                    name = "synpuf-1k")

# Create clinical descriptions (see the vignette for more details)
clinicalDescriptionDir <- here("cinical_descriptions")
downloadClinicalDescriptionTemplate(directory = clinicalDescriptionDir, name = "user_of_warfarin")
downloadClinicalDescriptionTemplate(directory = clinicalDescriptionDir, name = "user_of_acetaminophen")
downloadClinicalDescriptionTemplate(directory = clinicalDescriptionDir, name = "user_of_morphine")
downloadClinicalDescriptionTemplate(directory = clinicalDescriptionDir, name = "measurements_cohort")

# Create the shiny app based on PhenotypeDiagnostics results, suppressing all 
# cell counts smaller than 2, saved in a temporary directory, with the 
# expectations created using "gemini" and the database and clinical descriptions:
shinyDiagnostics(result = result, 
                 minCellCount = 2, 
                 directory = tempdir(), 
                 expectationsDir = expectationsDir,
                 databaseDescriptionsDir = databaseDescriptionDir,
                 clinicalDescriptionsDir = clinicalDescriptionDir)
```

## Shiny App Overview
Let's now explore the Shiny App created together! Once you've run the code above, you should obtain a shiny app similar to this one: [here](https://dpa-pde-oxford.shinyapps.io/PhenotypeRShiny/.

The first thing we will find when creating the PhenotypeR Shiny Diagnostics is a tab with a small summary of all the diagnostics:
```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure1.png"))
```

You can see which PhenotypeR version was used to generate the Shiny App by clicking the *i* tab at the top.

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure2.png"))
```

Or download the summarised result by clicking the *download* tab:

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure3.png"))
```

In the **Background** tab we will find the database and clinical descriptions for each database and cohort. 

Notice that we have a tab for each one of the diagnostics, and those contain the specific analyses performed. Results are visualised in the form of interactive tables and plots.

**Database Diagnostics:**

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure4.png"))
```
**Codelist Diagnostics:**

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure5.png"))
```

**Cohort Diagnostics:**

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure6.png"))
```

**Population Diagnostics:**

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure7.png"))
```

Let's now explore additional functionalities that the ShinyDiagnostics offers. If we click to *Codelist diagnostics / Achilles code use* or *Codelist diagnostics / Orphan code use* tab, we will first find a horizontal purple bar that will show us all the databases we included:

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure8.png"))
```

Once we have selected the ones of interest, we will need to click the **UPDATE** button to generate the table with the results. 

For **Codelist diagnostics / cohort code use**, **Codelist diagnostics / measurement diagnostics**, **Cohort diagnostics** and **Population diagnostics**, we will also have the option to select the cohorts of interest:

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure9.png"))
```

We will always find (in all the tabs) a *download* icon on the left which will download the table, gt table, or plot that is being shown:

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure10.png"))
```

In some tabs, we will also find a left tab that will show additional filtering or formatting options (remember to click **UPDATE** every time you change a parameter!):

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure11.png"))
```

When we have two (or more) subtabs with different formatting formats (which is the case for **Population diagnostics / Incidence**, where we have a table and a plot), the formatting tab will be on the right:

```{r echo=FALSE, out.width='90%'}
knitr::include_graphics(here::here("vignettes/ShinyDiagnosticsFigures/figure12.png"))
```

Now it's your turn to explore the [Shiny App](https://dpa-pde-oxford.shinyapps.io/Readme_PhenotypeR/)!

## Special cases
As mentioned, `ShinyDiagnostics()` can be run with specific diagnostic results. This includes `DatabaseDiagnostics()`, `CodelistDiagnostics()`, `CohortDiagnostics()`, and `PopulationDiagnostics()`. Alternatively, you can disable diagnostics within `PhenotypeDiagnostics()`. If a diagnostic is not performed, its corresponding tab will not appear in the Shiny App. Similarly, if survival analysis is skipped in `CohortDiagnostics()`, its tab will be removed. The same applies if your CDM lacks ACHILLES tables, which means "achilles code use" and "orphan code use" cannot be performed. In such cases, their tabs will also be automatically removed from the Shiny App.
