Introduction

shiny is an R package developed by the RStudio group allowing you to turn your ananlysis into interactive web applications, without having to deal wih HTML, CSS or JavaScript. Here are some gallery to show its capability. We will be using shiny for our Public Health Informatic lecture

shiny tutorial

It is highly recommended that you go through the shiny tutorial

shiny resources

Our Tutorial

We will build a simple Histogram using hist to build our first shiny application (app). First, let’s define some specifications for our app

  1. Name our application “My First App”
  2. Construct 2 areas in our app
    1. Control area
      1. An interactive “bin slider” to select the number of “bin” to plot
      2. An interactive “color selector” to select the color of the histrogram
    2. Plot area where the Histogram is plotted

Here is what we are trying to build:

alt text

shiny applicaiton files

To build a shiny application, we need 2 files:

  • ui.R
  • server.R

Both of these files must be contained within a folder; let’s call this folder my1st-app

my1st-app
  |_____ ui.R
  |_____ server.R

Copy the following codes into its respective ui.R and server.R files. I will explain them below.

ui.R


library(shiny)

shinyUI(fluidPage(
  
  titlePanel("My First App"),
  
  sidebarLayout(
    sidebarPanel(
      
      helpText("Slide to the desired bin number"),
      
      sliderInput("binSlider", label = h3("bin slider"),
                  min = 5,
                  max = 60,
                  value = 10,step = 10),
      
      selectInput("colorPullDown", label = h3("color selector"),
                  choices = list("red",
                                 "blue",
                                 "yellow"),
                  selected = "red")
      
      
    ), ## close: sidebarPanel
    
    mainPanel(
      plotOutput("hitogram")
    ) ## close: mainPanel
    
  ) ## close: sidebarLayout
)) ## close: shinyUI(fluidPage(

server.R


library(shiny)


shinyServer(function(input, output){
  
  random.data = rnorm(100000)
  
  output$hitogram <- renderPlot({
    
    hist(random.data, input$binSlider, col = input$colorPullDown)
    
  }) ## close: renderPlot
  
  
}) ## close: shinyServer

Define Working Areas

First, we will learn how to define the different “Area” on our interactive application

Title and Interactive Areas

alt text

Name your applicaiton with titlePanel

Use the titlePanel to name or title your application

Layout your applicaiton with sidebarLayout

Use the sidebarLayout to define what you want to put in your working area

Intearctive Control and Plot Areas