Skip to content

Commit 9d24c04

Browse files
authored
Merge pull request #623 from The-Strategy-Unit/fix_interval_issue
fix interval issue
2 parents 4bff62d + 4b134f4 commit 9d24c04

4 files changed

Lines changed: 52 additions & 88 deletions

File tree

R/fct_age_pyramid.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
age_pyramid <- function(age_data) {
22
age_data |>
33
dplyr::mutate(
4-
dplyr::across("n", `*`, ifelse(.data$sex == 1, -1, 1)),
4+
dplyr::across("n", \(.x) .x * ifelse(.data$sex == 1, -1, 1)),
55
dplyr::across(
66
"sex",
7-
~ forcats::fct_recode(
8-
factor(.x, levels = c("1", "2")),
9-
"Males" = "1",
10-
"Females" = "2"
11-
)
7+
\(.x) {
8+
forcats::fct_recode(
9+
factor(.x, levels = c("1", "2")),
10+
"Males" = "1",
11+
"Females" = "2"
12+
)
13+
}
1214
)
1315
) |>
1416
ggplot2::ggplot(

R/mod_mitigators_server.R

Lines changed: 31 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ mod_mitigators_server <- function(
2828
activity_type <- config$activity_type
2929
mitigators_type <- config$mitigators_type
3030

31-
param_conversion <- list(
32-
absolute = list(\(r, p) p * r, \(r, q) q / r),
33-
relative = list(\(r, p) p, \(r, q) q)
34-
)
35-
3631
reasons_key <- shiny::reactiveVal()
3732
mod_reasons_server(
3833
shiny::NS(id, "reasons"),
@@ -44,7 +39,6 @@ mod_mitigators_server <- function(
4439

4540
shiny::moduleServer(id, function(input, output, session) {
4641
slider_values <- shiny::reactiveValues()
47-
output_conversions <- shiny::reactiveValues()
4842
time_profile_mappings <- shiny::reactiveValues()
4943

5044
strategies <- shiny::reactive({
@@ -114,33 +108,16 @@ mod_mitigators_server <- function(
114108
)
115109
)
116110

117-
output_conversions[[mitigators_type]][[
118-
i
119-
]] <- (config$param_output %||% \(...) identity)(r$rate)
120-
121111
params[[mitigators_type]][[activity_type]][[i]] <- if (
122112
!is.null(loaded_values[[i]])
123113
) {
124-
fn <- output_conversions[[mitigators_type]][[i]]
125-
126-
v <- slider_values[[mitigators_type]][[i]]
127-
v$interval <- fn(v$interval)
128-
129-
v
114+
slider_values[[mitigators_type]][[i]]
130115
}
131116
})
132117

133118
tpm <- params$time_profile_mappings[[mitigators_type]][[activity_type]]
134119
time_profile_mappings$mappings <- tpm
135120

136-
shiny::updateCheckboxInput(
137-
session,
138-
"include",
139-
value = !is.null(params[[mitigators_type]][[
140-
activity_type
141-
]][[strategies[[1]]]])
142-
)
143-
144121
init$destroy()
145122
},
146123
priority = 100
@@ -198,9 +175,11 @@ mod_mitigators_server <- function(
198175

199176
shiny::observe({
200177
# ensure include checkbox is on or off given param value
201-
shiny::req(input$strategy)
178+
strategy <- shiny::req(input$strategy)
179+
# Wait for slider to be initialized before updating checkbox
180+
shiny::req(slider_values[[mitigators_type]][[strategy]])
202181
include <- !is.null(params[[mitigators_type]][[activity_type]][[
203-
input$strategy
182+
strategy
204183
]])
205184
shiny::updateCheckboxInput(session, "include", value = include)
206185
}) |>
@@ -209,24 +188,13 @@ mod_mitigators_server <- function(
209188
shiny::observe({
210189
# update slider
211190
strategy <- shiny::req(input$strategy)
212-
max_value <- provider_max_value()
213191
scale <- 100
214-
range <- c(0, 100)
215-
step <- 0.1
216-
pc_fn <- param_conversion$relative[[1]]
217-
218-
values <- pc_fn(
219-
max_value,
220-
slider_values[[mitigators_type]][[strategy]]$interval
221-
) *
222-
scale
192+
193+
values <- slider_values[[mitigators_type]][[strategy]]$interval * scale
223194
shiny::updateSliderInput(
224195
session,
225196
"slider",
226-
value = values,
227-
min = range[[1]],
228-
max = range[[2]],
229-
step = step
197+
value = values
230198
)
231199

232200
update_time_profile(
@@ -238,23 +206,16 @@ mod_mitigators_server <- function(
238206
shiny::observe({
239207
values <- input$slider
240208
strategy <- shiny::req(input$strategy)
241-
max_value <- provider_max_value()
209+
# Ensure slider values have been initialized for this strategy
210+
shiny::req(slider_values[[mitigators_type]][[strategy]])
242211
scale <- 100
243-
pc_fn <- param_conversion$relative[[2]]
244-
245-
v <- pc_fn(max_value, values / scale)
246212

247213
at <- activity_type
248214
mt <- mitigators_type
249-
slider_values[[mt]][[strategy]]$interval <- v
215+
slider_values[[mt]][[strategy]]$interval <- values / scale
250216

251217
params[[mt]][[at]][[strategy]] <- if (input$include) {
252-
fn <- output_conversions[[mitigators_type]][[strategy]]
253-
254-
v <- slider_values[[mitigators_type]][[strategy]]
255-
v$interval <- fn(v$interval)
256-
257-
v
218+
slider_values[[mitigators_type]][[strategy]]
258219
}
259220
}) |>
260221
shiny::bindEvent(input$slider, input$include)
@@ -279,12 +240,14 @@ mod_mitigators_server <- function(
279240
# plot ribbon to show selected params ----
280241

281242
plot_ribbon <- shiny::reactive({
282-
max_value <- dplyr::filter(rates_baseline_data(), !.data$is_peer)$rate
243+
baseline_value <- dplyr::filter(
244+
rates_baseline_data(),
245+
!.data$is_peer
246+
)$rate
283247

284-
values <- param_conversion$absolute[[1]](
285-
max_value,
286-
slider_values[[mitigators_type]][[input$strategy]]$interval
287-
)
248+
# convert the slider values to absolute values
249+
interval <- slider_values[[mitigators_type]][[input$strategy]]$interval
250+
values <- interval * baseline_value
288251

289252
colour <- "#f9bf07"
290253

@@ -620,34 +583,23 @@ mod_mitigators_server <- function(
620583
# rate values ----
621584

622585
output$slider_absolute <- shiny::renderUI({
623-
scale <- config$slider_scale
624586
strategy <- shiny::req(input$strategy)
625-
max_value <- provider_max_value()
626-
627-
convert_params_a <- param_conversion$absolute[[1]]
628-
rate <- convert_params_a(
629-
max_value,
630-
slider_values[[mitigators_type]][[strategy]]$interval
631-
) *
632-
scale
633-
634-
convert_number <- function(value, config) {
635-
converted <- scales::number(value, 0.001)
636-
is_percent <- stringr::str_detect(config$y_axis_title, "%")
637-
if (is_percent) {
638-
converted <- scales::number(value, 0.01, suffix = "%")
639-
}
640-
converted
641-
}
587+
baseline_value <- provider_max_value()
588+
589+
number_format <- config$interval_text_number_type %||% config$number_type
590+
591+
# convert the slider values to absolute values
592+
interval_str <- number_format(
593+
slider_values[[mitigators_type]][[strategy]]$interval * baseline_value
594+
)
642595

643-
rate_lo <- convert_number(rate[1], config)
644-
rate_hi <- convert_number(rate[2], config)
645-
rate_max <- convert_number(max_value * scale, config)
596+
rate_baseline <- number_format(baseline_value)
646597
y_axis_title <- config$y_axis_title
647598

648599
text <- glue::glue(
649-
"This is equivalent to a rate interval of {rate_lo} to {rate_hi}",
650-
"({y_axis_title}) given the baseline of {rate_max}.",
600+
"This is equivalent to a rate interval of {interval_str[[1]]} to",
601+
"{interval_str[[2]]}",
602+
"({y_axis_title}) given the baseline of {rate_baseline}.",
651603
.sep = " "
652604
)
653605

R/mod_mitigators_ui.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ mod_mitigators_ui <- function(id, title, show_diagnoses_table = TRUE) {
3636
shiny::sliderInput(
3737
ns("slider"),
3838
"80% prediction interval",
39-
0,
40-
1,
41-
c(0, 1)
39+
min = 0,
40+
max = 100,
41+
value = c(0, 100),
42+
step = 0.1
4243
),
4344
shiny::htmlOutput(ns("slider_absolute")),
4445
shiny::p(),

inst/golem-config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ default:
137137
y_axis_title: "% of Procedures Performed in Non-Target Setting"
138138
x_axis_title: "Financial Year"
139139
number_type: !expr scales::percent_format()
140+
interval_text_number_type: !expr scales::percent_format(accuracy = 0.01)
140141
funnel_x_title: "Number of Procedures Performed"
141142
slider_scale: 100
142143
slider_step: 0.1
@@ -151,6 +152,7 @@ default:
151152
y_axis_title: "% of Procedures Performed in Non-Target Setting"
152153
x_axis_title: "Financial Year"
153154
number_type: !expr scales::percent_format()
155+
interval_text_number_type: !expr scales::percent_format(accuracy = 0.01)
154156
funnel_x_title: "Number of Procedures Performed"
155157
slider_scale: 100
156158
slider_step: 0.1
@@ -165,6 +167,7 @@ default:
165167
y_axis_title: "% of Appointments that are Consultant to Consultant Referrals"
166168
x_axis_title: "Financial Year"
167169
number_type: !expr scales::percent_format()
170+
interval_text_number_type: !expr scales::percent_format(accuracy = 0.01)
168171
funnel_x_title: "Number of Appointments"
169172
slider_scale: 100
170173
slider_step: 0.1
@@ -179,6 +182,7 @@ default:
179182
y_axis_title: "% of Appointments that are Face-to-Face"
180183
x_axis_title: "Financial Year"
181184
number_type: !expr scales::percent_format()
185+
interval_text_number_type: !expr scales::percent_format(accuracy = 0.01)
182186
funnel_x_title: "Number of Appointments"
183187
slider_scale: 100
184188
slider_step: 0.1
@@ -207,6 +211,7 @@ default:
207211
y_axis_title: "% of Attendances that were GP Referred First Attendances"
208212
x_axis_title: "Financial Year"
209213
number_type: !expr scales::percent_format()
214+
interval_text_number_type: !expr scales::percent_format(accuracy = 0.01)
210215
funnel_x_title: "Number of Appointments"
211216
slider_scale: 100
212217
slider_step: 0.1
@@ -221,6 +226,7 @@ default:
221226
y_axis_title: "% of Attendances that were Frequent Attenders"
222227
x_axis_title: "Financial Year"
223228
number_type: !expr scales::percent_format()
229+
interval_text_number_type: !expr scales::percent_format(accuracy = 0.01)
224230
funnel_x_title: "Number of Attendances"
225231
slider_scale: 100
226232
slider_step: 0.1
@@ -235,6 +241,7 @@ default:
235241
y_axis_title: "% of Attendances Where the Patient Left Before Being Seen"
236242
x_axis_title: "Financial Year"
237243
number_type: !expr scales::percent_format()
244+
interval_text_number_type: !expr scales::percent_format(accuracy = 0.01)
238245
funnel_x_title: "Number of Attendances"
239246
slider_scale: 100
240247
slider_step: 0.1
@@ -249,6 +256,7 @@ default:
249256
y_axis_title: "% of Arrivals where the patient was discharged\nwith no treatment or investigations being performed"
250257
x_axis_title: "Financial Year"
251258
number_type: !expr scales::percent_format()
259+
interval_text_number_type: !expr scales::percent_format(accuracy = 0.01)
252260
funnel_x_title: "Number of Attendances"
253261
slider_scale: 100
254262
slider_step: 0.1
@@ -263,6 +271,7 @@ default:
263271
y_axis_title: "% of Attendances Which Were Low Cost and Patient Was Discharged Home"
264272
x_axis_title: "Financial Year"
265273
number_type: !expr scales::percent_format()
274+
interval_text_number_type: !expr scales::percent_format(accuracy = 0.01)
266275
funnel_x_title: "Number of Attendances"
267276
slider_scale: 100
268277
slider_step: 0.1

0 commit comments

Comments
 (0)