Skip to content

Commit 8fca07c

Browse files
authored
Merge pull request #73 from The-Strategy-Unit/use-new-rates-data-format
use new rates data format
2 parents 670a62d + 4ddce39 commit 8fca07c

File tree

10 files changed

+90
-48
lines changed

10 files changed

+90
-48
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,5 @@ poetry.toml
253253
pyrightconfig.json
254254

255255
/.quarto/
256+
257+
.cache/

R/app_server.R

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
#' @param input,output,session Internal parameters for 'shiny'.
33
#' @noRd
44
app_server <- function(input, output, session) {
5+
cache <- cachem::cache_disk(".cache")
6+
inputs_data_fn <- memoise::memoise(
7+
get_all_geo_data,
8+
cache = cache
9+
)
10+
511
# Variables ----
612
geographies <- c(
713
"New Hospital Programme (NHP) schemes" = "nhp",
@@ -11,26 +17,40 @@ app_server <- function(input, output, session) {
1117
baseline_year <- Sys.getenv("BASELINE_YEAR") |> as.numeric()
1218

1319
# Data ----
14-
inputs_container <- get_container(
15-
container_name = Sys.getenv("AZ_CONTAINER_INPUTS")
16-
)
17-
inputs_data <- get_all_geo_data(inputs_container, geographies, data_types)
20+
inputs_data <- inputs_data_fn(geographies, data_types)
1821
age_sex_data <- shiny::reactive({
19-
shiny::req(selected_geography())
20-
inputs_data[[selected_geography()]][["age_sex"]] |>
22+
sg <- shiny::req(selected_geography())
23+
inputs_data[[sg]][["age_sex"]] |>
2124
prepare_age_sex_data()
2225
})
2326
diagnoses_data <- shiny::reactive({
24-
shiny::req(selected_geography())
25-
inputs_data[[selected_geography()]][["diagnoses"]]
27+
sg <- shiny::req(selected_geography())
28+
inputs_data[[sg]][["diagnoses"]]
2629
})
2730
procedures_data <- shiny::reactive({
28-
shiny::req(selected_geography())
29-
inputs_data[[selected_geography()]][["procedures"]]
31+
sg <- shiny::req(selected_geography())
32+
inputs_data[[sg]][["procedures"]]
3033
})
3134
rates_data <- shiny::reactive({
32-
shiny::req(selected_geography())
33-
inputs_data[[selected_geography()]][["rates"]]
35+
sg <- shiny::req(selected_geography())
36+
df <- inputs_data[[sg]][["rates"]]
37+
38+
national <- df |>
39+
dplyr::filter(.data$provider == "national") |>
40+
dplyr::select(
41+
"strategy",
42+
"fyear",
43+
national_rate = "std_rate"
44+
)
45+
46+
df |>
47+
dplyr::filter(!.data$provider %in% c("national", "unknown")) |>
48+
dplyr::inner_join(
49+
national,
50+
by = c("strategy", "fyear")
51+
) |>
52+
dplyr::rename(rate = "std_rate") |>
53+
dplyr::select(-"crude_rate")
3454
})
3555
nee_data <- readr::read_csv(
3656
app_sys("app", "data", "nee_table.csv"),
@@ -118,7 +138,8 @@ app_server <- function(input, output, session) {
118138
mod_show_strategy_text_server(
119139
"mod_show_strategy_text",
120140
descriptions_lookup,
121-
selected_strategy
141+
selected_strategy,
142+
cache
122143
)
123144
mod_plot_rates_server(
124145
"mod_plot_rates",

R/fct_azure.R

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77
#' @return A blob_container/storage_container list.
88
#' @export
99
get_container <- function(
10-
tenant = Sys.getenv("AZ_TENANT_ID"),
11-
app_id = Sys.getenv("AZ_APP_ID"),
1210
ep_uri = Sys.getenv("AZ_STORAGE_EP"),
1311
container_name = Sys.getenv("AZ_CONTAINER_INPUTS")
1412
) {
1513
# if the app_id variable is empty, we assume that this is running on an Azure
1614
# VM, and then we will use Managed Identities for authentication.
17-
token <- if (app_id != "") {
18-
AzureAuth::get_azure_token(
19-
resource = "https://storage.azure.com",
20-
tenant = tenant,
21-
app = app_id,
22-
auth_type = "device_code",
23-
use_cache = TRUE # avoid browser-authorisation prompt
24-
)
25-
} else {
26-
AzureAuth::get_managed_token("https://storage.azure.com/")
27-
}
15+
16+
token <- tryCatch(
17+
{
18+
AzureAuth::get_managed_token("https://storage.azure.com/")
19+
},
20+
error = function(...) {
21+
AzureAuth::get_azure_token(
22+
resource = "https://storage.azure.com",
23+
tenant = "common",
24+
app = "04b07795-8ddb-461a-bbee-02f9e1bf7b46",
25+
use_cache = TRUE # avoid browser-authorisation prompt
26+
)
27+
}
28+
)
2829
ep_uri |>
2930
AzureStor::blob_endpoint(token = token) |>
3031
AzureStor::storage_container(container_name)

R/fct_plots.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
#' @param y_axis_limits Numeric vector. Min and max values for the y axis.
66
#' @param x_axis_title Character. Title for the x-axis.
77
#' @param y_axis_title Character. Title for the y-axis.
8+
#' @param y_labels A function. Function to format y-axis labels.
89
#' @return A 'ggplot2' object.
910
#' @export
1011
plot_rates_trend <- function(
1112
rates_trend_data,
1213
baseline_year,
1314
y_axis_limits,
1415
x_axis_title = "Financial year",
15-
y_axis_title
16+
y_axis_title,
17+
y_labels
1618
) {
1719
rates_trend_data |>
1820
ggplot2::ggplot(
@@ -23,8 +25,7 @@ plot_rates_trend <- function(
2325
data = \(.x) dplyr::filter(.x, .data[["fyear"]] == baseline_year),
2426
colour = "red"
2527
) +
26-
# TODO: add labels = number_format to scale_y_continuous
27-
ggplot2::scale_y_continuous(name = y_axis_title) +
28+
ggplot2::scale_y_continuous(name = y_axis_title, labels = y_labels) +
2829
ggplot2::coord_cartesian(ylim = y_axis_limits) +
2930
ggplot2::scale_x_discrete(
3031
labels = \(.x) stringr::str_replace(.x, "^(\\d{4})(\\d{2})$", "\\1/\\2")

R/mod_plot_rates.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ mod_plot_rates_server <- function(
107107
shiny::req(strategy_config())
108108
strategy_config()[["y_axis_title"]]
109109
})
110+
y_labels <- shiny::reactive({
111+
shiny::req(strategy_config())
112+
strategy_config()[["number_type"]]
113+
})
110114
funnel_x_title <- shiny::reactive({
111115
shiny::req(strategy_config())
112116
strategy_config()[["funnel_x_title"]]
@@ -118,6 +122,7 @@ mod_plot_rates_server <- function(
118122
rates_trend_data,
119123
y_axis_limits,
120124
y_axis_title,
125+
y_labels,
121126
baseline_year
122127
)
123128
mod_plot_rates_funnel_server(

R/mod_plot_rates_trend.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ mod_plot_rates_trend_ui <- function(id) {
2020
#' and strategy.
2121
#' @param y_axis_limits Numeric vector. Min and max values for the y axis.
2222
#' @param y_axis_title Character. Title for the y-axis.
23+
#' @param y_labels Function. Function to format y-axis labels.
2324
#' @param baseline_year Integer. Baseline year in the form `202324`.
2425
#' @noRd
2526
mod_plot_rates_trend_server <- function(
2627
id,
2728
rates,
2829
y_axis_limits,
2930
y_axis_title,
31+
y_labels,
3032
baseline_year
3133
) {
3234
shiny::moduleServer(id, function(input, output, session) {
@@ -42,7 +44,8 @@ mod_plot_rates_trend_server <- function(
4244
rates(),
4345
baseline_year,
4446
y_axis_limits(),
45-
y_axis_title = y_axis_title()
47+
y_axis_title = y_axis_title(),
48+
y_labels = y_labels()
4649
)
4750
})
4851
})

R/mod_show_strategy_text.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ mod_show_strategy_text_ui <- function(id) {
1919
mod_show_strategy_text_server <- function(
2020
id,
2121
descriptions_lookup,
22-
selected_strategy
22+
selected_strategy,
23+
cache
2324
) {
2425
shiny::moduleServer(id, function(input, output, session) {
2526
output$strategy_text <- shiny::renderText({
@@ -28,6 +29,7 @@ mod_show_strategy_text_server <- function(
2829
selected_strategy() |>
2930
fetch_strategy_text(descriptions_lookup) |>
3031
convert_md_to_html()
31-
})
32+
}) |>
33+
shiny::bindCache(selected_strategy(), cache = cache)
3234
})
3335
}

R/utils_server.R

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#' Read Inputs Datasets for All Geographies
2-
#' @param inputs_container A blob_container/storage_container list. The
3-
#' object containing the connection to the Azure container hosting the
4-
#' datasets named by `data_types`.
52
#' @param geographies Character. The geography level for which the user wants to
63
#' select a provider.
74
#' @param data_types Character. A vector of filenames (without filetypes) for
@@ -10,19 +7,33 @@
107
#' @return A list. One element for each of the `geographies`, with subelements
118
#' that are dataframes of each one of the `data_types`.
129
#' @export
13-
get_all_geo_data <- function(inputs_container, geographies, data_types) {
10+
get_all_geo_data <- function(geographies, data_types) {
11+
inputs_container <- get_container(
12+
container_name = Sys.getenv("AZ_CONTAINER_INPUTS")
13+
)
14+
1415
purrr::map(
1516
geographies,
1617
\(geography) {
1718
purrr::map(
1819
data_types,
1920
\(data_type) {
20-
container_dir <- if (geography == "la") {
21-
"local_authorities"
22-
} else {
23-
Sys.getenv("DATA_VERSION")
24-
}
25-
col_renames <- c(provider = "resladst_ons")
21+
geography_folder <- switch(
22+
geography,
23+
"nhp" = "provider",
24+
"la" = "lad23cd"
25+
)
26+
27+
stopifnot(
28+
"Unknown geography" = !is.null(geography_folder)
29+
)
30+
31+
container_dir <- file.path(
32+
Sys.getenv("DATA_VERSION"),
33+
geography_folder
34+
)
35+
36+
col_renames <- c(provider = "lad23cd")
2637
azkit::read_azure_parquet(
2738
inputs_container,
2839
data_type,

inst/app/data/mitigators.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
"ambulatory_care_conditions_acute": "Ambulatory Care Sensitive Admissions (Acute Conditions) (IP-AA-004)",
66
"ambulatory_care_conditions_chronic": "Ambulatory Care Sensitive Admissions (Chronic Conditions) (IP-AA-005)",
77
"ambulatory_care_conditions_vaccine_preventable": "Ambulatory Care Sensitive Admissions (Vaccine Preventable) (IP-AA-006)",
8-
"ambulatory_emergency_care_high": "Ambulatory Emergency Care (High Potential) (IP-EF-001)",
9-
"ambulatory_emergency_care_low": "Ambulatory Emergency Care (Low Potential) (IP-EF-002)",
10-
"ambulatory_emergency_care_moderate": "Ambulatory Emergency Care (Moderate Potential) (IP-EF-003)",
11-
"ambulatory_emergency_care_very_high": "Ambulatory Emergency Care (Very High Potential) (IP-EF-004)",
128
"cancelled_operations": "Cancelled Operations (IP-AA-007)",
139
"consultant_to_consultant_reduction_adult_non-surgical": "Outpatient Consultant to Consultant Referrals (Adult, Non-Surgical) (OP-AA-001)",
1410
"consultant_to_consultant_reduction_adult_surgical": "Outpatient Consultant to Consultant Referrals (Adult, Surgical) (OP-AA-002)",
@@ -95,4 +91,4 @@
9591
"virtual_wards_efficiencies_heart_failure": "Virtual Wards LoS Reduction (Heart Failure) (IP-EF-027)",
9692
"zero_los_no_procedure_adult": "Admission With No Overnight Stay and No Procedure (Adults) (IP-AA-032)",
9793
"zero_los_no_procedure_child": "Admission With No Overnight Stay and No Procedure (Children) (IP-AA-033)"
98-
}
94+
}

inst/golem-config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ default:
2929
mitigators_type: activity_avoidance
3030
y_axis_title: "Admissions per 1,000 population"
3131
x_axis_title: "Financial Year"
32-
number_type: !expr scales::comma_format(accuracy = 0.001)
32+
number_type: !expr scales::comma_format(accuracy = 0.001, scale = 1000)
3333
funnel_x_title: "Catchment Population of Trust"
3434
funnel_number_type: !expr scales::comma_format()
3535
slider_scale: 1

0 commit comments

Comments
 (0)