Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
^\.vscode$
^app\.R$
^CODEOWNERS$
^cpma-explorer\.Rproj$
^tpma-explorer\.Rproj$
^dev$
^LICENSE\.md$
^README\.md$
Expand Down
17 changes: 8 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Package: cpma.explorer
Title: Explore CPMA Data
Version: 0.0.0.9000
Package: tpma.explorer
Title: Explore TPMA Data
Version: 0.1.0.9000
Authors@R:
person("Matt", "Dray", , "matt.dray@nhs.net", role = c("aut", "cre"))
Description: A Shiny-app-in-a-package to explore data related to
categories of potentially mitigaable activity (CPMAs).
Types of Potentially Mitigatable Activity (TPMAs).
License: MIT + file LICENSE
URL: https://github.com/The-Strategy-Unit/cpma-explorer
BugReports: https://github.com/The-Strategy-Unit/cpma-explorer/issues
URL: https://github.com/The-Strategy-Unit/tpma-explorer
BugReports: https://github.com/The-Strategy-Unit/tpma-explorer/issues
Depends:
R (>= 4.1.0)
Imports:
Expand All @@ -19,10 +19,9 @@ Imports:
jsonlite,
purrr,
rlang,
shiny,
stringr
shiny
Remotes:
The-Strategy-Unit/azkit
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(plot_rates)
export(run_app)
importFrom(rlang,.data)
importFrom(rlang,.env)
5 changes: 3 additions & 2 deletions R/add_external_resources.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Add External Resources to the Application
#' This function is internally used to add external resources inside the Shiny
#' application.
#' @noRd
add_external_resources <- function() {
shiny::addResourcePath(
"www",
Expand All @@ -12,9 +13,9 @@ add_external_resources <- function() {
}

#' Access Files in the Current App
#' @param ... character vectors, specifying subdirectory and file(s)
#' @param ... Character vectors, specifying subdirectory and file(s)
#' within your package. The default, none, returns the root of the app.
#' @noRd
app_sys <- function(...) {
system.file(..., package = "cpma.explorer")
system.file(..., package = "tpma.explorer")
}
83 changes: 29 additions & 54 deletions R/app_server.R
Original file line number Diff line number Diff line change
@@ -1,58 +1,33 @@
#' The application server-side
#' @param input,output,session Internal parameters for {shiny}.
#' Server-Side Application
#' @param input,output,session Internal parameters for 'shiny'.
#' @noRd
app_server <- function(input, output, session) {
# Data ----

container <- azkit::get_container(Sys.getenv("AZ_CONTAINER_INPUTS"))
rates <- azkit::read_azure_parquet(container, "rates", "dev")

# Reactives ----

selected_provider <- shiny::reactive(input$provider_select)
selected_strategy <- shiny::reactive(input$strategy_select)

rates_prepared <- shiny::reactive({
rates |>
dplyr::filter(
.data$provider == selected_provider(),
.data$strategy == selected_strategy()
) |>
dplyr::arrange(.data$fyear)
}) |>
shiny::bindEvent(selected_provider(), selected_strategy())

# Observers ----

shiny::observe({
providers <- jsonlite::read_json(
app_sys("app", "data", "datasets.json"),
simplify_vector = TRUE
)
provider_choices <- purrr::set_names(names(providers), providers)
shiny::updateSelectInput(
session,
"provider_select",
choices = provider_choices
)
})

shiny::observe({
strategies <- jsonlite::read_json(
app_sys("app", "data", "mitigators.json"),
simplify_vector = TRUE
)
strategy_choices <- purrr::set_names(names(strategies), strategies)
shiny::updateSelectInput(
session,
"strategy_select",
choices = strategy_choices
)
})

# Outputs ----

output$rates_plot <- shiny::renderPlot({
rates_prepared() |> plot_rates()
})
rates_data <- azkit::read_azure_parquet(container, "rates", "dev")

providers_lookup <- jsonlite::read_json(
app_sys("app", "data", "datasets.json"),
simplify_vector = TRUE
)

strategies_lookup <- jsonlite::read_json(
app_sys("app", "data", "mitigators.json"),
simplify_vector = TRUE
)

selected_provider <- mod_select_provider_server(
"mod_select_provider",
providers_lookup
)
selected_strategy <- mod_select_strategy_server(
"mod_select_strategy",
strategies_lookup
)

mod_plot_trend_server(
"mod_plot_trend",
rates_data,
selected_provider,
selected_strategy
)
}
22 changes: 6 additions & 16 deletions R/app_ui.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
#' The application User-Interface
#' @param request Internal parameter for shiny.
#' Application User Interface
#' @param request Internal parameter for 'shiny'.
#' @noRd
app_ui <- function(request) {
bslib::page_sidebar(
title = "CPMA Explorer",
title = "TPMA Explorer",
sidebar = bslib::sidebar(
shiny::selectInput(
"provider_select",
"Choose a provider:",
choices = NULL
),
shiny::selectInput(
"strategy_select",
"Choose a strategy:",
choices = NULL
)
mod_select_provider_ui("mod_select_provider"),
mod_select_strategy_ui("mod_select_strategy"),
),

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

bslib::card(
bslib::card_header("Trend in rates"),
bslib::card_body(
shiny::plotOutput("rates_plot")
),
bslib::card_body(mod_plot_trend_ui("mod_plot_trend")),
full_screen = TRUE
)
)
Expand Down
20 changes: 8 additions & 12 deletions R/fct_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@
#' financial year in the form `"2023/24"`.
#' @param rate_col Character. Name of the column in `rates_df` containing the
#' rate value (the type of which is dependent on the strategy).
#' @return A ggplot2 object.
#' @export
plot_rates <- function(rates_df, fyear_col = "fyear", rate_col = "rate") {
x_breaks <- rates_df[[fyear_col]]
x_labels <- enstring_fyear(x_breaks)

rates_df |>
ggplot2::ggplot(ggplot2::aes(
x = .data[[fyear_col]],
y = .data[[rate_col]]
)) +
ggplot2::ggplot(
ggplot2::aes(
x = .data[[fyear_col]],
y = .data[[rate_col]]
)
) +
ggplot2::geom_line() +
ggplot2::labs(
x = "Financial year",
y = "Rate"
) +
ggplot2::scale_x_continuous(
breaks = x_breaks,
labels = x_labels,
guide = ggplot2::guide_axis(angle = 45)
)
}
8 changes: 0 additions & 8 deletions R/fct_utils.R

This file was deleted.

46 changes: 46 additions & 0 deletions R/mod_plot_trend.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' Plot Trend UI
#' @param id,input,output,session Internal parameters for `shiny`.
#' @noRd
mod_plot_trend_ui <- function(id) {
ns <- shiny::NS(id)
shiny::plotOutput(ns("rates_plot"))
}

#' Plot Trend Server
#' @param id Internal parameter for `shiny`.
#' @param rates A data.frame. Annual rate values for combinations of provider
#' and TPMA.
#' @param selected_provider Character. Provider code, e.g. `"RCX"`.
#' @param selected_strategy Character. TPMA variable name, e.g.
#' `"discharged_no_treatment_adult_ambulance"`.
#' @noRd
mod_plot_trend_server <- function(
id,
rates,
selected_provider,
selected_strategy
) {
shiny::moduleServer(id, function(input, output, session) {
rates_prepared <- 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)
})

output$rates_plot <- shiny::renderPlot({
rates <- rates_prepared()
shiny::validate(shiny::need(
nrow(rates) > 0,
"No data available for these selections"
))
plot_rates(rates)
})
})
}
31 changes: 31 additions & 0 deletions R/mod_select_provider.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#' Select Provider UI
#' @param id,input,output,session Internal parameters for `shiny`.
#' @noRd
mod_select_provider_ui <- function(id) {
ns <- shiny::NS(id)
shiny::selectInput(
ns("provider_select"),
"Choose a provider:",
choices = NULL
)
}

