Skip to content

Commit d55e1cf

Browse files
authored
Merge pull request #12 from The-Strategy-Unit/5-mvp
Update terminology and modularise
2 parents c94a854 + 26462e9 commit d55e1cf

20 files changed

+197
-160
lines changed

.Rbuildignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
^\.vscode$
55
^app\.R$
66
^CODEOWNERS$
7-
^cpma-explorer\.Rproj$
7+
^tpma-explorer\.Rproj$
88
^dev$
99
^LICENSE\.md$
1010
^README\.md$

DESCRIPTION

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
Package: cpma.explorer
2-
Title: Explore CPMA Data
3-
Version: 0.0.0.9000
1+
Package: tpma.explorer
2+
Title: Explore TPMA Data
3+
Version: 0.1.0.9000
44
Authors@R:
55
person("Matt", "Dray", , "matt.dray@nhs.net", role = c("aut", "cre"))
66
Description: A Shiny-app-in-a-package to explore data related to
7-
categories of potentially mitigaable activity (CPMAs).
7+
Types of Potentially Mitigatable Activity (TPMAs).
88
License: MIT + file LICENSE
9-
URL: https://github.com/The-Strategy-Unit/cpma-explorer
10-
BugReports: https://github.com/The-Strategy-Unit/cpma-explorer/issues
9+
URL: https://github.com/The-Strategy-Unit/tpma-explorer
10+
BugReports: https://github.com/The-Strategy-Unit/tpma-explorer/issues
1111
Depends:
1212
R (>= 4.1.0)
1313
Imports:
@@ -19,10 +19,9 @@ Imports:
1919
jsonlite,
2020
purrr,
2121
rlang,
22-
shiny,
23-
stringr
22+
shiny
2423
Remotes:
2524
The-Strategy-Unit/azkit
2625
Encoding: UTF-8
2726
Roxygen: list(markdown = TRUE)
28-
RoxygenNote: 7.3.2
27+
RoxygenNote: 7.3.3

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(plot_rates)
34
export(run_app)
45
importFrom(rlang,.data)
56
importFrom(rlang,.env)

