Skip to content

Commit 83151d0

Browse files
authored
Merge pull request #45 from The-Strategy-Unit/33-config
Use NHP-inputs config for dynamic axes labelling
2 parents 23f84f9 + cc439e8 commit 83151d0

24 files changed

+531
-76
lines changed

.Renviron.example

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
# To run app locally and in deployment
12
AZ_CONTAINER_INPUTS=
23
AZ_STORAGE_EP=
4+
AZ_SUPPORT_CONTAINER=
5+
BASELINE_YEAR=
36
DATA_VERSION=
4-
START_YEAR=
7+
# To run app locally
8+
AZ_APP_ID=
9+
AZ_TENANT_ID=

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ export(entable_encounters)
55
export(fetch_strategy_text)
66
export(generate_rates_baseline_data)
77
export(generate_rates_funnel_data)
8+
export(get_golem_config)
89
export(isolate_provider_peers)
10+
export(make_strategy_group_lookup)
911
export(plot_age_sex_pyramid)
1012
export(plot_nee)
1113
export(plot_rates_box)

R/app_server.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ app_server <- function(input, output, session) {
55
# Env variables ----
66
inputs_container_name <- Sys.getenv("AZ_CONTAINER_INPUTS")
77
data_version <- Sys.getenv("DATA_VERSION")
8-
start_year <- Sys.getenv("START_YEAR") |> as.numeric()
8+
baseline_year <- Sys.getenv("BASELINE_YEAR") |> as.numeric()
99

1010
# Data ----
11-
inputs_container <- azkit::get_container(inputs_container_name)
11+
inputs_container <- get_container(container_name = inputs_container_name)
1212
rates_data <- azkit::read_azure_parquet(
1313
inputs_container,
1414
"rates",
@@ -61,6 +61,9 @@ app_server <- function(input, output, session) {
6161
col_types = "c"
6262
)
6363

64+
# Config ----
65+
strategies_config <- get_golem_config("mitigators_config")
66+
6467
# User inputs ----
6568
selected_provider <- mod_select_provider_server(
6669
"mod_select_provider",
@@ -80,32 +83,34 @@ app_server <- function(input, output, session) {
8083
mod_plot_rates_server(
8184
"mod_plot_rates",
8285
rates_data,
86+
strategies_config,
8387
peers_lookup,
8488
selected_provider,
85-
selected_strategy
89+
selected_strategy,
90+
baseline_year
8691
)
8792
mod_table_procedures_server(
8893
"mod_table_procedures",
8994
procedures_data,
9095
procedures_lookup,
9196
selected_provider,
9297
selected_strategy,
93-
start_year
98+
baseline_year
9499
)
95100
mod_table_diagnoses_server(
96101
"mod_table_diagnoses",
97102
diagnoses_data,
98103
diagnoses_lookup,
99104
selected_provider,
100105
selected_strategy,
101-
start_year
106+
baseline_year
102107
)
103108
mod_plot_age_sex_pyramid_server(
104109
"mod_plot_age_sex_pyramid",
105110
age_sex_data,
106111
selected_provider,
107112
selected_strategy,
108-
start_year
113+
baseline_year
109114
)
110115
mod_plot_nee_server(
111116
"mod_plot_nee",

R/fct_azure.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
get_container <- function(
2+
tenant = Sys.getenv("AZ_TENANT_ID"),
3+
app_id = Sys.getenv("AZ_APP_ID"),
4+
ep_uri = Sys.getenv("AZ_STORAGE_EP"),
5+
container_name = Sys.getenv("AZ_CONTAINER_INPUTS")
6+
) {
7+
# if the app_id variable is empty, we assume that this is running on an Azure
8+
# VM, and then we will use Managed Identities for authentication.
9+
token <- if (app_id != "") {
10+
AzureAuth::get_azure_token(
11+
resource = "https://storage.azure.com",
12+
tenant = tenant,
13+
app = app_id,
14+
auth_type = "device_code",
15+
use_cache = TRUE # avoid browser-authorisation prompt
16+
)
17+
} else {
18+
AzureAuth::get_managed_token("https://storage.azure.com/")
19+
}
20+
ep_uri |>
21+
AzureStor::blob_endpoint(token = token) |>
22+
AzureStor::storage_container(container_name)
23+
}

R/fct_plots.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#' Plot Rates Trend Over Time
22
#' @param rates_trend_data A data.frame. Rates data read in from Azure, filtered
33
#' for a given provider and strategy, and arranged by year.
4-
#' @param baseline_year Numeric. In the form `202324`.
4+
#' @param baseline_year Integer. Baseline year in the form `202324`.
5+
#' @param y_axis_limits Numeric vector. Min and max values for the y axis.
56
#' @param x_axis_title Character. Title for the x-axis.
67
#' @param y_axis_title Character. Title for the y-axis.
7-
#' @param y_axis_limits Numeric vector. Min and max values for the y axis.
88
#' @return A 'ggplot2' object.
99
#' @export
1010
plot_rates_trend <- function(
1111
rates_trend_data,
12-
baseline_year = 202324,
12+
baseline_year,
13+
y_axis_limits,
1314
x_axis_title = "Financial year",
14-
y_axis_title = "Rate",
15-
y_axis_limits
15+
y_axis_title
1616
) {
1717
rates_trend_data |>
1818
ggplot2::ggplot(

R/mod_plot_age_sex_pyramid.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod_plot_age_sex_pyramid_ui <- function(id) {
1818
#' @param selected_provider Character. Provider code, e.g. `"RCF"`.
1919
#' @param selected_strategy Character. Strategy variable name, e.g.
2020
#' `"alcohol_partially_attributable_acute"`.
21+
#' @param baseline_year Integer. Baseline year in the form `202324`.
2122
#' @noRd
2223
# nolint start: object_length_linter.
2324
mod_plot_age_sex_pyramid_server <- function(
@@ -26,20 +27,20 @@ mod_plot_age_sex_pyramid_server <- function(
2627
age_sex_data,
2728
selected_provider,
2829
selected_strategy,
29-
start_year
30+
baseline_year
3031
) {
3132
shiny::moduleServer(id, function(input, output, session) {
3233
output$age_sex_pyramid <- shiny::renderPlot({
3334
shiny::req(age_sex_data)
3435
shiny::req(selected_provider())
3536
shiny::req(selected_strategy())
36-
shiny::req(start_year)
37+
shiny::req(baseline_year)
3738

3839
age_sex_filtered <- age_sex_data |>
3940
dplyr::filter(
4041
.data$provider == selected_provider(),
4142
.data$strategy == selected_strategy(),
42-
.data$fyear == .env$start_year
43+
.data$fyear == .env$baseline_year
4344
)
4445

4546
shiny::validate(shiny::need(

R/mod_plot_rates.R

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' @noRd
44
mod_plot_rates_ui <- function(id) {
55
ns <- shiny::NS(id)
6-
# these plots share a y axis, so don't use layout_column_wrap()
6+
# Rates plots share a y-axis, so don't wrap
77
bslib::layout_columns(
88
col_widths = c(5, 5, 2),
99
fill = FALSE,
@@ -18,19 +18,27 @@ mod_plot_rates_ui <- function(id) {
1818
#' @param id Internal parameter for `shiny`.
1919
#' @param rates A data.frame. Annual rate values for combinations of provider
2020
#' and strategy.
21+
#' @param strategies_config List. Configuration for strategies from the
22+
#' `"mitigators_config"` element of `golem-config.yml`, read in with
23+
#' [get_golem_config].
2124
#' @param peers_lookup A data.frame. A row per provider-peer pair.
2225
#' @param selected_provider Character. Provider code, e.g. `"RCF"`.
2326
#' @param selected_strategy Character. Strategy variable name, e.g.
2427
#' `"alcohol_partially_attributable_acute"`.
28+
#' @param baseline_year Integer. Baseline year in the form `202324`.
2529
#' @noRd
2630
mod_plot_rates_server <- function(
2731
id,
2832
rates,
33+
strategies_config,
2934
peers_lookup,
3035
selected_provider,
31-
selected_strategy
36+
selected_strategy,
37+
baseline_year
3238
) {
3339
shiny::moduleServer(id, function(input, output, session) {
40+
# Prepare data ----
41+
3442
rates_trend_data <- shiny::reactive({
3543
shiny::req(rates)
3644
shiny::req(selected_provider())
@@ -59,7 +67,7 @@ mod_plot_rates_server <- function(
5967
selected_provider(),
6068
provider_peers,
6169
selected_strategy(),
62-
start_year = "202324"
70+
baseline_year
6371
)
6472
})
6573

@@ -68,6 +76,8 @@ mod_plot_rates_server <- function(
6876
rates_baseline_data() |> generate_rates_funnel_data()
6977
})
7078

79+
# Prepare variables ----
80+
7181
y_axis_limits <- shiny::reactive({
7282
shiny::req(rates_trend_data())
7383
shiny::req(rates_funnel_data())
@@ -80,27 +90,47 @@ mod_plot_rates_server <- function(
8090
pmax(0)
8191
})
8292

93+
strategy_config <- shiny::reactive({
94+
shiny::req(strategies_config)
95+
shiny::req(selected_strategy())
96+
97+
strategy_group_lookup <- strategies_config |> make_strategy_group_lookup()
98+
99+
strategy_group <- strategy_group_lookup |>
100+
dplyr::filter(.data$strategy == selected_strategy()) |>
101+
dplyr::pull("group")
102+
103+
strategies_config[[strategy_group]]
104+
})
105+
106+
y_axis_title <- shiny::reactive({
107+
shiny::req(strategy_config())
108+
strategy_config()[["y_axis_title"]]
109+
})
110+
funnel_x_title <- shiny::reactive({
111+
shiny::req(strategy_config())
112+
strategy_config()[["funnel_x_title"]]
113+
})
114+
115+
# Declare modules ----
83116
mod_plot_rates_trend_server(
84117
"mod_plot_rates_trend",
85118
rates_trend_data,
86-
selected_provider,
87-
selected_strategy,
88-
y_axis_limits
119+
y_axis_limits,
120+
y_axis_title,
121+
baseline_year
89122
)
90123
mod_plot_rates_funnel_server(
91124
"mod_plot_rates_funnel",
92125
rates_funnel_data,
93126
peers_lookup,
94-
selected_provider,
95-
selected_strategy,
96-
y_axis_limits
127+
y_axis_limits,
128+
funnel_x_title
97129
)
98130
mod_plot_rates_box_server(
99131
"mod_plot_rates_box",
100132
rates_baseline_data,
101133
peers_lookup,
102-
selected_provider,
103-
selected_strategy,
104134
y_axis_limits
105135
)
106136
})

R/mod_plot_rates_box.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ mod_plot_rates_box_ui <- function(id) {
1515
#' @param rates A data.frame. Annual rate values for combinations of provider
1616
#' and strategy.
1717
#' @param peers_lookup A data.frame. A row per provider-peer pair.
18-
#' @param selected_provider Character. Provider code, e.g. `"RCF"`.
19-
#' @param selected_strategy Character. Strategy variable name, e.g.
20-
#' `"alcohol_partially_attributable_acute"`.
2118
#' @param y_axis_limits Numeric vector. Min and max values for the y axis.
2219
#' @noRd
2320
mod_plot_rates_box_server <- function(
2421
id,
2522
rates,
2623
peers_lookup,
27-
selected_provider,
28-
selected_strategy,
2924
y_axis_limits
3025
) {
3126
shiny::moduleServer(id, function(input, output, session) {

R/mod_plot_rates_funnel.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@ mod_plot_rates_funnel_ui <- function(id) {
1515
#' @param rates A data.frame. Annual rate values for combinations of provider
1616
#' and strategy
1717
#' @param peers_lookup A data.frame. A row per provider-peer pair.
18-
#' @param selected_provider Character. Provider code, e.g. `"RCF"`.
19-
#' @param selected_strategy Character. Strategy variable name, e.g.
20-
#' `"alcohol_partially_attributable_acute"`.
2118
#' @param y_axis_limits Numeric vector. Min and max values for the y axis.
19+
#' @param x_axis_title Character. Title for the x-axis.
2220
#' @noRd
2321
mod_plot_rates_funnel_server <- function(
2422
id,
2523
rates,
2624
peers_lookup,
27-
selected_provider,
28-
selected_strategy,
29-
y_axis_limits
25+
y_axis_limits,
26+
x_axis_title
3027
) {
3128
shiny::moduleServer(id, function(input, output, session) {
3229
output$rates_funnel_plot <- shiny::renderPlot({
@@ -35,10 +32,13 @@ mod_plot_rates_funnel_server <- function(
3532
nrow(rates) > 0,
3633
"No data available for these selections."
3734
))
35+
shiny::req(y_axis_limits())
36+
shiny::req(x_axis_title())
37+
3838
plot_rates_funnel(
3939
rates,
4040
y_axis_limits(),
41-
x_axis_title = "Denominator" # TODO: make this dynamic
41+
x_axis_title = x_axis_title()
4242
)
4343
})
4444
})

R/mod_plot_rates_trend.R

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@ mod_plot_rates_trend_ui <- function(id) {
1414
#' @param id Internal parameter for `shiny`.
1515
#' @param rates A data.frame. Annual rate values for combinations of provider
1616
#' and strategy.
17-
#' @param selected_provider Character. Provider code, e.g. `"RCF"`.
18-
#' @param selected_strategy Character. Strategy variable name, e.g.
19-
#' `"alcohol_partially_attributable_acute"`.
2017
#' @param y_axis_limits Numeric vector. Min and max values for the y axis.
18+
#' @param y_axis_title Character. Title for the y-axis.
19+
#' @param baseline_year Integer. Baseline year in the form `202324`.
2120
#' @noRd
2221
mod_plot_rates_trend_server <- function(
2322
id,
2423
rates,
25-
selected_provider,
26-
selected_strategy,
27-
y_axis_limits
24+
y_axis_limits,
25+
y_axis_title,
26+
baseline_year
2827
) {
2928
shiny::moduleServer(id, function(input, output, session) {
3029
output$rates_trend_plot <- shiny::renderPlot({
@@ -33,7 +32,15 @@ mod_plot_rates_trend_server <- function(
3332
nrow(rates) > 0,
3433
"No data available for these selections."
3534
))
36-
plot_rates_trend(rates, y_axis_limits = y_axis_limits())
35+
shiny::req(y_axis_limits())
36+
shiny::req(y_axis_title())
37+
38+
plot_rates_trend(
39+
rates,
40+
baseline_year,
41+
y_axis_limits(),
42+
y_axis_title = y_axis_title()
43+
)
3744
})
3845
})
3946
}

0 commit comments

Comments
 (0)