Skip to content

Commit e160038

Browse files
committed
more eagerly filter data, and move single-use reactives to their respective modules
1 parent 8829ce7 commit e160038

6 files changed

Lines changed: 98 additions & 92 deletions

R/app_server.R

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,24 @@ app_server <- function(input, output, session) {
2424
})
2525

2626
age_sex_data <- shiny::reactive({
27-
get_age_sex_data()
28-
})
29-
30-
diagnoses_data <- shiny::reactive({
31-
get_diagnoses_data()
32-
})
33-
34-
procedures_data <- shiny::reactive({
35-
get_procedures_data()
36-
})
27+
provider <- params$dataset
28+
fyear <- params$start_year
3729

38-
baseline_data <- shiny::reactive({
39-
get_baseline_data()
30+
get_age_sex_data(provider, fyear)
4031
})
4132

42-
inequalities_data <- shiny::reactive({
43-
get_inequalities_data()
44-
})
33+
diagnoses_data <- shiny::reactive({
34+
provider <- params$dataset
35+
fyear <- params$start_year
4536

46-
expat_data <- shiny::reactive({
47-
get_expat_data()
37+
get_diagnoses_data(provider, fyear)
4838
})
4939

50-
repat_local_data <- shiny::reactive({
51-
get_repat_local_data()
52-
})
40+
procedures_data <- shiny::reactive({
41+
provider <- params$dataset
42+
fyear <- params$start_year
5343

54-
repat_nonlocal_data <- shiny::reactive({
55-
get_repat_nonlocal_data()
44+
get_procedures_data(provider, fyear)
5645
})
5746

5847
# load all other modules once the home module has finished loading
@@ -78,11 +67,7 @@ app_server <- function(input, output, session) {
7867
unique()
7968
})
8069

81-
mod_baseline_adjustment_server(
82-
"baseline_adjustment",
83-
baseline_data(),
84-
params
85-
)
70+
mod_baseline_adjustment_server("baseline_adjustment", params)
8671

8772
mod_population_growth_server("population_growth", params)
8873

@@ -100,15 +85,9 @@ app_server <- function(input, output, session) {
10085
)
10186
})
10287

103-
mod_inequalities_server("inequalities", inequalities_data(), params)
88+
mod_inequalities_server("inequalities", params)
10489

105-
mod_expat_repat_server(
106-
"expat_repat",
107-
expat_data(),
108-
repat_local_data(),
109-
repat_nonlocal_data(),
110-
params
111-
)
90+
mod_expat_repat_server("expat_repat", params)
11291

11392
mod_non_demographic_adjustment_server("non_demographic_adjustment", params)
11493

@@ -133,10 +112,10 @@ app_server <- function(input, output, session) {
133112
),
134113
mod_mitigators_server,
135114
params,
136-
rates_data(),
137-
age_sex_data(),
138-
diagnoses_data(),
139-
procedures_data(),
115+
rates_data,
116+
age_sex_data,
117+
diagnoses_data,
118+
procedures_data,
140119
available_strategies
141120
)
142121

R/fct_load_provider_data.R

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ get_rates_data <- function() {
3535
dplyr::inner_join(national_rate, by = c("fyear", "strategy"))
3636
}
3737

