Skip to content

Commit

Permalink
Merge pull request #3 from dfe-analytical-services/add-disconnect-mes…
Browse files Browse the repository at this point in the history
…sage

Add custom disconnect message function
  • Loading branch information
chfoster authored Mar 27, 2024
2 parents d22732b + c36226a commit 207aeb8
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 4 deletions.
6 changes: 6 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ Version: 0.1.1
Authors@R: c(
person("Rich", "Bielby", , "richard.bielby@education.gov.uk", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9070-9969")),
person("Charlotte", "Foster", , "charlotte.foster@education.gov.uk", role = "aut"),
person("Cam", "Race", , "cameron.race@education.gov.uk", role = "aut")
)
Description: R package containing preferred methods for creating official
DfE R-Shiny dashboards.
License: GPL (>= 3)
Imports:
checkmate,
glue,
htmltools,
RCurl,
shiny,
shinyGovstyle,
shinyjs,
stringr,
styler
Suggests:
diffviewer,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

export(cookie_banner_server)
export(cookie_banner_ui)
export(custom_disconnect_message)
export(dfe_cookie_script)
export(support_panel)
export(tidy_code)
importFrom(htmltools,tagList)
importFrom(htmltools,tags)
143 changes: 143 additions & 0 deletions R/custom_disconnect_message.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#' Custom disconnect message
#'
#'
#' @description
#' Create the html overlay panel to appear when RSConnect disconnects
#'
#' @param refresh the text to appear that will refresh the page when clicked
#' @param links A list of mirrors or alternative links to the dashboard
#' @param publication_name The parent publication name
#' @param publication_link The link to the publication on Explore Education
#' Statistics
#'
#' @importFrom htmltools tags tagList
#'
#'
#' @return A html overlay panel that appears when RSConnect disconnects for a
#' public R Shiny dashboard in DfE
#' @export
#'
#' @examples
#' custom_disconnect_message(
#' refresh = "Refresh page",
#' links = c(
#' "https://department-for-education.shinyapps.io/dfe-shiny-template/",
#' "https://department-for-education.shinyapps.io/dfe-shiny-template-overflow/" # nolint: [line_length_linter]
#' ),
#' publication_name = "Explore Education Statistics Publication",
#' publication_link =
#' "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter]
#' )
#'
custom_disconnect_message <- function(refresh = "Refresh page",
links = NULL,
publication_name = NULL,
publication_link = NULL) {
# Check links are valid

is_valid_sites_list <- function(sites) {
lapply(
stringr::str_trim(sites), startsWith,
"https://department-for-education.shinyapps.io/"
)
}

if (FALSE %in% is_valid_sites_list(links) ||
"https://department-for-education.shinyapps.io/" %in% links) { # nolint: [indentation_linter]
stop("You have entered an invalid site link in the links argument.")
}

pub_prefix <- c(
"https://explore-education-statistics.service.gov.uk/find-statistics/",
"https://www.explore-education-statistics.service.gov.uk/find-statistics/",
"https://www.gov.uk/",
"https://gov.uk/"
)

is_valid_publication_link <- function(link) {
startsWith(stringr::str_trim(link), pub_prefix)
}

if (RCurl::url.exists(publication_link) == FALSE ||
(TRUE %in% is_valid_publication_link(publication_link)) == FALSE || # nolint: [indentation_linter]
publication_link %in% pub_prefix) {
stop("You have entered an invalid publication link in the publication_link
argument.")
}

checkmate::assert_string(refresh)
tagList(
tags$script(
paste0(
"$(function() {",
" $(document).on('shiny:disconnected', function(event) {",
" $('#custom-disconnect-dialog').show();",
" $('#ss-overlay').show();",
" })",
"});"
)
),
tags$div(
id = "custom-disconnect-dialog",
style = "display: none !important;",
tags$div(
id = "ss-connect-refresh",
tags$p("You've lost connection to the dashboard server - please try
refreshing the page:"),
tags$p(tags$a(
id = "ss-reload-link",
href = "#", "Refresh page",
onclick = "window.location.reload(true);"
)),
if (length(links) > 1) {
tags$p(
"If this persists, you can also view the dashboard at one of our
mirror sites:",
tags$p(
tags$a(href = links[1], "Site 1"),
" - ",
tags$a(href = links[2], "Site 2"),
if (length(links) == 3) {
"-"
},
if (length(links) == 3) {
tags$a(href = links[3], "Site 3")
}
)
)
},
if (!is.null(publication_name)) {
tags$p(
"All the data used in this dashboard can also be viewed or
downloaded via the ",
tags$a(
href = publication_link,
publication_name
),
"on Explore Education Statistics."
)
},
tags$p(
"Please contact",
tags$a(
href = "mailto:statistics.development@education.gov.uk",
"statistics.development@education.gov.uk"
),
"with details of any problems with this resource."
)
)
),
tags$div(id = "ss-overlay", style = "display: none;"),
tags$head(htmltools::tags$style(
glue::glue(
.open = "{{", .close = "}}",
"#custom-disconnect-dialog a {
display: {{ if (refresh == '') 'none' else 'inline' }} !important;
color: #1d70b8 !important;
font-size: 16px !important;
font-weight: normal !important;
}"
)
))
)
}
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ script as this will provide
users with the necessary explanatory text on how we use cookies and the ability
to change their decision on whether or not to accept the use of cookies.

### Adding a custom disconect message to your dashboard

dfeshiny provides a function to add a custom disconnect message to your dashboard - this will appear when a dashboard would otherwise 'grey-screen' and will include options to refresh the page, go to overflow sites or visit the publication directly on Explore education statistics.

The following parameters should be defined and up-to-date in the global.R script:

- sites_list
- ees_pub_name
- ees_publication

To include a custom disconnect message, you should insert the following line into the ui.R script:

```
custom_disconnect_message(
links = sites_list,
publication_name = ees_pub_name,
publication_link = ees_publication
),
```

Putting this on the lines *just before* the `shinyGovstyle::header(...)` line
should work well.


## Contributing

Try and make use of the [usethis](https://usethis.r-lib.org/) package wherever possible.
Expand Down
37 changes: 37 additions & 0 deletions man/custom_disconnect_message.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/test_dashboard/server.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
server <- function(input, output, session) {
output$cookie_status <- cookie_banner_server(
"cookies",
input_cookies = reactive(input$cookies),
input_clear = reactive(input$cookie_consent_clear),
input_cookies = shiny::reactive(input$cookies),
input_clear = shiny::reactive(input$cookie_consent_clear),
parent_session = session,
google_analytics_key = google_analytics_key
google_analytics_key = google_analytics_key # # nolint: [object_usage_linter]
)
}
11 changes: 10 additions & 1 deletion tests/test_dashboard/ui.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
ui <- function(input, output, session) {
fluidPage(
shiny::fluidPage(
shinyjs::useShinyjs(),
custom_disconnect_message(
refresh = "Refresh page",
links = c(
"https://department-for-education.shinyapps.io/dfe-shiny-template/",
"https://department-for-education.shinyapps.io/dfe-shiny-template-overflow/" # nolint: [line_length_linter]
),
publication_name = "Explore Education Statistics Publication",
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter]
),
dfe_cookie_script(),
cookie_banner_ui("cookies"),
shiny::navlistPanel(
Expand Down
122 changes: 122 additions & 0 deletions tests/testthat/test-custom_disconnect_message.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
test_that("publication link is valid", {
# Test that an EES publication link passes
expect_no_error(
custom_disconnect_message(
refresh = "Refresh page",
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter]
publication_name = "Pupil attendance in schools",
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter]
)
)

# Test that a gov.uk publication link passes
expect_no_error(
custom_disconnect_message(
refresh = "Refresh page",
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter]
publication_name = "Pupil attendance in schools",
publication_link = "https://www.gov.uk/government/collections/job-and-skills-data" # nolint: [line_length_linter]
)
)

# Test that it fails for non-existent links/typos/random links
expect_error(
custom_disconnect_message(
refresh = "Refresh page",
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter]
publication_name = "Pupil attendance in schools",
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/hello" # nolint: [line_length_linter]
)
)

expect_error(
custom_disconnect_message(
refresh = "Refresh page",
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter]
publication_name = "Pupil attendance in schools",
publication_link = "https://www.bbc.com/news"
)
)

# Test that just linking to EES homepage fails

expect_error(
custom_disconnect_message(
refresh = "Refresh page",
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter]
publication_name = "Pupil attendance in schools",
publication_link = "https://explore-education-statistics.service.gov.uk/" # nolint: [line_length_linter]
)
)
})

