6  Chapter 5: Introduction to Shiny for Interactive Web Applications

6.0.1 Key Topics

  • Basics of Shiny for developing interactive web applications in R.
  • Introduction to reactive inputs, outputs, and building a dynamic UI with Shiny.
  • Hands-on exercises to develop a simple Shiny app that includes interactive input and output elements.

6.0.2 Outcome

Participants will create a basic Shiny app to add interactivity to data visualizations and outputs.

6.0.3 Example 1

6.0.4 Example 2

6.1 Creating a Shiny App

Shiny is an R package that makes it easy to build interactive web applications directly from R. Below are the steps to create a basic Shiny app.

6.1.1 Step-by-Step Guide: Creating a Shiny App

6.1.1.1 Step 1: Set Up Your Environment

  1. Install the Shiny Package:
Show the code
install.packages("shiny")
  1. Create a New Directory for Your App:

    • Create a new folder for your app and name it appropriately (e.g., my_shiny_app).
  2. Create the app.R File:

    • Inside the new directory, create a file named app.R.

6.1.1.2 Step 2: Write the Basic Structure of the App

  1. Load the Shiny Library and Define UI and Server Functions:
Show the code
    library(shiny)

    # Define UI
    ui <- fluidPage(
      titlePanel("Hello Shiny!"),
      sidebarLayout(
        sidebarPanel(
          sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30)
        ),
        mainPanel(
          plotOutput("distPlot")
        )
      )
    )

    # Define server logic
    server <- function(input, output) {
      output$distPlot <- renderPlot({
        x <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    }

    # Run the application
    shinyApp(ui = ui, server = server)
  1. Run the App Locally:

    • Open app.R in RStudio and click “Run App” to start your application locally.

6.2 Publishing to shinyapps.io

Once your app is working locally, you can publish it online using shinyapps.io.

6.2.1 Step-by-Step Guide: Publishing to shinyapps.io

6.2.1.1 Step 1: Install and Set Up rsconnect

  1. Login to https://shinyapps.io:
  1. Use your GitHub account to login
  2. Click on the account icon on upper right corner and select “Tokens”
  3. Click on “Create Token” -> “Show” -> “Show secret” and copy to Clipboard
  1. Install rsconnect Package:
Show the code
    install.packages("rsconnect")
  1. Load rsconnect Library and Authenticate:
Show the code
    library(rsconnect)

    # Set account info (replace with your own account details)
    rsconnect::setAccountInfo(name='yourname',
                              token='yourtoken',
                              secret='yoursecret')

6.2.1.2 Step 2: Deploy Your App

  1. Deploy the App Using rsconnect:

    • In RStudio, deploy your app by running:
Show the code
    rsconnect::deployApp('path/to/your/app')
  1. Access Your Live App:

    • Once deployed, you will receive a URL where your app is hosted on shinyapps.io.

6.2.2 Exercises

  • Exercise 1: Modify the UI to include additional inputs like text boxes or dropdowns.
  • Exercise 2: Add another output element such as a table or plot.
  • Exercise 3: Customize the appearance of your app using CSS or themes.

By following these steps, participants will gain practical experience in creating interactive web applications using Shiny and learn how to deploy them online using shinyapps.io.