# Cohorts

```{r}
#| label: cohort definitions target heading
#| echo: false
#| results: 'asis'

cat(paste0('\n\n## Target ',targetName, ' {#sec-target}\n\n'))

cat(paste0('> Full name: ',targets$cohortName[targets$cohortDefinitionId == targetId]))

cat(paste0('\n\n::: {.callout-note title="Cohort Definition"}'))
cat(paste0('\n\n### Cohort Definition\n\n'))

json <- targets$json[targets$cohortDefinitionId == params$targetId]

markdown <- tryCatch(
  {CirceR::cohortPrintFriendly(json)}, 
  error = function(e) return('')
)

cat(markdown)
cat('\n\n:::')

# if subset is not null then add that here
if(FALSE){ # figure out why this causes issue
#if(!is.null(cmSubsetIds)){
  
  for(i in 1:length(cmSubsetIds)){
    json <- subsets$json[subsets$subsetDefinitionId == cmSubsetIds[i]]

  cat("\n\n::: {.callout-note title='Subset'}\n")
  cat(paste0('\n\n### Subset ', i ,' used in CM'))
  
  cat('\n\n')
  cat(cmSubsetIds[i])
  cat(' Add json parsed code here')
  cat('\n\n')
  cat('\n\n:::\n\n')
  
  }
}
  
  if(!is.null(sccsSubsetIds)){
   
  for(i in 1:length(sccsSubsetIds)){
    json <- subsets$json[subsets$subsetDefinitionId == sccsSubsetIds[i]]

  cat("\n\n::: {.callout-note title='Subset'}")
  cat(paste0('\n\n### Subset ', i ,' used in SCCS'))
  
  cat('\n\n')
  cat('Add json parsed code here')
  cat('\n\n:::')
  
  }
}

```



```{r}
#| label: outcomes
#| echo: false
#| results: 'asis'
# Outcome

for(i in 1:nrow(outcomes)){
  cat(paste0('\n\n## Outcome ',outcomes$cohortFriendlyName[i], ' {#sec-',OhdsiReportGenerator::removeSpaces(outcomes$cohortFriendlyName[i]),'}\n\n'))
  
  cat(paste0("\n\n> Full name: ", outcomes$cohortName[i]))
  cat('\n\n::: {.callout-note title="Cohort Definition"}')
  cat('\n\n### Cohort Definition \n')
  
  json <- outcomes$json[i]
  
  markdown <- tryCatch(
    {CirceR::cohortPrintFriendly(json)}, 
    error = function(e) return('')
  )
  cat(markdown)
  cat('\n\n:::')
}

```


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

if(!is.null(indications)){
  # Indications

  for(i in 1:nrow(indications)){
    cat(paste0('\n\n## Indication ',indications$cohortFriendlyName[i], ' {#sec-',OhdsiReportGenerator::removeSpaces(indications$cohortFriendlyName[i]),'}\n\n'))
  
  cat(paste0("\n\n> Full name: ", indications$cohortName[i]))
  cat('\n\n::: {.callout-note title="Cohort Definition"}')
  cat("\n\n### Cohort Definition\n\n")
  
  json <- indications$json[i]
  
  markdown <- tryCatch(
    {CirceR::cohortPrintFriendly(json)}, 
    error = function(e) return('')
  )
  cat(markdown)
  cat('\n\n:::')
  }
}
```



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

if(!is.null(comparators)){
# Comparator

for(i in 1:nrow(unique(comparators))){
  cat(paste0('\n\n## Comparator ',comparators$cohortFriendlyName[i], ' {#sec-',OhdsiReportGenerator::removeSpaces(comparators$cohortFriendlyName[i]),'}\n\n'))
  
  cat(paste0('\n\n> Full name:', comparators$cohortName[i]))
  
  cat('\n\n::: {.callout-note title="Cohort Definition"}')
  cat('\n\n### Cohort Definition\n')
  
  json <- comparators$json[i]
  
  markdown <- tryCatch(
    {CirceR::cohortPrintFriendly(json)}, 
    error = function(e) return('')
  )
  cat(markdown)
  cat('\n\n:::')
  
  # add same subset stuff as target has here?
}
}

```