38-
get_age_sex_data <- function() {
38+
get_age_sex_data <- function(provider, fyear) {
3939
age_sex <- load_provider_data("age_sex")
4040

4141
age_fct <- sort(unique(age_sex[["age_group"]]))
4242

4343
age_sex |>
44+
dplyr::filter(
45+
.data$provider == .env$provider,
46+
.data$fyear == .env$fyear
47+
) |>
4448
dplyr::mutate(
4549
age_group = factor(
4650
.data[["age_group"]],
@@ -49,24 +53,43 @@ get_age_sex_data <- function() {
4953
)
5054
}
5155

52-
get_diagnoses_data <- function() {
53-
load_provider_data("diagnoses")
56+
get_diagnoses_data <- function(provider, fyear) {
57+
load_provider_data("diagnoses") |>
58+
dplyr::filter(
59+
.data$provider == .env$provider,
60+
.data$fyear == .env$fyear
61+
)
5462
}
5563

56-
get_procedures_data <- function() {
57-
load_provider_data("procedures")
64+
get_procedures_data <- function(provider, fyear) {
65+
load_provider_data("procedures") |>
66+
dplyr::filter(
67+
.data$provider == .env$provider,
68+
.data$fyear == .env$fyear
69+
)
5870
}
5971

60-
get_baseline_data <- function() {
61-
load_provider_data("baseline")
72+
get_baseline_data <- function(provider, fyear) {
73+
load_provider_data("baseline") |>
74+
dplyr::filter(
75+
.data$provider == .env$provider,
76+
.data$fyear == .env$fyear
77+
)
6278
}
6379

64-
get_inequalities_data <- function() {
65-
load_provider_data("inequalities")
80+
get_inequalities_data <- function(provider, fyear) {
81+
load_provider_data("inequalities") |>
82+
dplyr::filter(
83+
.data$provider == .env$provider,
84+
.data$fyear == .env$fyear
85+
)
6686
}
6787

68-
get_expat_data <- function() {
69-
load_provider_data("expat")
88+
get_expat_data <- function(provider) {
89+
load_provider_data("expat") |>
90+
dplyr::filter(
91+
.data$provider == .env$provider
92+
)
7093
}
7194

7295
get_repat_local_data <- function() {

R/mod_baseline_adjustment_server.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#' baseline_adjustment Server Functions
22
#'
33
#' @noRd
4-
mod_baseline_adjustment_server <- function(id, baseline_data, params) {
4+
mod_baseline_adjustment_server <- function(id, params) {
55
mod_reasons_server(shiny::NS(id, "reasons"), params, "baseline_adjustment")
66

77
rtt_specialties <- get_lookups()[["rtt_specialties"]]
88

99
shiny::moduleServer(id, function(input, output, session) {
10+
baseline_data <- shiny::reactive({
11+
get_baseline_data(params$dataset, params$start_year)
12+
})
13+
1014
# static data ----
1115

1216
# creates a table containing all of the options shown in the baseline adjustment page, including the input id
@@ -48,7 +52,7 @@ mod_baseline_adjustment_server <- function(id, baseline_data, params) {
4852
year <- as.character(shiny::req(params$start_year))
4953
# nolint end
5054

51-
baseline_data |>
55+
baseline_data() |>
5256
dplyr::filter(
5357
.data[["fyear"]] == .env[["year"]],
5458
.data[["provider"]] == .env[["dataset"]]

R/mod_expat_repat_server.R

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
#' expat_repat Server Functions
22
#'
33
#' @noRd
4-
mod_expat_repat_server <- function(
5-
id,
6-
expat_data,
7-
repat_local_data,
8-
repat_nonlocal_data,
9-
params
10-
) {
4+
mod_expat_repat_server <- function(id, params) {
115
providers <- get_lookups()[["providers"]]
126

137
selected_time_profile <- update_time_profile <- NULL
@@ -28,6 +22,18 @@ mod_expat_repat_server <- function(
2822
mod_reasons_server(shiny::NS(id, "reasons"), params, "expat_repat")
2923

3024
shiny::moduleServer(id, function(input, output, session) {
25+
expat_data <- shiny::reactive({
26+
get_expat_data(params$dataset)
27+
})
28+
29+
repat_local_data <- shiny::reactive({
30+
get_repat_local_data()
31+
})
32+
33+
repat_nonlocal_data <- shiny::reactive({
34+
get_repat_nonlocal_data()
35+
})
36+
3137
# helpers ----
3238

3339
extract_expat_repat_data <- function(dat) {
@@ -58,24 +64,21 @@ mod_expat_repat_server <- function(
5864

5965
# extract the expat data for the current selection
6066
expat <- shiny::reactive({
61-
ds <- shiny::req(params$dataset)
62-
63-
expat_data |>
64-
dplyr::filter(.data$provider == ds) |>
67+
expat_data() |>
6568
extract_expat_repat_data() |>
6669
dplyr::select("fyear", "count")
6770
})
6871

6972
# extract the repat local data for the current selection
7073
repat_local <- shiny::reactive({
71-
repat_local_data |>
74+
repat_local_data() |>
7275
extract_expat_repat_data() |>
7376
dplyr::select("fyear", "icb", "provider", "count", "pcnt")
7477
})
7578

7679
# extract the repat nonlocal data for the current selection
7780
repat_nonlocal <- shiny::reactive({
78-
repat_nonlocal_data |>
81+
repat_nonlocal_data() |>
7982
extract_expat_repat_data() |>
8083
dplyr::select(
8184
"fyear",

R/mod_inequalities_server.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#' inequalities Server Functions
22
#'
33
#' @noRd
4-
mod_inequalities_server <- function(id, inequalities_data, params) {
4+
mod_inequalities_server <- function(id, params) {
55
shiny::moduleServer(id, function(input, output, session) {
66
ns <- session$ns
77

8+
inequalities_data <- shiny::reactive({
9+
provider <- params$dataset
10+
fyear <- params$start_year
11+
12+
get_inequalities_data(provider, fyear)
13+
})
14+
815
mod_reasons_server(shiny::NS(id, "reasons"), params, "inequalities")
916

1017
# This is the data for each HRG split by IMD for the selector provider
1118
# load_inequalities_data() is pulling from Azure so might take some time
1219
provider_inequalities <- shiny::reactive({
1320
dataset <- shiny::req(params$dataset) # nolint: object_usage_linter
1421

15-
inequalities_data |>
22+
inequalities_data() |>
1623
dplyr::filter(
1724
.data[["provider"]] == .env[["dataset"]]
1825
)

R/mod_mitigators_server.R

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ mod_mitigators_server <- function(
193193
dplyr::pull(.data$peer)
194194
# nolint end
195195

196-
rates_data |>
196+
rates_data() |>
197197
dplyr::filter(
198198
.data$strategy == .env$strategy,
199199
.data$fyear == params$start_year
@@ -301,7 +301,7 @@ mod_mitigators_server <- function(
301301
# use the rates data, filtered to the provider that has been selected
302302
trend_data <- shiny::reactive({
303303
strategy <- shiny::req(input$strategy)
304-
rates_data |>
304+
rates_data() |>
305305
dplyr::filter(
306306
.data$strategy == .env$strategy,
307307
.data$provider == params$dataset
@@ -383,21 +383,19 @@ mod_mitigators_server <- function(
383383

384384
# render the diagnoses table
385385
output$diagnoses_table <- gt::render_gt({
386+
data <- diagnoses_data()
387+
386388
shiny::validate(
387389
shiny::need(
388-
diagnoses_data,
390+
data,
389391
message = "Insufficient or suppressed data."
390392
)
391393
)
392394

393395
strategy <- shiny::req(input$strategy)
394396

395-
data <- diagnoses_data |>
396-
dplyr::filter(
397-
.data$provider == params$dataset,
398-
.data$strategy == .env$strategy,
399-
.data$fyear == params$start_year
400-
) |>
397+
data <- data |>
398+
dplyr::filter(.data$strategy == .env$strategy) |>
401399
dplyr::inner_join(
402400
lookups[["diagnoses"]],
403401
by = c("diagnosis" = "diagnosis_code")
@@ -472,30 +470,26 @@ mod_mitigators_server <- function(
472470

473471
# render the procedures table
474472
output$procedures_table <- gt::render_gt({
473+
data <- procedures_data()
474+
475475
shiny::validate(
476476
shiny::need(
477-
procedures_data,
477+
data,
478478
message = "Insufficient or suppressed data."
479479
)
480480
)
481481

482-
pd <- procedures_data
483-
484482
shiny::validate(
485483
shiny::need(
486-
!is.null(pd) && nrow(pd) > 0,
484+
!is.null(data) && nrow(data) > 0,
487485
"No procedures to display"
488486
)
489487
)
490488

491489
strategy <- shiny::req(input$strategy)
492490

493-
data <- pd |>
494-
dplyr::filter(
495-
.data$provider == params$dataset,
496-
.data$strategy == .env$strategy,
497-
.data$fyear == params$start_year
498-
) |>
491+
data <- data |>
492+
dplyr::filter(.data$strategy == .env$strategy) |>
499493
dplyr::left_join(
500494
lookups[["procedures"]],
501495
by = c("procedure_code" = "code")
@@ -574,12 +568,8 @@ mod_mitigators_server <- function(
574568
# render the age group pyramid plot
575569
output$age_grp_plot <- shiny::renderPlot({
576570
strategy <- shiny::req(input$strategy)
577-
age_data <- age_sex_data |>
578-
dplyr::filter(
579-
.data$provider == params$dataset,
580-
.data$strategy == .env$strategy,
581-
.data$fyear == params$start_year
582-
)
571+
age_data <- age_sex_data() |>
572+
dplyr::filter(.data$strategy == .env$strategy)
583573

584574
shiny::req(nrow(age_data) > 0)
585575
age_pyramid(age_data)

0 commit comments

Comments
 (0)