```{r}
#| label: ci_extract
#| results: 'asis'
#| include: false

  # ===================================
  # Get CI
  # ===================================
  cohortIncidence <- OhdsiReportGenerator::getIncidenceRates(
    connectionHandler = connectionHandler, 
    schema = params$schema, 
    targetIds = unique(c(targetIdsOfInterest, comparatorIdsOfInterest, indicationIdsOfInterest)), 
    outcomeIds = outcomeIdsOfInterest
  ) %>%
  dplyr::filter(
    .data$ageGroupName == 'Any',
    .data$genderName == 'Any',
    .data$startYear == 'Any'
  )

# add the friendly names to the results

tempDf <- cohortDefinitions[,c('friendlyName','cohortDefinitionId',"subsetText")]
colnames(tempDf) <- paste0(colnames(tempDf), 'Target')
cohortIncidence <- merge(cohortIncidence, tempDf, by.x = 'targetId', by.y = 'cohortDefinitionIdTarget')
tempDf <- cohortDefinitions[,c('friendlyName','cohortDefinitionId',"subsetText")]
colnames(tempDf) <- paste0(colnames(tempDf), 'Outcome')
cohortIncidence <- merge(cohortIncidence, tempDf, by.x = 'outcomeId', by.y = 'cohortDefinitionIdOutcome')

```

# Cohort Incidence 

## Method
Incidence proportion per 100 patients, in each database, was calculated by finding the number of patients with `r targetName` recorded in the database who have `r outcomeName` recorded during a time-at-risk relative to exposure divided by the number of patients with `r targetName` recorded in the database multiplied by 100.

Incidence rate per 100 patient years, in each database, was calculated by finding the number of patients with `r targetName` recorded in the database who have `r outcomeName` recorded during a time-at-risk relative to exposure divided by the total number of patient years exposed to `r targetName` in the database multiplied by 100.

## Results
```{r}
#| label: ci_results
#| echo: false
#| results: 'asis'

headings <- cohortIncidence %>%
  dplyr::select('outcomeId', "cleanWindow","tar") %>%
  dplyr::distinct()

if(nrow(headings) > 0){
for(ind in 1:nrow(headings)){
      
      cohortIncidenceOfInt <-  cohortIncidence %>%
        filter(.data$outcomeId == !!headings$outcomeId[ind]) %>%
        filter(.data$tar == !!headings$tar[ind]) %>%
        filter(.data$cleanWindow == !!headings$cleanWindow[ind])
      
      boxTitle <- paste0('The incidence proportion/rate for outcome ', cohortDefinitions$friendlyName[cohortDefinitions$cohortDefinitionId == headings$outcomeId[ind]], ' occuring between ', headings$tar[ind], ' after exposure when the outcome clean window is ', headings$cleanWindow[ind] )
      

      cat('\n\n::: {.callout-note collapse="true"}')
      cat(paste0("\n# ",boxTitle, '\n\n'))
      
      if(nrow(cohortIncidenceOfInt) > 0){
        cat(gt::as_raw_html(
         cohortIncidenceOfInt %>% 
  dplyr::select("friendlyNameTarget","subsetTextTarget", "databaseName","personsAtRisk", "personDays", "personOutcomes", "incidenceProportionP100p", "incidenceRateP100py") %>%
  dplyr::group_by(
    .data$friendlyNameTarget,
    .data$subsetTextTarget
  ) %>%
  gt::gt() %>%
  gt::fmt_integer(
    columns = c("personsAtRisk", "personDays", "personOutcomes"), 
    suffixing = TRUE
  ) %>%
  gt::fmt_number(
    columns = c("incidenceProportionP100p", "incidenceRateP100py"),
    decimals = 4
  ) %>%
  gt::fmt(
    columns = c("personOutcomes","incidenceProportionP100p", "incidenceRateP100py"),
    fns = function(x){ 
      x <- round(x, digits = 4)
      if(sum(x < 0) > 0){ 
        x[x < 0] <- paste('<', abs(x[x < 0]))
      }
      return(x)
    }
  ) %>%
  gt::cols_label(
    databaseName = 'Database',
    personsAtRisk = "Persons at risk", 
    personDays = "Person Days",
    personOutcomes = "Outcome count",
    incidenceProportionP100p = " Incidence proportion per 100 persons",
    incidenceRateP100py = "Incidence rate per 100 person years"
  ) %>%
  gt::tab_options(
    heading.align = "left",
    data_row.padding = gt::px(7),
    column_labels.font.size = gt::px(12),
    column_labels.background.color = "#EB1800",
    column_labels.font.weight = "bold",
    row_group.background.color = "#D3D3D3",
    row_group.font.weight = "bold"
  ) %>%
  gt::tab_style(
    style = gt::cell_text(
      color = "darkgrey",
      font = gt::google_font("Source Sans Pro"),
      transform = "uppercase"
    ),
    locations = gt::cells_column_labels(gt::everything())
  ) %>%
  gt::tab_options(quarto.disable_processing = TRUE)
        ))
        cat('\n')
      } else{
        cat('No results to display\n')
      }
      
      cat('\n\n:::\n')
}
} else{
  cat('No cohort incidence results to display\n')
}

```