test_that("site links are valid", {
# Test that valid long shinyapps.io link pass
expect_no_error(
custom_disconnect_message(
refresh = "Refresh page",
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter]
publication_name = "Pupil attendance in schools",
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter]
)
)

# Test that valid short shinyapps.io link pass
expect_no_error(
custom_disconnect_message(
refresh = "Refresh page",
links = "https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter]
publication_name = "Pupil attendance in schools",
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter]
)
)

# Test that a list of valid shinyapps.io links pass
expect_no_error(
custom_disconnect_message(
refresh = "Refresh page",
links = c(
"https://department-for-education.shinyapps.io/dfe-shiny-template/", # nolint: [line_length_linter]
"https://department-for-education.shinyapps.io/dfe-shiny-template-overflow/", # nolint: [line_length_linter]
"https://department-for-education.shinyapps.io/dfe-shiny-template/" # nolint: [line_length_linter]
),
publication_name = "Pupil attendance in schools",
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter]
)
)

# Test that it fails for non-shinyapps.io links
expect_error(
custom_disconnect_message(
refresh = "Refresh page",
links = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools", # nolint: [line_length_linter]
publication_name = "Pupil attendance in schools",
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter]
)
)

expect_error(
custom_disconnect_message(
refresh = "Refresh page",
links = c(
"https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools", # nolint: [line_length_linter]
"https://department-for-education.shinyapps.io/dfe-shiny-template/" # nolint: [line_length_linter]
),
publication_name = "Pupil attendance in schools",
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter]
)
)

# Test that it fails for a link to shinyapps.io with no suffix
expect_error(
custom_disconnect_message(
refresh = "Refresh page",
links = c(
"https://department-for-education.shinyapps.io/dfe-shiny-template/",
"https://department-for-education.shinyapps.io/"
),
publication_name = "Pupil attendance in schools",
publication_link = "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-attendance-in-schools" # nolint: [line_length_linter]
)
)
})

0 comments on commit 207aeb8

Please sign in to comment.