Skip to content

Commit 5842192

Browse files
authored
Merge pull request #624 from The-Strategy-Unit/refactor_mitigators_server
2 parents 7b98510 + cd3cfe5 commit 5842192

38 files changed

Lines changed: 638 additions & 567 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ inputs_selection_app/
1818

1919
.cache/
2020
tmp/
21+
22+
app_data/

R/ZZZ.R

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,13 @@ utils::globalVariables(c(
99
".env"
1010
))
1111

12-
rtt_specialties <- function() {
13-
app_sys("app", "data", "rtt_specialties.csv") |>
14-
readr::read_csv(col_types = "cc")
15-
}
16-
1712
sanitize_input_name <- function(.x) {
1813
.x |>
1914
stringr::str_to_lower() |>
2015
stringr::str_replace_all("(\\s|\\_|-)+", "-") |>
2116
stringr::str_remove_all("[^a-z0-9-]+")
2217
}
2318

24-
# suppress vs code / languageserver "no visible binding" warnings
25-
if (FALSE) {
26-
.data <- NULL
27-
.env <- NULL
28-
}
29-
3019
md_file_to_html <- function(...) {
3120
file <- app_sys(...)
3221

@@ -77,38 +66,30 @@ is_local <- function() {
7766
Sys.getenv("SHINY_PORT") == "" || !getOption("golem.app.prod", TRUE)
7867
}
7968

80-
encrypt_filename <- function(
81-
filename,
82-
key_b64 = Sys.getenv("NHP_ENCRYPT_KEY")
83-
) {
84-
key <- openssl::base64_decode(key_b64)
85-
86-
f <- charToRaw(filename)
87-
88-
ct <- openssl::aes_cbc_encrypt(f, key, NULL)
89-
hm <- as.raw(openssl::sha256(ct, key))
90-
91-
openssl::base64_encode(c(hm, ct))
92-
}
93-
94-
get_params_schema_text <- function(
69+
download_params_schema <- function(
70+
data_path = "app_data",
9571
app_version = Sys.getenv("INPUTS_DATA_VERSION", "dev")
9672
) {
97-
tf <- tempfile()
73+
file_path <- file.path(data_path, "params-schema.json")
9874

99-
utils::download.file(
100-
glue::glue(
101-
"https://the-strategy-unit.github.io/nhp_model/{app_version}/params-schema.json"
102-
),
103-
tf
104-
)
105-
# append a newline to the end of the params-schema file, otherwise you get a warning
106-
# "incomplete final line found"
107-
cat("\n", file = tf, append = TRUE)
75+
params_schema <- httr2::request(
76+
"https://the-strategy-unit.github.io"
77+
) |>
78+
httr2::req_url_path("nhp_model", app_version, "params-schema.json") |>
79+
httr2::req_perform() |>
80+
httr2::resp_body_string()
81+
82+
readr::write_lines(params_schema, file_path)
10883

109-
paste(readLines(tf), collapse = "\n")
84+
invisible(file_path)
11085
}
11186

112-
create_params_schema <- function(schema_text) {
113-
jsonvalidate::json_schema$new(schema_text)
87+
.schema_cache <- new.env()
88+
get_params_schema <- function() {
89+
if (!exists("schema", envir = .schema_cache)) {
90+
.schema_cache[["schema"]] <- file.path("app_data", "params-schema.json") |>
91+
readr::read_file() |>
92+
jsonvalidate::json_schema$new()
93+
}
94+
.schema_cache[["schema"]]
11495
}

R/app_server.R

Lines changed: 30 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -5,140 +5,40 @@
55
#' @import shiny
66
#' @noRd
77
app_server <- function(input, output, session) {
8-
# in fct_create_data_cache, we utilise this env var to invalidate the cache
9-
# we can use it's value to allow us to cache all of the reactive data without
10-
# having to bind to some other input which might change
11-
cache_version <- shiny::reactive({
12-
Sys.getenv("CACHE_VERSION", 0)
13-
})
14-
15-
diagnoses_lkup <- shiny::reactive({
16-
readr::read_csv(app_sys("app", "data", "diagnoses.csv"), col_types = "ccc")
17-
})
18-
19-
procedures_lkup <- shiny::reactive({
20-
readr::read_csv(app_sys("app", "data", "procedures.csv"), col_types = "ccc")
21-
})
22-
23-
mitigator_codes_lkup <- shiny::reactive({
24-
lkup <- app_sys("app", "data", "mitigator-codes.csv") |>
25-
readr::read_csv(col_types = "c")
26-
27-
purrr::set_names(
28-
paste0(lkup[["strategy_name"]], " (", lkup[["mitigator_code"]], ")"),
29-
lkup[["strategy"]]
30-
)
31-
})
32-
33-
providers <- shiny::reactive({
34-
app_sys("app", "data", "providers.csv") |>
35-
readr::read_csv(col_types = "cc") |>
36-
tibble::deframe() # convert tibble to named vector
37-
})
38-
39-
peers <- shiny::reactive({
40-
readr::read_csv(app_sys("app", "data", "peers.csv"), col_types = "cc")
41-
})
42-
438
params <- mod_home_server(
449
"home",
45-
providers(),
4610
shiny::reactive(input$params_file)
4711
)
4812

49-
# we could probably drop the need for start now, kept for historical reasons
50-
start <- shiny::reactive({
51-
shiny::req(length(params) > 0)
52-
shiny::req(params$dataset)
53-
shiny::req(params$scenario)
54-
1
55-
})
56-
5713
# load all data
5814
rates_data <- shiny::reactive({
59-
rates <- load_provider_data("rates") |>
60-
dplyr::select(-"crude_rate") |>
61-
dplyr::rename(rate = "std_rate")
62-
63-
national_rate <- rates |>
64-
dplyr::filter(
65-
.data$provider == "national"
66-
) |>
67-
dplyr::summarise(
68-
.by = c("fyear", "strategy"),
69-
national_rate = dplyr::first(.data$rate)
70-
)
71-
72-
rates |>
73-
dplyr::filter(.data$provider != "national") |>
74-
dplyr::inner_join(national_rate, by = c("fyear", "strategy"))
75-
}) |>
76-
shiny::bindCache(cache_version())
15+
get_rates_data()
16+
})
7717

7818
age_sex_data <- shiny::reactive({
79-
age_sex <- load_provider_data("age_sex")
80-
# nolint start: object_usage_linter
81-
age_fct <- age_sex |> _[["age_group"]] |> unique() |> sort()
82-
# nolint end
83-
age_sex |>
84-
dplyr::mutate(
85-
dplyr::across("sex", as.character),
86-
age_group = factor(
87-
.data[["age_group"]],
88-
levels = .env[["age_fct"]]
89-
)
90-
)
91-
}) |>
92-
shiny::bindCache(cache_version())
93-
94-
diagnoses_data <- shiny::reactive({
95-
load_provider_data("diagnoses")
96-
}) |>
97-
shiny::bindCache(cache_version())
19+
provider <- params$dataset
20+
fyear <- params$start_year
9821

99-
procedures_data <- shiny::reactive({
100-
load_provider_data("procedures")
101-
}) |>
102-
shiny::bindCache(cache_version())
103-
104-
baseline_data <- shiny::reactive({
105-
load_provider_data("baseline")
106-
}) |>
107-
shiny::bindCache(cache_version())
108-
109-
wli_data <- shiny::reactive({
110-
load_provider_data("wli")
111-
}) |>
112-
shiny::bindCache(cache_version())
113-
114-
inequalities_data <- shiny::reactive({
115-
load_provider_data("inequalities")
116-
}) |>
117-
shiny::bindCache(cache_version())
22+
get_age_sex_data(provider, fyear)
23+
})
11824

