Skip to content

Commit 9831cd3

Browse files
authored
Merge pull request #19 from The-Strategy-Unit/14-md
Add TPMA descriptions
2 parents d55e1cf + ddaa75e commit 9831cd3

File tree

10 files changed

+172
-6
lines changed

10 files changed

+172
-6
lines changed

DESCRIPTION

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ Imports:
1616
bslib,
1717
dplyr,
1818
ggplot2,
19+
glue,
1920
jsonlite,
21+
markdown,
2022
purrr,
2123
rlang,
22-
shiny
24+
shiny,
25+
stringr,
26+
withr
2327
Remotes:
2428
The-Strategy-Unit/azkit
2529
Encoding: UTF-8

NAMESPACE

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

3+
export(convert_md_to_html)
4+
export(fetch_strategy_description)
35
export(plot_rates)
46
export(run_app)
57
importFrom(rlang,.data)

R/ZZZ.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
#' @importFrom rlang .data .env
22
NULL
3+
4+
utils::globalVariables("temp")

R/app_server.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ app_server <- function(input, output, session) {
99
app_sys("app", "data", "datasets.json"),
1010
simplify_vector = TRUE
1111
)
12-
1312
strategies_lookup <- jsonlite::read_json(
1413
app_sys("app", "data", "mitigators.json"),
1514
simplify_vector = TRUE
1615
)
16+
descriptions_lookup <- jsonlite::read_json(
17+
"inst/app/data/descriptions.json",
18+
simplifyVector = TRUE
19+
)
1720

1821
selected_provider <- mod_select_provider_server(
1922
"mod_select_provider",
@@ -24,6 +27,11 @@ app_server <- function(input, output, session) {
2427
strategies_lookup
2528
)
2629

30+
mod_show_description_server(
31+
"mod_show_description",
32+
descriptions_lookup,
33+
selected_strategy
34+
)
2735
mod_plot_trend_server(
2836
"mod_plot_trend",
2937
rates_data,

R/app_ui.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ app_ui <- function(request) {
2020
The information presented here should not be relied on as fact."
2121
),
2222

23-
bslib::card(
24-
bslib::card_header("Trend in rates"),
25-
bslib::card_body(mod_plot_trend_ui("mod_plot_trend")),
26-
full_screen = TRUE
23+
bslib::layout_columns(
24+
bslib::card(
25+
bslib::card_header("Description"),
26+
bslib::card_body(mod_show_description_ui("mod_show_description"))
27+
),
28+
bslib::card(
29+
bslib::card_header("Trend in rates"),
30+
bslib::card_body(mod_plot_trend_ui("mod_plot_trend")),
31+
full_screen = TRUE
32+
)
2733
)
2834
)
2935
}

R/mod_show_description.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#' Show Descriptions UI
2+
#' @param id,input,output,session Internal parameters for `shiny`.
3+
#' @noRd
4+
mod_show_description_ui <- function(id) {
5+
ns <- shiny::NS(id)
6+
shiny::htmlOutput(ns("strategy_description"))
7+
}
8+
9+
#' Show Descriptions Server
10+
#' @param id Internal parameter for `shiny`.
11+
#' @noRd
12+
mod_show_description_server <- function(
13+
id,
14+
descriptions_lookup,
15+
selected_strategy
16+
) {
17+
shiny::moduleServer(id, function(input, output, session) {
18+
output$strategy_description <- shiny::renderText({
19+
shiny::req(selected_strategy())
20+
selected_strategy() |>
21+
fetch_strategy_description(descriptions_lookup) |>
22+
convert_md_to_html()
23+
})
24+
})
25+
}

R/utils_description.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#' Fetch Strategy Text from NHP Inputs
2+
#' @param strategy Character. The variable name for a strategy, e.g.
3+
#' `"discharged_no_treatment_adult_ambulance"`.
4+
#' @param descriptions_lookup Character. The names of the available Markdown
5+
#' description files.
6+
#' @details Markdown files containing strategy descriptions are read from
7+
#' [NHP Inputs](https://github.com/The-Strategy-Unit/nhp_inputs/).
8+
#' @return Character.
9+
#' @export
10+
fetch_strategy_description <- function(strategy, descriptions_lookup) {
11+
is_stub <- stringr::str_detect(strategy, descriptions_lookup)
12+
strategy_stub <- descriptions_lookup[is_stub]
13+
14+
withr::with_tempfile("temp", {
15+
# nolint start object_usage_linter.
16+
utils::download.file(
17+
glue::glue(
18+
"https://raw.githubusercontent.com/The-Strategy-Unit/nhp_inputs/",
19+
"refs/heads/main/inst/app/strategy_text/{strategy_stub}.md"
20+
),
21+
temp
22+
)
23+
cat("\n", file = temp, append = TRUE) # stop 'incomplete final line' warning
24+
paste(readLines(temp), collapse = "\n")
25+
# nolint end
26+
})
27+
}
28+
29+
#' Convert Strategy Text from Markdown to HTML
30+
#' @param text Character. The Markdown text description for a strategy, as read
31+
#' by [fetch_strategy_description].
32+
#' @return HTML/character.
33+
#' @export
34+
convert_md_to_html <- function(text) {
35+
shiny::HTML(markdown::mark_html(text, output = FALSE, template = FALSE))
36+
}

inst/app/data/descriptions.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[
2+
"alcohol_partially_attributable",
3+
"alcohol_wholly_attributable",
4+
"ambulatory_care_conditions",
5+
"ambulatory_emergency_care",
6+
"cancelled_operations",
7+
"consultant_to_consultant_reduction",
8+
"convert_to_tele",
9+
"day_procedures",
10+
"discharged_no_treatment",
11+
"emergency_elderly",
12+
"enhanced_recovery",
13+
"eol_care",
14+
"evidence_based_interventions",
15+
"excess_beddays",
16+
"falls_related_admissions",
17+
"followup_reduction",
18+
"frail_elderly",
19+
"frequent_attenders",
20+
"general_los_reduction",
21+
"gp_referred_first_attendance_reduction",
22+
"intentional_self_harm",
23+
"left_before_seen",
24+
"low_cost_discharged",
25+
"medically_unexplained_related_admissions",
26+
"medicines_related_admissions",
27+
"obesity_related_admissions",
28+
"pre-op_los",
29+
"raid_ae",
30+
"raid_ip",
31+
"readmission_within_28_days",
32+
"same_day_emergency_care",
33+
"smoking",
34+
"stroke_early_supported_discharge",
35+
"virtual_wards_activity_avoidance_ari",
36+
"virtual_wards_activity_avoidance_heart_failure",
37+
"virtual_wards_efficiencies_ari",
38+
"virtual_wards_efficiencies_heart_failure",
39+
"zero_los_no_procedure"
40+
]

man/convert_md_to_html.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fetch_strategy_description.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)