@@ -125,3 +125,166 @@ make_scheme_dt <- function(trust_code_lookup) {
125
125
)
126
126
127
127
}
128
+
129
+
130
+ # ' Make the scheme uptake DT object
131
+ # '
132
+ # ' Renders a DT object showing the proportion of mitigators in use by each scheme.
133
+ # ' Two rates are shown:
134
+ # ' 1. covers all available mitigators,
135
+ # ' 2. covers the subset of mitigators selected by the user.
136
+ # '
137
+ # ' @param dat Tibble - the full prepared dataset for this app
138
+ # ' @param selected_schemes Character vector - a list of mitigator_codes selected by the user
139
+ # '
140
+ # ' @return DT object listing schemes and the proportions of mitigators in use by them
141
+ # ' @export
142
+ make_mitigator_uptake_dt <- function (dat , selected_schemes ) {
143
+
144
+ dat | >
145
+ # remove ampersand from mitigator names - causes issues with DT filters
146
+ dplyr :: mutate(
147
+ mitigator_name = gsub(
148
+ pattern = ' A&E' ,
149
+ x = mitigator_name ,
150
+ replacement = ' ED'
151
+ )
152
+ ) | >
153
+ # count schemes per mitigator
154
+ dplyr :: summarise(
155
+ n_schemes_using_all = dplyr :: n_distinct(
156
+ scheme_code ,
157
+ na.rm = TRUE
158
+ ),
159
+ n_schemes_using_selected = dplyr :: n_distinct(
160
+ scheme_code [scheme_code %in% selected_schemes ],
161
+ na.rm = TRUE
162
+ ),
163
+ .by = c(mitigator_activity_type , mitigator_group , mitigator_name )
164
+ ) | >
165
+ # convert to rate
166
+ dplyr :: mutate(
167
+ # get denominators
168
+ n_schemes_all = dplyr :: n_distinct(
169
+ dat $ scheme_code ,
170
+ na.rm = TRUE
171
+ ),
172
+ n_schemes_selected = dplyr :: n_distinct(
173
+ dat $ scheme_code [dat $ scheme_code %in% selected_schemes ],
174
+ na.rm = TRUE
175
+ ),
176
+
177
+ # convert to rates
178
+ n_schemes_using_all_rate = n_schemes_using_all / n_schemes_all ,
179
+ n_schemes_using_selected_rate = n_schemes_using_selected / n_schemes_selected
180
+ ) | >
181
+ # prepare for display
182
+ dplyr :: select(
183
+ - c(n_schemes_using_all , n_schemes_all ,
184
+ n_schemes_using_selected , n_schemes_selected )
185
+ ) | >
186
+ dplyr :: mutate(
187
+ # convert mitigators to factors for drop-down selectors in DT
188
+ mitigator_activity_type = mitigator_activity_type | > factor (),
189
+ mitigator_group = mitigator_group | > factor (),
190
+ mitigator_name = mitigator_name | > factor ()
191
+ ) | >
192
+ # display as DT
193
+ DT :: datatable(
194
+ rownames = FALSE ,
195
+ options = list (pageLength = 100 , dom = ' Bft' ),
196
+ fillContainer = TRUE ,
197
+ escape = TRUE ,
198
+ filter = ' top' ,
199
+ colnames = c(
200
+ ' Activity type' , ' Mitigator group' , ' Mitigator' ,
201
+ ' Coverage (all schemes)' , ' Coverage (selected schemes)'
202
+ )
203
+ ) | >
204
+ DT :: formatPercentage(
205
+ columns = c(' n_schemes_using_all_rate' , ' n_schemes_using_selected_rate' )
206
+ )
207
+ }
208
+
209
+
210
+ # ' Make the mitigator uptake DT object
211
+ # '
212
+ # ' Renders a DT object showing the proportion of mitigators used by each scheme.
213
+ # ' Two rates are shown:
214
+ # ' 1. covers all available mitigators,
215
+ # ' 2. covers the subset of mitigators selected by the user.
216
+ # '
217
+ # ' @param dat Tibble - the full prepared dataset for this app
218
+ # ' @param selected_schemes Character vector - a list of scheme_codes selected by the user
219
+ # ' @param focal_scheme Character vector - the focal scheme_code
220
+ # '
221
+ # ' @return DT object listing schemes and the proportions of mitigators in use by them
222
+ # ' @export
223
+ make_scheme_uptake_dt <- function (dat , selected_mitigators , selected_schemes , focal_scheme ) {
224
+
225
+ dat | >
226
+ # count schemes per mitigator
227
+ dplyr :: summarise(
228
+ n_mitigators_using_all = dplyr :: n_distinct(
229
+ mitigator_code ,
230
+ na.rm = T
231
+ ),
232
+ n_mitigators_using_selected = dplyr :: n_distinct(
233
+ mitigator_code [mitigator_code %in% selected_mitigators ],
234
+ na.rm = T
235
+ ),
236
+ .by = c(scheme_code , scheme_name )
237
+ ) | >
238
+ # convert to rate
239
+ dplyr :: mutate(
240
+ # get denominators
241
+ n_mitigators_all = dplyr :: n_distinct(
242
+ dat $ mitigator_code ,
243
+ na.rm = T
244
+ ),
245
+ n_mitigators_selected = dplyr :: n_distinct(
246
+ dat $ mitigator_code [dat $ mitigator_code %in% selected_mitigators ],
247
+ na.rm = T
248
+ ),
249
+
250
+ # convert to rates
251
+ n_mitigators_using_all_rate = n_mitigators_using_all / n_mitigators_all ,
252
+ n_mitigators_using_selected_rate = n_mitigators_using_selected / n_mitigators_selected
253
+ ) | >
254
+ # prepare for display
255
+ dplyr :: select(
256
+ - c(n_mitigators_using_all , n_mitigators_all ,
257
+ n_mitigators_using_selected , n_mitigators_selected )
258
+ ) | >
259
+ dplyr :: mutate(
260
+ # convert scheme details to factors for drop-down selectors in DT
261
+ scheme_code = scheme_code | > factor (),
262
+ scheme_name = scheme_name | > factor ()
263
+ ) | >
264
+ dplyr :: filter(! is.na(scheme_code )) | >
265
+ # display as DT
266
+ DT :: datatable(
267
+ rownames = FALSE ,
268
+ options = list (pageLength = 100 , dom = ' Bft' ),
269
+ fillContainer = TRUE ,
270
+ escape = TRUE ,
271
+ style = ' default' , # needed to ensure formatStyle works as expected - due to clashes with bslib & bootstrap theme
272
+ colnames = c(
273
+ ' Scheme code' , ' Scheme name' , ' Coverage (all mitigators)' ,
274
+ ' Coverage (selected mitigators)'
275
+ ),
276
+ filter = ' top'
277
+ ) | >
278
+ DT :: formatPercentage(
279
+ columns = c(' n_mitigators_using_all_rate' , ' n_mitigators_using_selected_rate' )
280
+ ) | >
281
+ # style selected schemes in bold
282
+ DT :: formatStyle(
283
+ columns = ' scheme_code' ,
284
+ target = ' row' ,
285
+ # highlight all selected schemes in bold
286
+ fontWeight = DT :: styleEqual(levels = c(selected_schemes ), ' bold' , ' normal' ),
287
+ # highlight focal scheme in red too
288
+ color = DT :: styleEqual(levels = focal_scheme , ' red' , ' black' )
289
+ )
290
+ }
0 commit comments