Building a Shiny app

Megumi Oshima & Nicholas Ducharme-Barth

January 2025

What is Shiny?


Shiny is a package that can be used to build interactive web apps in either R or Python.

Why?


  • Interactive dashboards can help with communicating science, and making it more accessible
  • Useful for analysts to visualize data or model outputs
  • Facilitates multi-model comparisons which can assist in model development and building intuition about the model.

How to build?


All Shiny apps need three components:

  • a user interface (ui.R): this is the interactive part
  • a server (server.R): this is the computation/plotting engine
  • a call to shiny::shinyApp() (app.R): this ties everything together and launches the app

Let’s look closer at an example.

Running an app

If the ui.R and server.R are defined as separate scripts running the app is straightforward.


Package all code and data in the same directory, this will make publishing the app easier.

# load packages
  library(shiny)

# source ui/server
  source("./ui.R")
  source("./server.R")

# Run the app
  app = shinyApp(ui=ui,server=server)
  runApp(app)

Publishing

Once your app is built it can hosted online via shinyapps.io (individuals) or Posit Connect (enterprise users).


In both cases, publishing can be done in a couple steps using the rsconnect R package.

  • Deploy your app rsconnect::deployApp(appDir = '<project-dir>')!

Group activity!


Play with the AI assistant for Shiny and Shiny live to add some new features to the existing app 02-shiny-example-vb-curve.r.

  • change the user interface options: add a slider?
  • add a second sex?
  • add a download data button?

Resources