55# ' @import shiny
66# ' @noRd
77app_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}
0 commit comments