Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shiny best practice #120

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

christopherdias11
Copy link

Overview of changes

This adds more to the Learning R Shiny section.

Why are these changes being made?

As part of work in a Dashboards working group we are looking to promote consistent approaches to R Shiny dashboards across the DfE. This aims to give:

  • Resources on getting started
  • Best practice guidance
  • Resources for more advanced Shiny techniques

Detailed description of changes

  • Adding a section on different packages for rendering tables to learning-development/r.qmd

The following changes to writing-visualising/dashboards_rshiny.qmd:

  • Added a section to Creating a user interface to advise people how to construct a template and to use bslib
  • Section on R Shiny best practice giving a checklist of things a developer should strive to do in their project
  • Section on using databases with R Shiny for optimal performance and security
  • Section on Leveling up your shiny to give some more advanced techniques and reading material

To check:

  • Links work
  • You agree with the materials suggested
  • Do you have any other suggestions on material/structure?

Issue ticket number/s and link

Issue #112

Checklist before requesting a review

  • I have checked the contributing guidelines
  • I have checked for and linked any relevant issues that this may resolve
  • I have checked that these changes build locally
  • I understand that if merged into main, these changes will be publicly available

Copy link
Contributor

@jen-machin jen-machin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some irritating nitpicky comments from me - apologies. Thanks for all your help on this!


---

