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
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
^\.github$
^\.Renviron\.example$
^\.Rproj\.user$
^\.vscode$
^app\.R$
Expand Down
2 changes: 2 additions & 0 deletions .Renviron.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AZ_CONTAINER_INPUTS=
AZ_STORAGE_EP=
14 changes: 13 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ Description: A Shiny-app-in-a-package to explore data related to
License: MIT + file LICENSE
URL: https://github.com/The-Strategy-Unit/cpma-explorer
BugReports: https://github.com/The-Strategy-Unit/cpma-explorer/issues
Depends:
R (>= 4.1.0)
Imports:
azkit,
bsicons,
bslib,
shiny
dplyr,
ggplot2,
jsonlite,
purrr,
rlang,
shiny,
stringr
Remotes:
The-Strategy-Unit/azkit
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(run_app)
importFrom(rlang,.data)
importFrom(rlang,.env)
2 changes: 2 additions & 0 deletions R/ZZZ.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#' @importFrom rlang .data .env
NULL
3 changes: 1 addition & 2 deletions R/add_external_resources.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ add_external_resources <- function() {
#' @param ... character vectors, specifying subdirectory and file(s)
#' within your package. The default, none, returns the root of the app.
#' @noRd
#' @noRd
app_sys <- function(...) {
system.file(..., package = "cpma-explorer")
system.file(..., package = "cpma.explorer")
}
63 changes: 51 additions & 12 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,57 @@
#' @param input,output,session Internal parameters for {shiny}.
#' @noRd
app_server <- function(input, output, session) {
output$distPlot <- shiny::renderPlot({
x <- datasets::faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)

# draw the histogram with the specified number of bins
graphics::hist(
x,
breaks = bins,
col = "darkgray",
border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times"
# 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()
})
}
39 changes: 27 additions & 12 deletions R/app_ui.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
#' The application User-Interface
#' @param request Internal parameter for `{shiny}`.
#' @param request Internal parameter for shiny.
#' @noRd
app_ui <- function(request) {
bslib::page_sidebar(
title = "Old Faithful Geyser Data",
lang = "en",
theme = bslib::bs_theme(primary = "#005EB8", base_font = "Frutiger"),
title = "CPMA Explorer",
sidebar = bslib::sidebar(
shiny::sliderInput(
"bins",
"Number of bins:",
min = 1,
max = 50,
value = 30
shiny::selectInput(
"provider_select",
"Choose a provider:",
choices = NULL
),
shiny::selectInput(
"strategy_select",
"Choose a strategy:",
choices = NULL
)
),

bslib::card(
bslib::card_header("A nice plot"),
shiny::plotOutput("distPlot")
fill = FALSE,
bslib::card_header(
class = "bg-warning",
bsicons::bs_icon("exclamation-triangle"),
"Warning"
),
"This application is in development and its output has not been verified.
The information presented here should not be relied on as fact."
),

bslib::card(
bslib::card_header("Trend in rates"),
bslib::card_body(
shiny::plotOutput("rates_plot")
),
full_screen = TRUE
)
)
}
28 changes: 28 additions & 0 deletions R/fct_plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' Plot Trend in Rates
#' @param rates_df A data.frame. Must contain columns given by `fyear_col` and
#' `rate_col`. Pre-filtered for a given provider and strategy. One row per
#' financial year.
#' @param fyear_col Character. Name of the column in `rates_df` containing the
#' 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).
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::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: 8 additions & 0 deletions R/fct_utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#' Convert a Financial Year to Character
#' @param year Integer. Expressed in the form `202526`. To be converted to the
#' form `"2025/26"`.
enstring_fyear <- function(year) {
yyyy <- stringr::str_sub(year, 1, 4)
yy <- stringr::str_sub(year, -2)
paste0(yyyy, "/", yy)
}
4 changes: 4 additions & 0 deletions dev/deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ rsconnect::deployApp(
"DESCRIPTION",
"app.R"
),
envVars = c(
"AZ_CONTAINER_INPUTS",
"AZ_STORAGE_EP"
),
lint = FALSE,
forceUpdate = TRUE
)
Loading