---
title: "Add labels to results table"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{add-label-to-results-table}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

This is an example how to add labels to the results table.

```{r setup}
library(presentresults)
library(dplyr)
```

```{r}
# load dataset and kobo
results_table <- presentresults_MSNA2024_results_table

kobo_survey <- presentresults_MSNA2024_kobotool_template$kobo_survey
kobo_choices <- presentresults_MSNA2024_kobotool_template$kobo_choices
```
Note that the kobotools have additions, new indicators were added to the tools
```{r}
kobo_survey |>
  tail() |>
  select(type, name, `label::english`)
```


```{r}
kobo_choices |>
  tail() |>
  select(list_name, name, `label::english`)
```

First you should review the kobotool to see if there is any duplicated label, names, etc. This will cause issues later one. I am passing the results table so I only look at the variables presents in the results, not everything.

```{r}
review_kobo_labels_results <- review_kobo_labels(kobo_survey,
  kobo_choices,
  results_table = results_table
)
review_kobo_labels_results
```

In this case we have the HHS frequency question repeated, I will add which one they are referring to.

```{r}
kobo_survey_fixed <- kobo_survey
kobo_survey_fixed[
  which(kobo_survey_fixed[["label::english"]] == "How often did this happen in the past [4 weeks/30 days]?"),
  "label::english"
] <- paste(
  "How often did this happen in the past [4 weeks/30 days]? ---",
  c(
    "In the past 4 weeks (30 days), was there ever no food to eat of any kind in your house because of lack of resources to get food?",
    "In the past 4 weeks (30 days), did you or any household member go to sleep at night hungry because there was not enough food?",
    "In the past 4 weeks (30 days), did you or any household member go a whole day or night without eating anything at all because there was not enough food?"
  )
)
```


Then I will deal in the choices sheet. There are 2 flags:
- Kobo choices sheet has duplicated names in the same list_name.	
- Kobo choices sheet has duplicated labels in the same list_name.	

For the duplicated names in the same list name, these were added with the composite indicators. I will just keep one.
```{r}
kobo_choices_fixed <- kobo_choices |>
  filter(!`label::english` %in% c(
    "No shelter (sleeping in the open)",
    "Surface water (river, dam, lake, pond, stream, canal, irrigation channel)"
  ))

duplicated_listname_label <- review_kobo_labels_results |> filter(comments == "Kobo choices sheet has duplicated labels in the same list_name.")
```

For the duplicated labels, these are because of the template. I will just add a number based on their order.
```{r}
kobo_choices_fixed <- kobo_choices_fixed |>
  group_by(list_name)  |> 
  mutate(`label::english` = case_when(
    list_name %in% duplicated_listname_label$list_name ~ paste(`label::english`, row_number()),
    TRUE ~ `label::english`
  ))  |> 
  ungroup()
```

I can review again.
```{r}
review_kobo_labels(kobo_survey_fixed, kobo_choices_fixed, results_table = results_table)
```

I can now create a dictionary that will be used to create labels.
```{r}
label_dictionary <- create_label_dictionary(kobo_survey_fixed, kobo_choices_fixed, results_table = results_table)
```

I can then add the labels to the results table.
```{r}
results_table_labeled <- add_label_columns_to_results_table(
  results_table,
  label_dictionary
)

results_table_labeled |> 
  head()
```
