R Shiny for Data Science Tutorial – Build Interactive Data-Driven Web Apps

4 min read 16 days ago
Published on Sep 04, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you will learn how to build interactive data-driven web applications using R and the Shiny package. This guide is based on a comprehensive video tutorial by Chanin Nantasenamat, also known as the Data Professor. By following along, you will be able to create various apps that visualize data and deploy them online, making your data science projects more accessible.

Step 1: Introduction to Shiny

  • Understand what Shiny is:
    • Shiny is an R package that allows you to build interactive web applications directly from R.
  • Install the Shiny package:
    • Use the following command in R:
      install.packages("shiny")
      
  • Load the Shiny library:
    library(shiny)
    

Step 2: Create Your First App to Print User Input

  • Set up the UI (User Interface):
    • Use the fluidPage function to create a simple layout.
    • Add input elements like textInput for user input.
  • Set up the server function:
    • Use renderText to display user input dynamically.
  • Combine UI and server functions:
    • Use the shinyApp function to launch your app.

Example code:

ui <- fluidPage(
  textInput("text", "Enter text:"),
  textOutput("outputText")
)

server <- function(input, output) {
  output$outputText <- renderText({ input$text })
}

shinyApp(ui = ui, server = server)

Step 3: Build an App to Display a Histogram

  • Set up the UI with an input slider:
    • Include a slider input for users to select a numeric range.
  • Set up the server to render a histogram:
    • Use the renderPlot function to create a histogram based on user input.

Example code:

ui <- fluidPage(
  sliderInput("bins", "Number of bins:", 1, 50, 30),
  plotOutput("histPlot")
)

server <- function(input, output) {
  output$histPlot <- renderPlot({
    x <- faithful$eruptions
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    hist(x, breaks = bins, col = 'blue', border = 'white')
  })
}

shinyApp(ui = ui, server = server)

Step 4: Create a Machine Learning App with a Weather Dataset

  • Load necessary libraries:
    • Use libraries such as caret for machine learning operations.
  • Set up a UI with input fields for model parameters.
  • Prepare the server logic to train and visualize the model.

Step 5: Build a Machine Learning App with the Iris Dataset

  • Similar to the previous step, but focus on the Iris dataset.
  • Allow users to select different classifiers and visualize results.

Step 6: Develop a BMI Calculator App

  • Create a UI with input fields for weight and height.
  • Calculate BMI in the server function and display the result.

Example code:

ui <- fluidPage(
  numericInput("weight", "Weight (kg):", value = 70),
  numericInput("height", "Height (m):", value = 1.75),
  textOutput("bmiResult")
)

server <- function(input, output) {
  output$bmiResult <- renderText({
    bmi <- input$weight / (input$height^2)
    paste("Your BMI is", round(bmi, 2))
  })
}

shinyApp(ui = ui, server = server)

Step 7: Deploy Your Shiny App to Heroku

  • Set up your Heroku account and install the Heroku CLI.
  • Prepare your app for deployment:
    • Create a Procfile with the following content:
      web: R -e "shiny::runApp('.')" 
      
  • Use Git to deploy:
    • Initialize a git repository, commit your files, and push to Heroku.

Conclusion

You have now learned how to create interactive web applications using R and Shiny. By following these steps, you can develop various apps to visualize data, implement machine learning models, and deploy them online. Explore further by customizing your apps and experimenting with different datasets. For more resources and to see examples, check out the links provided in the video description.