
```{r}
data_domi <- read.csv(file.path(per_dominante_folder, cah_data_list[{{i}}]), check.names = FALSE)

nom_dominante <- as.character(unique(data_domi[["dominante3A"]]))

colnames(data_domi[6:ncol(data_domi)-1]) <- c(
    "1AC-OUVERTURE", "1AC-MI",
    "2A-UEchoix-S1-UC1", "2A-UEchoix-S1-UC2", "2A-UEchoix-S2-UC3",
    "2A-UEchoix-S2-UC4",
    "2A-UEchoix-S2-UC5", "2A-UEchoix-S2-UC6",
    "2A-Projet-S2"
)

data_domi <- data_domi %>%
    mutate_if(sapply(data_domi, is.character), as.factor)

for (code_bloc in colnames(data_domi)[6:ncol(data_domi) - 1]) {
    # code_bloc <- "1AC-OUVERTURE"
    matching_code_cours <- data_details_sequence[which(data_details_sequence[["code_bloc"]] == code_bloc), c("code_cours", "cours")]
    row_id_in_matching <- which(matching_code_cours[["code_cours"]] %in% levels(data_domi[[code_bloc]]))
    levels(data_domi[[code_bloc]]) <- matching_code_cours[row_id_in_matching, "cours"]
}

effectif_cluster_domi <- table(data_domi[["cluster"]])
```

### Dominante : `r nom_dominante`

#### Graphiques

```{r}
#| plotly: true
#| result: asis

ac <- NULL
vec_sequences <- (colnames(data_domi)[4:ncol(data_domi)-1])
htmltools::tagList(lapply(vec_sequences, function (sequence) {
    ac <- as.data.frame(table(data_domi[["cluster"]], data_domi[[sequence]], useNA = "ifany"))
    colnames(ac) <- c("cluster", "uc", "nb")
    ac <- ac %>% group_by(cluster) %>% mutate(Prop = nb/sum(nb), uc = str_wrap(uc,32))
    p <- ggplotly(ac %>% plot_prop_cluster(eff = effectif_cluster_domi) + ggtitle(paste("Proportion pour", sequence, "- domaine :", nom_dominante)))
    p
}))
```

#### Tables
```{r} 
#| tbl-cap: !expr 'paste("Table des proportions de choix par cluster pour ",(colnames(data)[3:ncol(data)-1]))'
#| result: asis
ac <- NULL
vec_sequences <- (colnames(data_domi)[4:ncol(data_domi)-1])
for(sequence in vec_sequences) {
    ac <- as.data.frame(table(data_domi[["cluster"]], data_domi[[sequence]], useNA = "ifany"))
    colnames(ac) <- c("cluster", "uc", "nb")
    ac <- ac %>% group_by(cluster) %>% mutate(Prop = nb/sum(nb), uc = str_wrap(uc,30))
    ac_table <- ac %>% select(-nb) %>% pivot_wider(names_from = uc, values_from = Prop)
    print(ac_table %>% datatable(filter = "top") %>% formatRound(columns = - 1, digits = 3))
}
```