@@ -7,7 +7,9 @@ mod_inequalities_server <- function(id, params) {
77
88 mod_reasons_server(shiny :: NS(id , " reasons" ), params , " inequalities" )
99
10- inequalities_data <- shiny :: reactive({
10+ # This is the data for each HRG split by IMD for the selector provider
11+ # load_inequalities_data() is pulling from Azure so might take some time
12+ provider_inequalities <- shiny :: reactive({
1113 dataset <- shiny :: req(params $ dataset ) # nolint: object_usage_linter
1214
1315 load_inequalities_data() | >
@@ -16,52 +18,61 @@ mod_inequalities_server <- function(id, params) {
1618 )
1719 })
1820
19- # Initialize reactiveValues with NULL
20- hrg <- reactiveValues(
21+ # hrg is used to track the current choice selections in table form
22+ hrg <- shiny :: reactiveValues(
2123 selections = NULL
2224 )
2325
24- # Initialise data once inequalities_data is available
25- observe({
26- req(inequalities_data())
26+ # Initialisation
27+ init <- shiny :: observe(
28+ {
29+ # Wait for data to be available
30+ shiny :: req(provider_inequalities())
2731
28- if (is.null(hrg $ selections )) {
29- hrg $ selections <- tibble :: tibble(
30- hrg_code = unique(inequalities_data()$ sushrg_trimmed ),
31- choice = " No change"
32- )
33- }
34- })
32+ hrg $ selections <- initialise_hrg_table(provider_inequalities(), params )
3533
36- # Handle "Set all to zero sum" button
37- observeEvent(input $ set_all_zero_sum , {
34+ # Destroy the observer so it only runs once
35+ init $ destroy()
36+ },
37+ priority = - 1 # Low priority to ensure other reactives are ready
38+ )
39+
40+ # "Set all to zero sum" button
41+ shiny :: observeEvent(input $ set_all_zero_sum , {
42+ shiny :: req(hrg $ selections )
3843 hrg $ selections $ choice <- " Zero-sum"
3944 })
4045
41- # Handle "Clear all" button
42- observeEvent(input $ clear_all , {
46+ # "Clear all" button
47+ shiny :: observeEvent(input $ clear_all , {
48+ shiny :: req(hrg $ selections )
4349 hrg $ selections $ choice <- " No change"
4450 })
4551
4652 output $ hrg_table <- DT :: renderDataTable({
47- # Create dropdown options for Choice column
48- choice_options <- c(" No change" , " Zero-sum" , " Level up" , " Level down" )
53+ shiny :: req(hrg $ selections )
54+
55+ choice_options <- unname(get_inequality_choice_mappings())
4956
5057 # Create the dropdown HTML for each row
51- dropdown_html <- sapply(seq_len(nrow(hrg $ selections )), function (i ) {
52- current_choice <- hrg $ selections $ choice [i ]
53- options_html <- glue :: glue_collapse(
54- sapply(choice_options , function (option ) {
55- selected <- if (option == current_choice ) " selected" else " "
56- glue :: glue(" <option value='{option}' {selected}>{option}</option>" )
57- })
58- )
59- glue :: glue(
60- " <select class='choice-select' data-row='{i}'>{options_html}</select>"
61- )
62- })
58+ dropdown_html <- purrr :: map_chr(
59+ seq_len(nrow(hrg $ selections )),
60+ function (i ) {
61+ current_choice <- hrg $ selections $ choice [i ]
62+ options_html <- glue :: glue_collapse(
63+ purrr :: map_chr(choice_options , function (option ) {
64+ selected <- if (option == current_choice ) " selected" else " "
65+ glue :: glue(
66+ " <option value='{option}' {selected}>{option}</option>"
67+ )
68+ })
69+ )
70+ glue :: glue(
71+ " <select class='choice-select' data-row='{i}'>{options_html}</select>"
72+ )
73+ }
74+ )
6375
64- # Replace the choice column with dropdown HTML
6576 display_data <- hrg $ selections
6677 display_data $ choice <- dropdown_html
6778
@@ -91,32 +102,35 @@ mod_inequalities_server <- function(id, params) {
91102
92103 # Handle dropdown changes
93104 shiny :: observeEvent(input $ choice_changed , {
105+ shiny :: req(hrg $ selections )
106+
94107 row_index <- input $ choice_changed $ row
95- new_value <- input $ choice_changed $ value
96108
97109 # Update the data
98- hrg $ selections $ choice [row_index ] <- new_value
110+ hrg $ selections $ choice [row_index ] <- input $ choice_changed $ value
99111 })
100112
113+ # Download inequalities data
101114 output $ download_inequalities <- shiny :: downloadHandler(
102115 filename = \() glue :: glue(" {params[['dataset']]}_inequalities.csv" ),
103116 content = \(file ) {
104- readr :: write_csv(inequalities_data (), file )
117+ readr :: write_csv(provider_inequalities (), file )
105118 }
106119 )
107120
108121 shiny :: observe({
122+ shiny :: req(hrg $ selections )
123+
109124 params $ inequalities <-
110125 hrg $ selections | >
111126 dplyr :: filter(.data $ choice != " No change" ) | >
112127 dplyr :: mutate(
113- choice = stringr :: str_to_snake( stringr :: str_to_lower( .data $ choice ) )
128+ choice = inequality_choices_to_snake( .data $ choice )
114129 ) | >
115130 dplyr :: group_by(.data $ choice ) | >
116131 dplyr :: summarise(hrg_codes = list (.data $ hrg_code )) | >
117132 tibble :: deframe() | >
118133 purrr :: map(I ) # Forces any single values to stay in a list (asis)
119-
120134 })
121135 })
122136}
0 commit comments