No history yet

Introduction to R and Shiny

From Data to Dynamic Apps

R is a programming language built by statisticians, for statisticians. It excels at data analysis, statistical modeling, and creating visualizations. If you need to wrangle data, run a regression, or plot a complex chart, R is one of the best tools for the job.

But what if you want to share your analysis with people who don't code? That's where Shiny comes in. Shiny is an R package that makes it easy to build interactive web applications directly from your R code. It lets you turn your data analysis into a user-friendly tool that anyone can explore through their web browser.

Think of R as the engine that crunches the numbers and Shiny as the dashboard that lets a driver interact with that engine without needing to be a mechanic.

Setting Up Your Environment

Before you can build anything, you need the right tools. The first step is to install R itself. You can download it for free from the Comprehensive R Archive Network (CRAN). It’s also highly recommended to install RStudio, a user-friendly interface that makes working with R much easier.

Once R is installed, you can add the Shiny package. Open R or RStudio and run the following command in the console. This tells R to download and install the Shiny package from CRAN.

install.packages("shiny")

With that one line, you're ready to start building web apps.

The Anatomy of a Shiny App

Every Shiny app has two essential components: a user interface (UI) and a server. These are often defined in two separate files, ui.R and server.R.

ComponentRole
User Interface (ui.R)Defines the layout and appearance of your app. It controls what the user sees, like text, plots, sliders, and buttons.
Server (server.R)Contains the instructions that your app uses to build and rebuild the objects displayed in the UI. It's the brain behind the operation, running the R code in response to user actions.

These two files must be in the same directory. When you run the app, Shiny connects them to create a working application.

Your First Shiny App

Let's build the simplest possible Shiny app. Create a new folder on your computer. Inside that folder, create two new files: ui.R and server.R.

First, add this code to your ui.R file. This sets up a basic webpage with a title.

# ui.R

library(shiny)

# Define UI for application
fluidPage(
  
  # Application title
  titlePanel("My First Shiny App"),
  
  # Main panel for displaying outputs
  mainPanel(
    h2("Hello, World!")
  )
)

Next, put this code in your server.R file. For this simple app, the server function doesn't need to do anything, but the file still needs to exist.

# server.R

library(shiny)

# Define server logic
function(input, output) {
  
}

The fluidPage function creates a display that automatically adjusts to the dimensions of the user's browser window. titlePanel creates the main title, and mainPanel is where your primary content will go. We've just added a simple heading for now.

To run your app, open either ui.R or server.R in RStudio and click the "Run App" button that appears at the top of the editor. Alternatively, you can run this command in the console, making sure to replace 'path/to/your/app' with the actual path to the folder you created.

shiny::runApp('path/to/your/app')

A new window will open, displaying your very first Shiny application. It's simple, but it's a running web app built entirely in R.

Time to check your understanding of these core concepts.

Quiz Questions 1/5

What is the primary purpose of the R programming language?

Quiz Questions 2/5

What does the Shiny package allow you to do with your R code?