Skip to content

Commit 45053f2

Browse files
committed
✏️ Handle editing existing scenarios
1 parent 1f96640 commit 45053f2

File tree

6 files changed

+233
-37
lines changed

6 files changed

+233
-37
lines changed

R/mod_inequalities_server.R

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

R/mod_inequalities_utils.R

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#' Initialise inequality choice table
2+
#'
3+
#' Creates a tibble containing unique HRG codes for selected provider and
4+
#' a "choice" column indicating the type of adjustment to apply.
5+
#' Set to "No change" by default
6+
#'
7+
#' If editing an existing scenario, previous parameters will be loaded.
8+
#'
9+
#' @param provider_inequalities A data frame containing provider inequality data.
10+
#' @param params The standard params object
11+
#'
12+
#' @return A tibble with two columns:
13+
#' \item{hrg_code}{Character vector of unique HRG codes from the input data}
14+
#' \item{choice}{Character vector indicating the inequality adjustment choice
15+
#' for each HRG code.}
16+
#' Possible values are defined in get_inequality_choice_mappings()
17+
#'
18+
19+
initialise_hrg_table <- function(provider_inequalities, params) {
20+
# Get unique HRG codes from the data
21+
unique_hrg_codes <- unique(provider_inequalities$sushrg_trimmed)
22+
23+
hrg_table <- tibble::tibble(
24+
hrg_code = unique_hrg_codes,
25+
choice = "No change"
26+
)
27+
28+
# Check if params$inequalities has existing data
29+
if (!is.null(params$inequalities) && length(params$inequalities) > 0) {
30+
# Update choices based on existing params$inequalities
31+
for (choice_type in names(params$inequalities)) {
32+
display_choice <- inequality_choices_to_display(choice_type)
33+
34+
# Get the HRG codes for this choice type
35+
hrg_codes_for_choice <- params$inequalities[[choice_type]]
36+
37+
# Update the selections for matching HRG codes
38+
hrg_table$choice[
39+
hrg_table$hrg_code %in% hrg_codes_for_choice
40+
] <- display_choice
41+
}
42+
}
43+
44+
hrg_table
45+
}
46+
47+
#' Get Inequality Choice Mappings
48+
#
49+
#' Returns the mapping between programmatic snake_case and display
50+
#' names for inequality adjustment choices.
51+
#'
52+
#' @return Named character vector where names are snake_case identifiers and
53+
#' values are display names.
54+
get_inequality_choice_mappings <- function() {
55+
c(
56+
no_change = "No change",
57+
zero_sum = "Zero-sum",
58+
level_up = "Level up",
59+
level_down = "Level down"
60+
)
61+
}
62+
63+
#' Convert inequality choices to snake case
64+
#'
65+
#' @param display_choice Character vector of display names e.g., Zero-sum"
66+
#'
67+
#' @return Character vector of snake_case names. Returns \code{NA} for
68+
#' unrecognized values.
69+
#'
70+
#' @examples
71+
#' inequality_choices_to_snake("Zero-sum")
72+
#'
73+
inequality_choices_to_snake <- function(display_choice) {
74+
mappings <- get_inequality_choice_mappings()
75+
names(mappings)[match(display_choice, mappings)]
76+
}
77+
78+
#' Convert inequality choices to display case
79+
#'
80+
#' @param snake_choice Character vector of snake_case names e.g., 'zero_sum'
81+
#'
82+
#' @return Character vector of display names. Returns \code{NA} for
83+
#' unrecognized values.
84+
#'
85+
#' @examples
86+
#' inequality_choices_to_display("zero_sum")
87+
#'
88+
inequality_choices_to_display <- function(snake_choice) {
89+
mappings <- get_inequality_choice_mappings()
90+
unname(mappings[snake_choice])
91+
}

man/get_inequality_choice_mappings.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/inequality_choices_to_display.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/inequality_choices_to_snake.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/initialise_hrg_table.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)