#' Select Provider Server
#' @param id Internal parameter for `shiny`.
#' @param providers A named list. Names are provider codes (e.g. `"RCF"`) and
#' their values are the corresponding human-readable names and codes (e.g.
#' `"Airedale NHS Foundation Trust (RCF)"`).
#' @noRd
mod_select_provider_server <- function(id, providers) {
shiny::moduleServer(id, function(input, output, session) {
shiny::observe({
provider_choices <- purrr::set_names(names(providers), providers)
shiny::updateSelectInput(
session,
"provider_select",
choices = provider_choices
)
})
shiny::reactive(input$provider_select)
})
}
33 changes: 33 additions & 0 deletions R/mod_select_strategy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#' Select Strategy UI
#' @param id,input,output,session Internal parameters for `shiny`.
#' @noRd
mod_select_strategy_ui <- function(id) {
ns <- shiny::NS(id)
shiny::selectInput(
ns("strategy_select"),
"Choose a strategy:",
choices = NULL
)
}

#' Select Strategy Server
#' @param id Internal parameter for `shiny`.
#' @param strategies A named list. Names are TPMA variable names (e.g.
#' `"alcohol_partially_attributable_acute"`) and their values are the
#' corresponding human-readable names and codes (e.g.
#' `"Alcohol Related Admissions (Acute Conditions - Partially Attributable)
#' (IP-AA-001)"`).
#' @noRd
mod_select_strategy_server <- function(id, strategies) {
shiny::moduleServer(id, function(input, output, session) {
shiny::observe({
strategy_choices <- purrr::set_names(names(strategies), strategies)
shiny::updateSelectInput(
session,
"strategy_select",
choices = strategy_choices
)
})
shiny::reactive(input$strategy_select)
})
}
File renamed without changes.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# cpma-explorer
# tpma-explorer

[![Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.](https://www.repostatus.org/badges/latest/concept.svg)](https://www.repostatus.org/#concept)

An app to explore data for categories of potentially-mitigable activity (CPMAs).
An app to explore data for Types of Potentially-Mitigable Activity (TPMAs).

The app is [deployed to Posit Connect](https://connect.strategyunitwm.nhs.uk/cpma-explorer/) (login and permissions required).
The app is [deployed to Posit Connect](https://connect.strategyunitwm.nhs.uk/tpma-explorer/) (login and permissions required).

The app is made with [Shiny](https://shiny.posit.co/) and is an R package following [the nolem approach](https://github.com/StatsRhian/nolem).
4 changes: 2 additions & 2 deletions dev/deploy.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rsconnect::deployApp(
appName = "cpma-explorer",
appTitle = "CPMA explorer",
appName = "tpma-explorer",
appTitle = "TPMA explorer",
server = "connect.strategyunitwm.nhs.uk",
appId = 200,
appFiles = c(
Expand Down
15 changes: 0 additions & 15 deletions man/add_external_resources.Rd

This file was deleted.

Loading