6.4 Building a simple shiny app

Shiny sticker
Shiny sticker

Shiny is web application framework for R that supports interactive apps and helps us deploy our R code in an accessible way. In particular it supports interactive data exploration. This user interaction is via its reactivity functionality such that elements in the app are updated whenever the user modifies an option. This is all provided by the {shiny} package.

Shiny offers a powerful framework to quickly build interactive data analysis apps ao that the developer need ot get involved in the details of html, CSS and javascript that would otherwise be necessary for web development.

This fosters better communication, engagement and also trust. Consider the following quotation:

Furthermore, there is still a lot of skepticism among stakeholders who do not trust the data and the analyses’ outcomes, and feel like going data-driven is risky and time-consuming … – (Figalist et al. 2021)

There are two key components for every Shiny app which must be provided:

  • the user interface (UI) which defines how it appears
  • the server function which defines how it works

Note that the shiny directory can also contain other files such as data or R scripts that are needed by the app. Also the app must be named app.R.

RStudio Tip: You can create a new directory and app.R file containing a basic shiny app in one step by choosing:

File > New Project > New Directory > Shiny Web Application

# A minimal shiny app

# Load the library
library(shiny)

# Define the UI function
ui <- fluidPage("Hello, world!")

# Define backend behaviour  
server <- function(input, output, session) {}

# Call to shinyApp() which returns a Shiny app object
shinyApp(ui, server)
Shiny applications not supported in static R Markdown documents

Running the app.R from RStudio we see from their bare bones web browser (i.e., hosting the shiny app locally) the following:

The minimal shiny app running in RStudio
The minimal shiny app running in RStudio

To stop the app

Running and stopping a shiny app under RStudio
Running and stopping a shiny app under RStudio

You can be inspired by exploring other shiny presentations at this gallery provided by RStudio.

References

Figalist, Iris, Christoph Elsner, Jan Bosch, and Helena Holmström Olsson. 2021. “Breaking the Vicious Circle: A Case Study on Why AI for Software Analytics and Business Intelligence Does Not Take Off in Practice.” Journal of Systems and Software, 111135.