R/add_external_resources.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#' Add External Resources to the Application
22
#' This function is internally used to add external resources inside the Shiny
33
#' application.
4+
#' @noRd
45
add_external_resources <- function() {
56
shiny::addResourcePath(
67
"www",
@@ -12,9 +13,9 @@ add_external_resources <- function() {
1213
}
1314

1415
#' Access Files in the Current App
15-
#' @param ... character vectors, specifying subdirectory and file(s)
16+
#' @param ... Character vectors, specifying subdirectory and file(s)
1617
#' within your package. The default, none, returns the root of the app.
1718
#' @noRd
1819
app_sys <- function(...) {
19-
system.file(..., package = "cpma.explorer")
20+
system.file(..., package = "tpma.explorer")
2021
}

R/app_server.R

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,33 @@
1-
#' The application server-side
2-
#' @param input,output,session Internal parameters for {shiny}.
1+
#' Server-Side Application
2+
#' @param input,output,session Internal parameters for 'shiny'.
33
#' @noRd
44
app_server <- function(input, output, session) {
5-
# Data ----
6-
75
container <- azkit::get_container(Sys.getenv("AZ_CONTAINER_INPUTS"))
8-
rates <- azkit::read_azure_parquet(container, "rates", "dev")
9-
10-
# Reactives ----
11-
12-
selected_provider <- shiny::reactive(input$provider_select)
13-
selected_strategy <- shiny::reactive(input$strategy_select)
14-
15-
rates_prepared <- shiny::reactive({
16-
rates |>
17-
dplyr::filter(
18-
.data$provider == selected_provider(),
19-
.data$strategy == selected_strategy()
20-
) |>
21-
dplyr::arrange(.data$fyear)
22-
}) |>
23-
shiny::bindEvent(selected_provider(), selected_strategy())
24-
25-
# Observers ----
26-
27-
shiny::observe({
28-
providers <- jsonlite::read_json(
29-
app_sys("app", "data", "datasets.json"),
30-
simplify_vector = TRUE
31-
)
32-
provider_choices <- purrr::set_names(names(providers), providers)
33-
shiny::updateSelectInput(
34-
session,
35-
"provider_select",
36-
choices = provider_choices
37-
)
38-
})
39-
40-
shiny::observe({
41-
strategies <- jsonlite::read_json(
42-
app_sys("app", "data", "mitigators.json"),
43-
simplify_vector = TRUE
44-
)
45-
strategy_choices <- purrr::set_names(names(strategies), strategies)
46-
shiny::updateSelectInput(
47-
session,
48-
"strategy_select",
49-
choices = strategy_choices
50-
)
51-
})
52-
53-
# Outputs ----
54-
55-
output$rates_plot <- shiny::renderPlot({
56-
rates_prepared() |> plot_rates()
57-
})
6+
rates_data <- azkit::read_azure_parquet(container, "rates", "dev")
7+
8+
providers_lookup <- jsonlite::read_json(
9+
app_sys("app", "data", "datasets.json"),
10+
simplify_vector = TRUE
11+
)
12+
13+
strategies_lookup <- jsonlite::read_json(
14+
app_sys("app", "data", "mitigators.json"),
15+
simplify_vector = TRUE
16+
)
17+
18+
selected_provider <- mod_select_provider_server(
19+
"mod_select_provider",
20+
providers_lookup
21+
)
22+
selected_strategy <- mod_select_strategy_server(
23+
"mod_select_strategy",
24+
strategies_lookup
25+
)
26+
27+
mod_plot_trend_server(
28+
"mod_plot_trend",
29+
rates_data,
30+
selected_provider,
31+
selected_strategy
32+
)
5833
}

R/app_ui.R

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
#' The application User-Interface
2-
#' @param request Internal parameter for shiny.
1+
#' Application User Interface
2+
#' @param request Internal parameter for 'shiny'.
33
#' @noRd
44
app_ui <- function(request) {
55
bslib::page_sidebar(
6-
title = "CPMA Explorer",
6+
title = "TPMA Explorer",
77
sidebar = bslib::sidebar(
8-
shiny::selectInput(
9-
"provider_select",
10-
"Choose a provider:",
11-
choices = NULL
12-
),
13-
shiny::selectInput(
14-
"strategy_select",
15-
"Choose a strategy:",
16-
choices = NULL
17-
)
8+
mod_select_provider_ui("mod_select_provider"),
9+
mod_select_strategy_ui("mod_select_strategy"),
1810
),
1911

2012
bslib::card(
@@ -30,9 +22,7 @@ app_ui <- function(request) {
3022

3123
bslib::card(
3224
bslib::card_header("Trend in rates"),
33-
bslib::card_body(
34-
shiny::plotOutput("rates_plot")
35-
),
25+
bslib::card_body(mod_plot_trend_ui("mod_plot_trend")),
3626
full_screen = TRUE
3727
)
3828
)

R/fct_plot.R

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@
66
#' financial year in the form `"2023/24"`.
77
#' @param rate_col Character. Name of the column in `rates_df` containing the
88
#' rate value (the type of which is dependent on the strategy).
9+
#' @return A ggplot2 object.
10+
#' @export
911
plot_rates <- function(rates_df, fyear_col = "fyear", rate_col = "rate") {
10-
x_breaks <- rates_df[[fyear_col]]
11-
x_labels <- enstring_fyear(x_breaks)
12-
1312
rates_df |>
14-
ggplot2::ggplot(ggplot2::aes(
15-
x = .data[[fyear_col]],
16-
y = .data[[rate_col]]
17-
)) +
13+
ggplot2::ggplot(
14+
ggplot2::aes(
15+
x = .data[[fyear_col]],
16+
y = .data[[rate_col]]
17+
)
18+
) +
1819
ggplot2::geom_line() +
1920
ggplot2::labs(
2021
x = "Financial year",
2122
y = "Rate"
22-
) +
23-
ggplot2::scale_x_continuous(
24-
breaks = x_breaks,
25-
labels = x_labels,
26-
guide = ggplot2::guide_axis(angle = 45)
2723
)
2824
}

R/fct_utils.R

Lines changed: 0 additions & 8 deletions
This file was deleted.

R/mod_plot_trend.R

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#' Plot Trend UI
2+
#' @param id,input,output,session Internal parameters for `shiny`.
3+
#' @noRd
4+
mod_plot_trend_ui <- function(id) {
5+
ns <- shiny::NS(id)
6+
shiny::plotOutput(ns("rates_plot"))
7+
}
8+
9+
#' Plot Trend Server
10+
#' @param id Internal parameter for `shiny`.
11+
#' @param rates A data.frame. Annual rate values for combinations of provider
12+
#' and TPMA.
13+
#' @param selected_provider Character. Provider code, e.g. `"RCX"`.
14+
#' @param selected_strategy Character. TPMA variable name, e.g.
15+
#' `"discharged_no_treatment_adult_ambulance"`.
16+
#' @noRd
17+
mod_plot_trend_server <- function(
18+
id,
19+
rates,
20+
selected_provider,
21+
selected_strategy
22+
) {
23+
shiny::moduleServer(id, function(input, output, session) {
24+
rates_prepared <- shiny::reactive({
25+
shiny::req(rates)
26+
shiny::req(selected_provider())
27+
shiny::req(selected_strategy())
28+
29+
rates |>
30+
dplyr::filter(
31+
.data$provider == selected_provider(),
32+
.data$strategy == selected_strategy()
33+
) |>
34+
dplyr::arrange(.data$fyear)
35+
})
36+
37+
output$rates_plot <- shiny::renderPlot({
38+
rates <- rates_prepared()
39+
shiny::validate(shiny::need(
40+
nrow(rates) > 0,
41+
"No data available for these selections"
42+
))
43+
plot_rates(rates)
44+
})
45+
})
46+
}

R/mod_select_provider.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#' Select Provider UI
2+
#' @param id,input,output,session Internal parameters for `shiny`.
3+
#' @noRd
4+
mod_select_provider_ui <- function(id) {
5+
ns <- shiny::NS(id)
6+
shiny::selectInput(
7+
ns("provider_select"),
8+
"Choose a provider:",
9+
choices = NULL
10+
)
11+
}
12+
13+
#' Select Provider Server
14+
#' @param id Internal parameter for `shiny`.
15+
#' @param providers A named list. Names are provider codes (e.g. `"RCF"`) and
16+
#' their values are the corresponding human-readable names and codes (e.g.
17+
#' `"Airedale NHS Foundation Trust (RCF)"`).
18+
#' @noRd
19+
mod_select_provider_server <- function(id, providers) {
20+
shiny::moduleServer(id, function(input, output, session) {
21+
shiny::observe({
22+
provider_choices <- purrr::set_names(names(providers), providers)
23+
shiny::updateSelectInput(
24+
session,
25+
"provider_select",
26+
choices = provider_choices
27+
)
28+
})
29+
shiny::reactive(input$provider_select)
30+
})
31+
}

0 commit comments

Comments
 (0)