There a lots of options for rendering tables in Quarto, RMarkdown and R Shiny, many of which are covered in [this blog about beautiful tables](https://rfortherestofus.com/2019/11/how-to-make-beautiful-tables-in-r/). Think about the user needs in the first instance and go from there - there's compelling arguments for each package mentioned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be "there are" at the start of the paragraph

There are a lot of resources already available to support you when working with R Shiny, if you're new, then this [blog post](https://www.r-bloggers.com/2021/03/a-gentle-introduction-to-r-shiny/) provides a gentle introduction.

If you like learning by reading/doing then the [Posit Shiny-specific site](https://shiny.posit.co/r/getstarted/shiny-basics/lesson1/index.html) has lots of resources that you can work through. Likewise [Introduction section of Mastering Shiny](https://mastering-shiny.org/basic-intro.html) is also a helpful resources.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for accessibility reasons we should try and have spaces before and after /

e.g. reading / doing

Our [template shiny app repository](https://github.com/dfe-analytical-services/shiny-template) is a useful starting point for all public facing dashboards in the department. Please see the [dashboard template section](#dfe-dashboard-template) for guidance on what the dashboard template includes and how to use it.

For more advanced shiny knowledge it's worth taking a look at the guide to [engineering production-grade shiny apps](https://engineering-shiny.org/).
[Shiny layouts](https://shiny.posit.co/r/layouts/) gives a high level idea of how to structure an R Shiny app. Alongside this it is recommended you take a look at [`bslib`](https://rstudio.github.io/bslib/index.html) which makes constructing beautiful user interface easy. This site is full of information on how to construct the page and how to work themeing. If you like watching it done then [this video](https://www.youtube.com/watch?v=3l3w2p1Fy4M) is a good place to start.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "interfaces", not "interface"? Sorry, nitpicky comment

- Your code should be [tested](#code-testing)
- Your file structure should be [well-organised](https://mastering-shiny.org/best-practices.html#code-organization)
- You should use [dependency management](https://mastering-shiny.org/best-practices.html#dependency-management)
- If using [databases](#databases-with-shiny), you should use the pool package and ensure you are careful of SQL Injection
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we have much info on SQL injection - worth adding an explainer sentence here maybe so people know what it is? Either here or in the actual section itself

}
```

### Levelling up your shiny
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shiny should have capital S here (sorry)


For improving your **tables**, check out [this section](../learning-development/r.html#rendering-tables).

For more advanced shiny knowledge it's worth taking a look at the guide to [engineering production-grade shiny apps](https://engineering-shiny.org/). It's worth reading and considering how much to adopt, remember that just because it sounds clever doesn't make it easy for other analysts to pick up. Read the [Don't sacrifice readability](https://engineering-shiny.org/need-for-optimization.html#dont-sacrifice-readability) section and then work from there.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capital S twice here

- Going beyond DfE there's a wealth of resources and communities online, including [Stack Overflow](https://stackoverflow.com/) discussions, cross government slack channels (e.g. #R and #Shiny on govdatascience.slack.com) and even tweets about [R Shiny on twitter](https://twitter.com/search?q=rshiny&src=spelling_expansion_revert_click).

---

# DfE styling and the DfE R Shiny template
## DfE styling and the DfE R Shiny template

---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Level 2 headings don't need --- under them so this one can be removed 😄


---

Our [template shiny app repository](https://github.com/dfe-analytical-services/shiny-template) should be used a starting point for all public facing dashboards as it gives a consistent set up for publishing and provides template code for common parts. You can start your own repository using the 'Use this template' green button on the template repository. You will need a GitHub account to do this. If you are unsure or need help, contact the Statistics Development team who will be able to walk you through this.

---

## What's in the template
### What's in the template
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Level 3 headings DO need --- underneath

Several more of these below with the same issue 😄


In terms of colour choices beyond charts, teams should pick from the [gov.uk design system colours](https://design-system.service.gov.uk/styles/colour/) when creating dashboards. Colours for the charts are pulled in using the [afcolours package](https://best-practice-and-impact.github.io/afcolours/), which is based on the [GSS colours guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/). These colours can be reused elsewhere if you need to match colours with those in the charts.

As with charts, be careful when choosing colours, and make sure that colours have sufficient contrast to be placed next to each other to meet WCAG AA standards using the [colour contrast checker](https://webaim.org/resources/contrastchecker/).

---

# Hosting dashboard code on GitHub
## Hosting dashboard code on GitHub

---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No --- here

@@ -170,7 +282,7 @@ Given the above, all analysts intending to create a dashboard should ensure they

---

# Publishing your dashboard
## Publishing your dashboard

---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and no --- here


---

Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage, embed them in [R Markdown documents](http://rmarkdown.rstudio.com/) or build [dashboards](http://rstudio.github.io/shinydashboard/). You can also extend your Shiny apps with [CSS themes](http://rstudio.github.io/shinythemes/), [htmlwidgets](http://www.htmlwidgets.org/), and [JavaScript actions](https://github.com/daattali/shinyjs/blob/master/README.md).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this link? Is it outdated?

Our [template shiny app repository](https://github.com/dfe-analytical-services/shiny-template) is a useful starting point for all public facing dashboards in the department. Please see the [dashboard template section](#dfe-dashboard-template) for guidance on what the dashboard template includes and how to use it.

For more advanced shiny knowledge it's worth taking a look at the guide to [engineering production-grade shiny apps](https://engineering-shiny.org/).
[Shiny layouts](https://shiny.posit.co/r/layouts/) gives a high level idea of how to structure an R Shiny app. Alongside this it is recommended you take a look at [`bslib`](https://rstudio.github.io/bslib/index.html) which makes constructing beautiful user interface easy. This site is full of information on how to construct the page and how to work themeing. If you like watching it done then [this video](https://www.youtube.com/watch?v=3l3w2p1Fy4M) is a good place to start.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get this when i try the video link:
image

[Shiny layouts](https://shiny.posit.co/r/layouts/) gives a high level idea of how to structure an R Shiny app. Alongside this it is recommended you take a look at [`bslib`](https://rstudio.github.io/bslib/index.html) which makes constructing beautiful user interface easy. This site is full of information on how to construct the page and how to work themeing. If you like watching it done then [this video](https://www.youtube.com/watch?v=3l3w2p1Fy4M) is a good place to start.

:::{.callout-warning}
Previously a lot of dashboards used `shinydashboard` - if starting afresh we **recommend `bslib` instead**. `shinydashboard` is uses an old version of bootstrap and has not been updated in many years. It shouldn't immediately break but a risk nonetheless.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmbielby to check messaging - I know the dfeshiny package uses bslib now so do we want to use stronger language to push towards bslib?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that level of pushing bslib is fine.


### Databases with Shiny

Currently only internal dashboards can have connectivity to databases. Check out [this data article from Posit](https://solutions.posit.co/connections/db/best-practices/) on best practice - in particular you should look to use the [`pool`](https://rstudio.github.io/pool/) package for database connections and be careful of [SQL Injection](https://solutions.posit.co/connections/db/best-practices/run-queries-safely/).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never seen/used Pool before, should we be adding this to workshops/other guidance @rmbielby @cjrace ? One for guide to the galaxy?

Finally, you should seek to make use of the community that is already out there, see what others are doing, ask them questions and for advice on any decisions or problems that you're facing, and share what it is that you're doing.
- The Statistics Development Team are experienced with R Shiny and happy to help or offer advice, and there is the [DfE R community](https://teams.microsoft.com/l/team/19%3a311ec2e4d46b4dd38f0d61f05fb93383%40thread.skype/conversations?groupId=b95c605d-8fbc-4e4d-9a76-2f7d1c55e70a&tenantId=fad277c9-c60a-4da1-b5f3-b3b8b34a82f9) on teams.
- There is a bank of [tips and tricks](https://teams.microsoft.com/l/entity/com.microsoft.teamspace.tab.wiki/tab::d9f50997-7180-407d-baf9-47f9ec9d54a3?context=%7B%22subEntityId%22%3A%22%7B%5C%22pageId%5C%22%3A21%2C%5C%22origin%5C%22%3A2%7D%22%2C%22channelId%22%3A%2219%3A311ec2e4d46b4dd38f0d61f05fb93383%40thread.skype%22%7D&tenantId=fad277c9-c60a-4da1-b5f3-b3b8b34a82f9){target="_blank" rel="noopener noreferrer"} stored in the DfE R teams area too.
- The Statistics Development Team are experienced with R Shiny and happy to help or offer advice;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make this a mailto:statistics.development@education.gov.uk?

If you like learning by watching then the [Posit Youtube channel](https://www.youtube.com/@PositPBC/videos) is also full of good resources.

### Creating a user interface

Our [template shiny app repository](https://github.com/dfe-analytical-services/shiny-template) is a useful starting point for all public facing dashboards in the department. Please see the [dashboard template section](#dfe-dashboard-template) for guidance on what the dashboard template includes and how to use it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chris didn't do this but the #dfe-dashboard-template link isn't working (think it needs changing to #dfe-styling-and-the-dfe-r-shiny-template ???)

HTML, CSS and Bootstrap are a slippery slope into web development. Remember that if you're working with Shiny then good analysis comes first!
:::

For **interactive maps**, check out [this section](../learning-development/r.html#example-shiny-app-with-map).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'this section' isn't great link text for accessibility, could you make it more like 'this section on interactive maps' ?


For **interactive maps**, check out [this section](../learning-development/r.html#example-shiny-app-with-map).

For improving your **tables**, check out [this section](../learning-development/r.html#rendering-tables).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above


---

Our [template shiny app repository](https://github.com/dfe-analytical-services/shiny-template) should be used a starting point for all public facing dashboards as it gives a consistent set up for publishing and provides template code for common parts. You can start your own repository using the 'Use this template' green button on the template repository. You will need a GitHub account to do this. If you are unsure or need help, contact the Statistics Development team who will be able to walk you through this.

---

## What's in the template
### What's in the template

The template provides code for a basic interactive dashboard, with an example line chart using drop-downs for education level and geographic levels. It also includes an example accessibility statement (this is a requirement for all public dashboards and must be filled in, see the [Accessibility](#accessibility) and [Accessibility testing](#accessibility-testing) sections earlier in this guidance), and a 'Support and feedback' page with suggested contact information and links.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, Chris didn't add this text but the section links to #accessibility and accessibility-testing are outdated/not working

- [Functions with Shiny](https://mastering-shiny.org/scaling-functions.html) make your life much easier


### Databases with Shiny
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be keen to have this section down in Dashboards and data section rather than up here. Wary that we'll end up with repetition or conflict having this as a separate thing.


[Mastering Shiny](https://mastering-shiny.org/index.html) is a really useful read, especially the [Best Practice](https://mastering-shiny.org/scaling-intro.html) section.

There is a plethora of [awesome shiny extensions](https://github.com/nanxstats/awesome-shiny-extensions) to explore. Keep it accessible, use packages from CRAN/ones you trust and the world is your oyster.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not keen on pointing analysts to extra extensions as standard, without us first verifying that they are definitely built with accessibility and continuity of service in mind. Too many dashboards already use shinyWidgets, which are a complete crapshow in terms of accessibility.

[Shiny layouts](https://shiny.posit.co/r/layouts/) gives a high level idea of how to structure an R Shiny app. Alongside this it is recommended you take a look at [`bslib`](https://rstudio.github.io/bslib/index.html) which makes constructing beautiful user interface easy. This site is full of information on how to construct the page and how to work themeing. If you like watching it done then [this video](https://www.youtube.com/watch?v=3l3w2p1Fy4M) is a good place to start.

:::{.callout-warning}
Previously a lot of dashboards used `shinydashboard` - if starting afresh we **recommend `bslib` instead**. `shinydashboard` is uses an old version of bootstrap and has not been updated in many years. It shouldn't immediately break but a risk nonetheless.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that level of pushing bslib is fine.

}
}
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're going to want a section on caching using bindCache here as well, but happy for that to be a separate PR and to write it.


For improving your **tables**, check out [this section](../learning-development/r.html#rendering-tables).

For more advanced shiny knowledge it's worth taking a look at the guide to [engineering production-grade shiny apps](https://engineering-shiny.org/). It's worth reading and considering how much to adopt, remember that just because it sounds clever doesn't make it easy for other analysts to pick up. Read the [Don't sacrifice readability](https://engineering-shiny.org/need-for-optimization.html#dont-sacrifice-readability) section and then work from there.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a big fan of the "just because it sounds clever, doesn't make it easy for other analysts to pick up" line. I'd be tempted to add a rephrasing of this right near the start of this page in a call-out box (could be any of note, tip, warning or danger...).

Maybe something along the lines of: "Just because something looks clever, doesn't mean you should do it. Don't over-complicate your user interface or the underlying code. Your priorities should be making something that meets user's needs, whilst also creating something that's manageable for whoever follows you in maintaining your dashboard."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants