generated from The-Strategy-Unit/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_plot_rates.R
More file actions
135 lines (121 loc) · 3.81 KB
/
mod_plot_rates.R
File metadata and controls
135 lines (121 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#' Plot Rates UI
#' @param id,input,output,session Internal parameters for `shiny`.
#' @noRd
mod_plot_rates_ui <- function(id) {
ns <- shiny::NS(id)
# Rates plots share a y-axis, so don't wrap
bslib::layout_columns(
col_widths = c(5, 5, 2),
fill = FALSE,
fillable = FALSE,
mod_plot_rates_trend_ui(ns("mod_plot_rates_trend")),
mod_plot_rates_funnel_ui(ns("mod_plot_rates_funnel")),
mod_plot_rates_box_ui(ns("mod_plot_rates_box"))
)
}
#' Plot Rates Server
#' @param id Internal parameter for `shiny`.
#' @param rates A data.frame. Annual rate values for combinations of provider
#' and strategy.
#' @param strategies_config List. Configuration for strategies from the
#' `"mitigators_config"` element of `golem-config.yml`, read in with
#' [get_golem_config].
#' @param peers_lookup A data.frame. A row per provider-peer pair.
#' @param selected_provider Character. Provider code, e.g. `"RCF"`.
#' @param selected_strategy Character. Strategy variable name, e.g.
#' `"alcohol_partially_attributable_acute"`.
#' @param baseline_year Integer. Baseline year in the form `202324`.
#' @noRd
mod_plot_rates_server <- function(
id,
rates,
strategies_config,
peers_lookup,
selected_provider,
selected_strategy,
baseline_year
) {
shiny::moduleServer(id, function(input, output, session) {
# Prepare data ----
rates_trend_data <- shiny::reactive({
shiny::req(rates())
shiny::req(selected_provider())
shiny::req(selected_strategy())
rates() |>
dplyr::filter(
.data$provider == selected_provider(),
.data$strategy == selected_strategy()
) |>
dplyr::arrange(.data$fyear)
})
rates_baseline_data <- shiny::reactive({
shiny::req(rates())
shiny::req(peers_lookup())
shiny::req(selected_provider())
shiny::req(selected_strategy())
provider_peers <- isolate_provider_peers(
selected_provider(),
peers_lookup()
)
rates() |>
generate_rates_baseline_data(
selected_provider(),
provider_peers,
selected_strategy(),
baseline_year
)
})
rates_funnel_data <- shiny::reactive({
shiny::req(rates_baseline_data())
rates_baseline_data() |> generate_rates_funnel_data()
})
# Prepare variables ----
y_axis_limits <- shiny::reactive({
shiny::req(rates_trend_data())
shiny::req(rates_funnel_data())
range(c(
rates_trend_data()[["rate"]],
rates_funnel_data()[["rate"]],
rates_funnel_data()[["lower3"]],
rates_funnel_data()[["upper3"]]
)) |>
pmax(0)
})
strategy_config <- shiny::reactive({
shiny::req(strategies_config)
shiny::req(selected_strategy())
strategy_group_lookup <- strategies_config |> make_strategy_group_lookup()
strategy_group <- strategy_group_lookup |>
dplyr::filter(.data$strategy == selected_strategy()) |>
dplyr::pull("group")
strategies_config[[strategy_group]]
})
y_axis_title <- shiny::reactive({
shiny::req(strategy_config())
strategy_config()[["y_axis_title"]]
})
funnel_x_title <- shiny::reactive({
shiny::req(strategy_config())
strategy_config()[["funnel_x_title"]]
})
# Declare modules ----
mod_plot_rates_trend_server(
"mod_plot_rates_trend",
rates_trend_data,
y_axis_limits,
y_axis_title,
baseline_year
)
mod_plot_rates_funnel_server(
"mod_plot_rates_funnel",
rates_funnel_data,
y_axis_limits,
funnel_x_title
)
mod_plot_rates_box_server(
"mod_plot_rates_box",
rates_baseline_data,
y_axis_limits
)
})
}