From 9a3a64823ce069c6329131da1a48fd724b212a23 Mon Sep 17 00:00:00 2001 From: Cam Race Date: Sat, 20 Jul 2024 09:39:20 +0100 Subject: [PATCH] tidying global file --- R/dashboard_variables.R | 0 R/dfe_resources.R | 14 --- R/dfe_template.R | 10 ++ .../example_tab_1.R} | 89 +---------------- R/ui_panels/user_guide.R | 82 ++++++++++++++++ R/utils.R | 14 +++ global.R | 95 +++++++++++-------- renv.lock | 15 +++ server.R | 4 +- .../test-Server-01-reactive_benchmark.R | 4 +- tests/testthat/test-UI-01-basic_load.R | 2 +- ui.R | 8 +- 12 files changed, 186 insertions(+), 151 deletions(-) create mode 100644 R/dashboard_variables.R delete mode 100644 R/dfe_resources.R rename R/{dashboard_panels.R => ui_panels/example_tab_1.R} (58%) create mode 100644 R/ui_panels/user_guide.R diff --git a/R/dashboard_variables.R b/R/dashboard_variables.R new file mode 100644 index 0000000..e69de29 diff --git a/R/dfe_resources.R b/R/dfe_resources.R deleted file mode 100644 index e4a33f8..0000000 --- a/R/dfe_resources.R +++ /dev/null @@ -1,14 +0,0 @@ -# Current GSS colours for use in charts. These are taken from the current -# guidance here: -# https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/ -# Note the advice on trying to keep to a maximum of 4 series in a single plot -# (the two commented out hex codes are for the suggested colours for two -# additional series if absolutely necessary). -# 2023-09-21: update made to change hex codes to AF colours package -# guidance here: https://best-practice-and-impact.github.io/afcolours/ - -suppressMessages( - gss_colour_pallette <- afcolours::af_colours("categorical", - colour_format = "hex", n = 6 - ) -) diff --git a/R/dfe_template.R b/R/dfe_template.R index 6aa87b9..cbb1d05 100644 --- a/R/dfe_template.R +++ b/R/dfe_template.R @@ -45,3 +45,13 @@ validate_color <- function(color) { paste(valid_colors, collapse = ", "), "." ) } + +# Current GSS colours for use in charts. These are taken from the current +# guidance here: +# https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/ +# Note the advice on trying to keep to a maximum of 4 series in a single plot +# AF colours package guidance here: https://best-practice-and-impact.github.io/afcolours/ + +suppressMessages( + gss_colour_pallette <- afcolours::af_colours("categorical", colour_format = "hex", n = 4) +) diff --git a/R/dashboard_panels.R b/R/ui_panels/example_tab_1.R similarity index 58% rename from R/dashboard_panels.R rename to R/ui_panels/example_tab_1.R index 1c132bd..eb5ea52 100644 --- a/R/dashboard_panels.R +++ b/R/ui_panels/example_tab_1.R @@ -1,91 +1,6 @@ -homepage_panel <- function() { +example_tab_1_panel <- function() { tabPanel( - "User guide and information", - gov_main_layout( - gov_row( - column( - 12, - h1("Department for Education (DfE) Analytical Services R-Shiny data dashboard template"), - br(), - br() - ), - - ## Left panel ------------------------------------------------------- - - column( - 6, - div( - div( - class = "panel panel-info", - div( - class = "panel-heading", - style = "color: white;font-size: 18px;font-style: bold; - background-color: #1d70b8;", - h2("Contents") - ), - div( - class = "panel-body", - tags$div( - h3("Introduction"), - p("This app demonstrates the DfE Analytical Services R-Shiny - data dashboard template."), - p("You might want to add some brief introductory text here - alongside some links to different tabs within your - dashboard. Here's an example of a link working:"), - p(actionLink("link_to_app_content_tab", "Dashboard panel")), - p("You need to add an observeEvent() function to the server.R - script for any link that navigates within your App.") - ), - br() - ) - ) - ), - ), - - ## Right panel ------------------------------------------------------ - - column( - 6, - div( - div( - class = "panel panel-info", - div( - class = "panel-heading", - style = "color: white;font-size: 18px;font-style: bold; - background-color: #1d70b8;", - h2("Background Info") - ), - div( - class = "panel-body", - h3("Context and purpose"), - p("This app is the DfE Analytical Service's R-Shiny template - demonstration app and is being developed to provide a coherent - styling for DfE dashboards alongside some useful example - componenets that teams can adapt for their own uses."), - p("DfE teams using this template should avoid changing the - styling and layout, keeping the header, footer and side - navigation list formats."), - p("You might want to add some relevant background information - for your users here. For example some useful links to your - Explore Education Statistics (EES) - publication, data sources and other relevant resources."), - h3("Guidance sources"), - p("For example, here we'll add some of the key resources we draw - on to guide styling and vizualisation...") - ) - ) - ) - ) - ) - ) - ) -} - - -dashboard_panel <- function() { - tabPanel( - value = "dashboard", - "Dashboard", + "Example tab 1", # Define UI for application that draws a histogram diff --git a/R/ui_panels/user_guide.R b/R/ui_panels/user_guide.R new file mode 100644 index 0000000..4a3ac8c --- /dev/null +++ b/R/ui_panels/user_guide.R @@ -0,0 +1,82 @@ +user_guide_panel <- function() { + tabPanel( + "User guide", + gov_main_layout( + gov_row( + column( + 12, + h1("Department for Education (DfE) Analytical Services R-Shiny data dashboard template"), + br(), + br() + ), + + ## Left panel ------------------------------------------------------- + + column( + 6, + div( + div( + class = "panel panel-info", + div( + class = "panel-heading", + style = "color: white;font-size: 18px;font-style: bold; + background-color: #1d70b8;", + h2("Contents") + ), + div( + class = "panel-body", + tags$div( + h3("Introduction"), + p("This app demonstrates the DfE Analytical Services R-Shiny + data dashboard template."), + p("You might want to add some brief introductory text here + alongside some links to different tabs within your + dashboard. Here's an example of a link working:"), + p(actionLink("link_to_app_content_tab", "Example tab 1")), + p("You need to add an observeEvent() function to the server.R + script for any link that navigates within your App.") + ), + br() + ) + ) + ), + ), + + ## Right panel ------------------------------------------------------ + + column( + 6, + div( + div( + class = "panel panel-info", + div( + class = "panel-heading", + style = "color: white;font-size: 18px;font-style: bold; + background-color: #1d70b8;", + h2("Background Info") + ), + div( + class = "panel-body", + h3("Context and purpose"), + p("This app is the DfE Analytical Service's R-Shiny template + demonstration app and is being developed to provide a coherent + styling for DfE dashboards alongside some useful example + componenets that teams can adapt for their own uses."), + p("DfE teams using this template should avoid changing the + styling and layout, keeping the header, footer and side + navigation list formats."), + p("You might want to add some relevant background information + for your users here. For example some useful links to your + Explore Education Statistics (EES) + publication, data sources and other relevant resources."), + h3("Guidance sources"), + p("For example, here we'll add some of the key resources we draw + on to guide styling and vizualisation...") + ) + ) + ) + ) + ) + ) + ) +} diff --git a/R/utils.R b/R/utils.R index 6df0d17..e293840 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1 +1,15 @@ # Add custom functions used across multiple places in the app into this script + +expandable <- function(input_id, label, contents) { + gov_details <- shiny::tags$details( + class = "govuk-details", id = input_id, + shiny::tags$summary( + class = "govuk-details__summary", + shiny::tags$span( + class = "govuk-details__summary-text", + label + ) + ), + shiny::tags$div(contents) + ) +} diff --git a/global.R b/global.R index 272855d..37cdc21 100644 --- a/global.R +++ b/global.R @@ -1,64 +1,94 @@ # ----------------------------------------------------------------------------- # This is the global file. +# # Use it to store functions, library calls, source files etc. -# Moving these out of the server file and into here improves performance -# The global file is run only once when the app launches and stays consistent +# +# Moving these out of the server file and into here improves performance as the +# global file is run only once when the app launches and stays consistent # across users whereas the server and UI files are constantly interacting and # responsive to user input. # # Library calls --------------------------------------------------------------- shhh <- suppressPackageStartupMessages # It's a library, so shhh! + +# Core shiny and R packages shhh(library(shiny)) +shhh(library(bslib)) +shhh(library(rstudioapi)) + +# Custom packages +shhh(library(dfeR)) +shhh(library(dfeshiny)) +shhh(library(shinyGovstyle)) + +# Creating charts and tables +shhh(library(ggplot2)) +shhh(library(DT)) + +# Data and string manipulation +shhh(library(dplyr)) +shhh(library(stringr)) +shhh(library(ggiraph)) + +# Shiny extensions shhh(library(shinyjs)) shhh(library(tools)) -shhh(library(testthat)) -shhh(library(stringr)) shhh(library(shinydashboard)) shhh(library(shinyWidgets)) -shhh(library(shinyGovstyle)) shhh(library(shinytitle)) -shhh(library(dplyr)) -shhh(library(ggplot2)) -shhh(library(DT)) shhh(library(xfun)) shhh(library(metathis)) shhh(library(shinyalert)) -shhh(library(shinytest2)) -shhh(library(rstudioapi)) -shhh(library(bslib)) -shhh(library(dfeshiny)) -shhh(library(ggiraph)) -# Enable bookmarking so that input choices are shown in the url --------------- -enableBookmarking("url") +# Dependencies needed for testing or CI but not for the app ------------------- +# Including them here keeps them in renv but avoids the app needlessly loading +# them, saving on load time. +if (FALSE) { + shhh(library(shinytest2)) + shhh(library(testthat)) +} # Source scripts -------------------------------------------------------------- # Source any scripts here. Scripts may be needed to process data before it gets -# to the server file. +# to the server file or to hold custom functions to keep the main files shorter +# # It's best to do this here instead of the server file, to improve performance. +# Source script for loading in data source("R/read_data.R") +# Source custom functions script +source("R/utils.R") + +# Source all files in the ui_panels folder +lapply(list.files("R/ui_panels/", full.names = TRUE), source) + # Set global variables -------------------------------------------------------- -site_title <- "Department for Education (DfE) Shiny Template" +site_title <- "Department for Education (DfE) Shiny Template" # name of app +parent_pub_name <- "Statistical publication" # name of source publication +parent_publication <- # link to source publication + "https://explore-education-statistics.service.gov.uk/find-statistics/apprenticeships" + +# Set the URLs that the site will be published to site_primary <- "https://department-for-education.shinyapps.io/dfe-shiny-template/" site_overflow <- "https://department-for-education.shinyapps.io/dfe-shiny-template-overflow/" +# Combine URLs into list for disconnect function # We can add further mirrors where necessary. Each one can generally handle # about 2,500 users simultaneously sites_list <- c(site_primary, site_overflow) -# Update this with your parent -# publication name (e.g. the EES publication) -ees_pub_name <- "Statistical publication" - -# Update with parent publication link, using apprenticeships as an example -ees_publication <- "https://explore-education-statistics.service.gov.uk/find-statistics/apprenticeships" +# Set the key for Google Analytics tracking google_analytics_key <- "Z967JJVQQX" -# Read in the data +# End of global variables ----------------------------------------------------- + +# Enable bookmarking so that input choices are shown in the url --------------- +enableBookmarking("url") + +# Read in the data ------------------------------------------------------------ df_revbal <- read_revenue_data() %>% mutate(school_phase = case_when( school_phase == "All LA maintained schools" ~ "All local authority maintained schools", @@ -74,6 +104,7 @@ df_areas <- df_revbal %>% ) %>% distinct() +## Extract lists for use in drop downs ---------------------------------------- choices_las <- df_areas %>% filter(geographic_level == "Local authority") %>% select(geographic_level, area_name = la_name) %>% @@ -89,20 +120,4 @@ choices_areas <- df_areas %>% ) %>% rbind(choices_las) -choices_years <- unique(df_revbal$time_period) - choices_phase <- unique(df_revbal$school_phase) - -expandable <- function(input_id, label, contents) { - gov_details <- shiny::tags$details( - class = "govuk-details", id = input_id, - shiny::tags$summary( - class = "govuk-details__summary", - shiny::tags$span( - class = "govuk-details__summary-text", - label - ) - ), - shiny::tags$div(contents) - ) -} diff --git a/renv.lock b/renv.lock index be083d4..5a5c667 100644 --- a/renv.lock +++ b/renv.lock @@ -418,6 +418,21 @@ ], "Hash": "99b79fcbd6c4d1ce087f5c5c758b384f" }, + "dfeR": { + "Package": "dfeR", + "Version": "0.3.0", + "Source": "GitHub", + "RemoteType": "github", + "RemoteUsername": "dfe-analytical-services", + "RemoteRepo": "dfeR", + "RemoteRef": "main", + "RemoteSha": "6f5aca112e8fa0f35541c4a987cea6222302f89d", + "RemoteHost": "api.github.com", + "Requirements": [ + "lifecycle" + ], + "Hash": "1ecf7c371ee428eebb9234de2d48c05a" + }, "dfeshiny": { "Package": "dfeshiny", "Version": "0.1.1", diff --git a/server.R b/server.R index 179632a..3821b0f 100644 --- a/server.R +++ b/server.R @@ -48,7 +48,7 @@ server <- function(input, output, session) { }) observe({ - if (input$navlistPanel == "dashboard") { + if (input$navlistPanel == "Example tab 1") { change_window_title( session, paste0( @@ -414,7 +414,7 @@ server <- function(input, output, session) { observeEvent(input$link_to_app_content_tab, { - updateTabsetPanel(session, "navlistPanel", selected = "dashboard") + updateTabsetPanel(session, "navlistPanel", selected = "Example tab 1") }) # Download the underlying data button diff --git a/tests/testthat/test-Server-01-reactive_benchmark.R b/tests/testthat/test-Server-01-reactive_benchmark.R index f1615fb..7c42c5d 100644 --- a/tests/testthat/test-Server-01-reactive_benchmark.R +++ b/tests/testthat/test-Server-01-reactive_benchmark.R @@ -15,7 +15,7 @@ testServer(expr = { # Give the inputs expected on initial load session$setInputs( - navlistPanel = "dashboard", + navlistPanel = "Example tab 1", region = "England", selectPhase = "All local authority maintained schools", tabsetpanels = "Benchmark example", @@ -30,14 +30,12 @@ testServer(expr = { # Change to a different dropdown selection session$setInputs( - navlistPanel = "dashboard", region = "North East", selectPhase = "Secondary", tabsetpanels = "Benchmarking example", selectBenchLAs = c("Barnsley", "Doncaster") ) - # Check the reactive benchmark table is a valid number # not currently running expect_true(grepl("^\\d*$", reactive_benchmark())) diff --git a/tests/testthat/test-UI-01-basic_load.R b/tests/testthat/test-UI-01-basic_load.R index 789c726..5125d7a 100644 --- a/tests/testthat/test-UI-01-basic_load.R +++ b/tests/testthat/test-UI-01-basic_load.R @@ -28,7 +28,7 @@ app$wait_for_idle(5) test_that("App loads and title of app appears as expected", { expect_equal( app$get_text("h1")[1], - # This matches what is set in dashboard_panels.R for the first panel + # This matches what is set in example_tab_1.R for the first panel "Overall content title for this dashboard page" ) }) diff --git a/ui.R b/ui.R index 4128da6..c8d1f3c 100644 --- a/ui.R +++ b/ui.R @@ -60,8 +60,8 @@ ui <- function(input, output, session) { # Variables used here are dset in the global.R file dfeshiny::custom_disconnect_message( links = sites_list, - publication_name = ees_pub_name, - publication_link = ees_publication + publication_name = parent_pub_name, + publication_link = parent_publication ), useShinydashboard(), # Setting up cookie consent based on a cookie recording the consent: @@ -110,8 +110,8 @@ ui <- function(input, output, session) { id = "navlistPanel", widths = c(2, 8), well = FALSE, - dashboard_panel(), - homepage_panel(), + example_tab_1_panel(), + user_guide_panel(), a11y_panel(), support_panel( team_email = "explore.statistics@education.gov.uk",