Introduction to Shiny

Session - Introduction

Zoë Turner

Agenda - session one (about 2 hours)

Chapter overview

Introduction and getting started

What is Shiny?

——— Break ———–

What does it do?

Intro to reactivity

Application 1 - minimal (Your first application)
Application 2 - Sitrep (Situation Report)

Reactive expressions

(Optional)
NSE in Shiny applications
Bookmarks

——— Course end ———–

Agenda - session two (about 2 hours)

Chapter overview

——— Course end ———–

Agenda - session three (about 2 hours)

Chapter overview

——— Course end ———–

What is Shiny?

  • Shiny is a web application programming framework
  • The language of the web (HTML, JavaScript, and CSS) all called from R
  • It requires no knowledge of HTML, JavaScript, or CSS, but if you have that knowledge you can add more of these languages

Let’s create a shiny app - on a computer

  • Go to File > New Project... > New Directory
  • Select Shiny Application
  • Browse to the location you want the new folder to go to
  • Name your new project!

Projects

  • Using projects is a good habit to get into
  • Creating a specific project through the RStudio provides all the relevant documents needed

Let’s create a shiny template app - in Posit Cloud

  • Go to File > New File > Shiny Web App...
  • Or use the short menu below File
  • Name your new project!

Posit Cloud workspaces are all projects!

  • Because the Cloud is a project it doesn’t need creating
  • What we do here you can also do on your own computer

Running the app

Screenshot of the RStudio pane under Tools and Help with the Run app icon and words

Changing the inputs (from the slider) changes the outputs (the chart)

Viewing as a pop-up… or not

You can change whether the app opens in a separate window in RStudio, the RStudio viewer pane, or externally in your web browser. Use whichever works best for you and your computer setup.

Listening?

Before closing the app look at the R Console:

#> Listening on http://127.0.0.1:3827

127.0.0.1 “this computer” and 3827 is a randomly assigned port number.

R is busy!

  • The R prompt isn’t visible
  • The console toolbar has a stop sign icon
  • No other commands can be used until the Shiny app is stopped!
  • To stop click the stop sign, Esc or Ctrl + C in some programs

Developing a Shiny app

The workflow is:

  • Write code
  • Start app
  • Play with app
  • Write more code
  • Repeat
  • And in RStudio you don’t need to stop and start just Reload app

Examples of Shiny

The Shiny Gallery which includes dashboards as well as parts (widgets) that can be added.

And from healthcare:

Screenshot of the Indices of Deprivation 2019 dashboard from Trafford Data lab

Mental Health Surge Model app

Screenshot of Mental Health Modelling app from The Strategy Unit used to estimate the impacts of COVID-19 on mental health services in England

Patient Feedback text mining app

Screenshot of Patient Text Mining app from Nottinghamshire Healthcare NHS Foundation Trust (via funding from NHS England)

The future of Shiny - is here!

As RStudio renamed to Posit in 2022 to extend their language association beyond R, so they are also working on extending Shiny apps

  • To be compatible with the power of Python
  • To publish without a server using Shinylive
  • An example is this app that converts a table to Govspeak Markdown, the format required for publishing reports by the UK’s government from Matt Dray’s blog

End session

Acknowledgements

Mastering Shiny by Hadley Wickham
Beginners course by Chris Beeley aimed at relatively novice R users
Shiny workshop for NHS-R Community
Shiny workshop materials on the NHS-R Community GitHub. Building Web Apps with R Shiny from PsyTeachR