---
title: "Tutorial: drug_regimen_sunburst"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Tutorial: drug_regimen_sunburst}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

# exit if user doesn't have synapser, a log in, or access to data.
if (genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT")) == FALSE){
  knitr::knit_exit()
}
```

```{r, include=FALSE, eval=genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT"))}
library(genieBPC)
library(dplyr)
library(ggplot2)
library(plotly)
library(gt)
library(magrittr)
library(tibble)
```

```{r, include = FALSE, eval=genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT"))}
gt_compact_fun <- function(x) {
  gt::tab_options(x,
                  table.font.size = 'small',
                  data_row.padding = gt::px(1),
                  summary_row.padding = gt::px(1),
                  grand_summary_row.padding = gt::px(1),
                  footnotes.padding = gt::px(1),
                  source_notes.padding = gt::px(1),
                  row_group.padding = gt::px(1))
}
```


## Introduction

The `drug_regimen_sunburst()` function allows the user to visualize the complete treatment course for selected diagnoses in a given analytic cohort. The function returns a list containing a 'treatment_history' data frame and the interactive plot 'sunburst_plot'.


## Modifying Function Arguments

```{r, include = FALSE, eval=genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT"))}
tbl_arguments <- tibble::tribble(
  ~Argument,       ~Description,
  "`data_synapse`",       "The item from the nested list returned from pull_data_synapse() corresponding to the cancer cohort of interest.",
  "`data_cohort`",       "The list returned from the `create_analytic_cohort()` function call.",
  "`max_n_regimens`",        "The number of lines of treatment displayed in the sunburst plot.") %>%
  gt::gt() %>%
  gt::tab_header(title = "`drug_regimen_sunburst()` Function Arguments") %>%
  gt::fmt_markdown(columns = c(Argument)) %>%
  gt::tab_options(table.font.size = 'small',
                  data_row.padding = gt::px(1),
                  summary_row.padding = gt::px(1),
                  grand_summary_row.padding = gt::px(1),
                  footnotes.padding = gt::px(1),
                  source_notes.padding = gt::px(1),
                  row_group.padding = gt::px(1)) %>%
  gt_compact_fun()

tbl_arguments
```

```{r, echo = FALSE, eval=genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT"))}
tbl_arguments
```

Note that the innermost circle of the sunburst plot indicates the first therapies that patients received following diagnosis. The circle is divided proportionally based on the percentage of patients that received a particular treatment. Each subsequent ring of the sunburst corresponds to a subsequent treatment regimen, such that the inner rings represent earlier treatments. Together, the rings illustrate the distribution and patterns of various treatment regimens.

## Setup

Before going through the tutorial, load the {genieBPC} library and log into Synapse using the `set_synapse_credentials()` function. For more information on `set_synapse_credentials()`, refer to the `Tutorial: pull_data_synapse()` vignette.

```{r, results = 'hide', eval=genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT"))}
library(genieBPC)

set_synapse_credentials()
```

This tutorial will utilize the data downloaded in the `Tutorial: pull_data_synapse()` vignette, as shown below:



```{r, results = 'hide', eval=genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT"))}
nsclc_2_0 <- pull_data_synapse("NSCLC", version = "v2.0-public")
```


## Example

The example below creates a sunburst plot of the first three cancer-directed regimens received by patients diagnosed with Stage IV NSCLC. 


```{r, results = 'hide', message = FALSE, eval=genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT"))}
nsclc_stg_iv <- create_analytic_cohort(data_synapse = nsclc_2_0$NSCLC_v2.0,
                                      stage = c("Stage IV"))

nsclc_treatment <- drug_regimen_sunburst(data_synapse = nsclc_2_0$NSCLC_v2.0,
                      data_cohort = nsclc_stg_iv,
                      max_n_regimens = 3)
```

```{r, eval=genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT"))}
head(nsclc_treatment$treatment_history)
```

```{r, fig.height=8, fig.width=8, eval=genieBPC:::.is_connected_to_genie(pat = Sys.getenv("SYNAPSE_PAT"))}
nsclc_treatment$sunburst_plot
```