119-
expat_data <- shiny::reactive({
120-
load_provider_data("expat")
121-
}) |>
122-
shiny::bindCache(cache_version())
25+
diagnoses_data <- shiny::reactive({
26+
provider <- params$dataset
27+
fyear <- params$start_year
12328

124-
repat_local_data <- shiny::reactive({
125-
load_provider_data("repat_local")
126-
}) |>
127-
shiny::bindCache(cache_version())
29+
get_diagnoses_data(provider, fyear)
30+
})
12831

129-
repat_nonlocal_data <- shiny::reactive({
130-
load_provider_data("repat_nonlocal")
131-
}) |>
132-
shiny::bindCache(cache_version())
32+
procedures_data <- shiny::reactive({
33+
provider <- params$dataset
34+
fyear <- params$start_year
13335

134-
params_schema_text <- shiny::reactive({
135-
get_params_schema_text()
136-
}) |>
137-
shiny::bindCache(cache_version())
36+
get_procedures_data(provider, fyear)
37+
})
13838

13939
# load all other modules once the home module has finished loading
14040
init <- shiny::observe({
141-
shiny::req(start() > 0)
41+
shiny::req(params$dataset)
14242

14343
available_strategies <- shiny::reactive({
14444
# nolint start: object_usage_linter
@@ -159,17 +59,13 @@ app_server <- function(input, output, session) {
15959
unique()
16060
})
16161

162-
mod_baseline_adjustment_server(
163-
"baseline_adjustment",
164-
baseline_data(),
165-
params
166-
)
62+
mod_baseline_adjustment_server("baseline_adjustment", params)
16763

16864
mod_population_growth_server("population_growth", params)
16965

17066
mod_health_status_adjustment_server("health_status_adjustment", params)
17167

172-
observe({
68+
shiny::observe({
17369
can_set_inequalities <- any(
17470
c("nhp_devs", "nhp_power_users", "nhp_test_inequalities") %in%
17571
session$groups
@@ -181,26 +77,13 @@ app_server <- function(input, output, session) {
18177
)
18278
})
18379

184-
mod_inequalities_server("inequalities", inequalities_data(), params)
185-
186-
mod_waiting_list_imbalances_server(
187-
"waiting_list_imbalances",
188-
wli_data(),
189-
params
190-
)
80+
mod_inequalities_server("inequalities", params)
19181

192-
mod_expat_repat_server(
193-
"expat_repat",
194-
expat_data(),
195-
repat_local_data(),
196-
repat_nonlocal_data(),
197-
params,
198-
providers()
199-
)
82+
mod_expat_repat_server("expat_repat", params)
20083

20184
mod_non_demographic_adjustment_server("non_demographic_adjustment", params)
20285

203-
mod_mitigators_summary_server("mitigators_summary", age_sex_data(), params)
86+
mod_mitigators_summary_server("mitigators_summary", age_sex_data, params)
20487

20588
purrr::walk(
20689
c(
@@ -221,15 +104,11 @@ app_server <- function(input, output, session) {
221104
),
222105
mod_mitigators_server,
223106
params,
224-
rates_data(),
225-
age_sex_data(),
226-
diagnoses_data(),
227-
procedures_data(),
228-
available_strategies,
229-
diagnoses_lkup(),
230-
procedures_lkup(),
231-
mitigator_codes_lkup(),
232-
peers()
107+
rates_data,
108+
age_sex_data,
109+
diagnoses_data,
110+
procedures_data,
111+
available_strategies
233112
)
234113

235114
# enable the run_model page for certain users/running locally
@@ -239,15 +118,13 @@ app_server <- function(input, output, session) {
239118
)
240119
if (is_local() || can_run_model) {
241120
shinyjs::show("run-model-container")
242-
mod_run_model_server("run_model", params, params_schema_text())
121+
mod_run_model_server("run_model", params)
243122
}
244123

245124
init$destroy()
246-
}) |>
247-
shiny::bindEvent(start())
125+
})
248126

249127
shiny::observe({
250-
shiny::req(start() > 0)
251128
shiny::req(params$dataset)
252129
shiny::req(params$scenario)
253130

@@ -260,28 +137,10 @@ app_server <- function(input, output, session) {
260137

261138
params |>
262139
shiny::reactiveValuesToList() |>
263-
mod_run_model_fix_params(params_schema_text()) |>
140+
mod_run_model_fix_params() |>
264141
jsonlite::write_json(file, pretty = TRUE, auto_unbox = TRUE)
265142
})
266143

267-
if (as.logical(Sys.getenv("ENABLE_AUTO_RECONNECT", FALSE))) {
268-
cat("auto reconnect enabled\n")
269-
session$allowReconnect("force")
270-
}
271-
272-
shiny::observe({
273-
shiny::req("nhp_devs" %in% session$groups)
274-
275-
u <- shiny::parseQueryString(session$clientData$url_search)
276-
277-
shiny::req(!is.null(u$reset_cache))
278-
cat("reset cache\n")
279-
280-
dc <- shiny::shinyOptions()$cache
281-
282-
dc$reset()
283-
})
284-
285144
# return
286145
NULL
287146
}

R/app_ui.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,6 @@ app_ui <- function(request) {
201201
tabName = "tab_nda",
202202
mod_non_demographic_adjustment_ui("non_demographic_adjustment")
203203
),
204-
bs4Dash::tabItem(
205-
tabName = "tab_wli",
206-
mod_waiting_list_imbalances_ui("waiting_list_imbalances")
207-
),
208204
bs4Dash::tabItem(
209205
tabName = "tab_er",
210206
mod_expat_repat_ui("expat_repat")

0 commit comments

Comments
 (0)