generated from dfe-analytical-services/shiny-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New helper function to add css to UI * Removed shiny-template cookie logic and replaced with dfeshiny cookie functions. Added cookie page to app. Removed use_shinydashboard. * renv updated
- Loading branch information
Showing
5 changed files
with
238 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,110 @@ | ||
# ----------------------------------------------------------------------------- | ||
# This is the helper file, filled with lots of helpful functions! | ||
# | ||
# It is commonly used as an R script to store custom functions used through the | ||
# app to keep the rest of the app code easier to read. | ||
# ----------------------------------------------------------------------------- | ||
|
||
# Expandable function --------------------------------------------------------- | ||
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) | ||
) | ||
} | ||
|
||
# Value box function ---------------------------------------------------------- | ||
# fontsize: can be small, medium or large | ||
value_box <- function(value, subtitle, icon = NULL, | ||
color = "blue", width = 4, | ||
href = NULL, fontsize = "medium") { | ||
validate_color(color) | ||
if (!is.null(icon)) tagAssert(icon, type = "i") | ||
|
||
box_content <- div( | ||
class = paste0("small-box bg-", color), | ||
div( | ||
class = "inner", | ||
p(value, id = paste0("vboxhead-", fontsize)), | ||
p(subtitle, id = paste0("vboxdetail-", fontsize)) | ||
), | ||
if (!is.null(icon)) div(class = "icon-large", icon) | ||
) | ||
|
||
if (!is.null(href)) { | ||
box_content <- a(href = href, box_content) | ||
} | ||
|
||
div( | ||
class = if (!is.null(width)) paste0("col-sm-", width), | ||
box_content | ||
) | ||
} | ||
|
||
# Valid colours for value box ------------------------------------------------- | ||
valid_colors <- c("blue", "dark-blue", "green", "orange", "purple", "white") | ||
|
||
# Validate that only valid colours are used ----------------------------------- | ||
validate_color <- function(color) { | ||
if (color %in% valid_colors) { | ||
return(TRUE) | ||
} | ||
|
||
stop( | ||
"Invalid color: ", color, ". Valid colors are: ", | ||
paste(valid_colors, collapse = ", "), "." | ||
) | ||
} | ||
|
||
# GSS colours ----------------------------------------------------------------- | ||
# 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) | ||
) | ||
# ----------------------------------------------------------------------------- | ||
# This is the helper file, filled with lots of helpful functions! | ||
# | ||
# It is commonly used as an R script to store custom functions used through the | ||
# app to keep the rest of the app code easier to read. | ||
# ----------------------------------------------------------------------------- | ||
|
||
# Expandable function --------------------------------------------------------- | ||
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) | ||
) | ||
} | ||
|
||
# Value box function ---------------------------------------------------------- | ||
# fontsize: can be small, medium or large | ||
value_box <- function(value, subtitle, icon = NULL, | ||
color = "blue", width = 4, | ||
href = NULL, fontsize = "medium") { | ||
validate_color(color) | ||
if (!is.null(icon)) tagAssert(icon, type = "i") | ||
|
||
box_content <- div( | ||
class = paste0("small-box bg-", color), | ||
div( | ||
class = "inner", | ||
p(value, id = paste0("vboxhead-", fontsize)), | ||
p(subtitle, id = paste0("vboxdetail-", fontsize)) | ||
), | ||
if (!is.null(icon)) div(class = "icon-large", icon) | ||
) | ||
|
||
if (!is.null(href)) { | ||
box_content <- a(href = href, box_content) | ||
} | ||
|
||
div( | ||
class = if (!is.null(width)) paste0("col-sm-", width), | ||
box_content | ||
) | ||
} | ||
|
||
# Valid colours for value box ------------------------------------------------- | ||
valid_colors <- c("blue", "dark-blue", "green", "orange", "purple", "white") | ||
|
||
# Validate that only valid colours are used ----------------------------------- | ||
validate_color <- function(color) { | ||
if (color %in% valid_colors) { | ||
return(TRUE) | ||
} | ||
|
||
stop( | ||
"Invalid color: ", color, ". Valid colors are: ", | ||
paste(valid_colors, collapse = ", "), "." | ||
) | ||
} | ||
|
||
# GSS colours ----------------------------------------------------------------- | ||
# 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) | ||
) | ||
|
||
|
||
#' Set CSS Style Sheet -------------------------------------------------------- | ||
#' | ||
#' This function generates an HTML `head` tag that includes a | ||
#' link to a specified CSS stylesheet. | ||
#' It can be used to dynamically set or include a CSS file in a Shiny | ||
#' application or any other HTML-based interface that utilizes R. | ||
#' | ||
#' @param css_filename A character string specifying the path or | ||
#' URL to the CSS file. | ||
#' This should be a relative or absolute path to a `.css` file or a URL | ||
#' pointing to an external stylesheet. | ||
#' | ||
#' @return A `tags$head` object containing a `link` tag that references | ||
#' the specified CSS file. | ||
#' This object can be directly included in the UI definition of a | ||
#' Shiny application. | ||
#' | ||
#' @details | ||
#' When included in the UI of a Shiny app, it instructs the web browser | ||
#' to load and apply the specified CSS stylesheet. | ||
#' | ||
#' This function is useful when you need to modularise the | ||
#' inclusion of stylesheets, especially in applications where the CSS file | ||
#' might change dynamically or needs to be set programmatically. | ||
#' | ||
set_css_style_sheet <- function(css_filename) { | ||
tags$head( | ||
tags$link( | ||
rel = "stylesheet", | ||
type = "text/css", | ||
href = css_filename | ||
) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.