Files
shiny/_includes/tutorial/hello-shiny.md
2012-07-26 09:45:29 -07:00

1.6 KiB

Hello Shiny Screenshot

The Hello Shiny example is a simple application that generates a random distribution with a configurable number of observations and then plots it. To run the example type:

> library(shiny)
> runExample("01_hello")

ui.R

library(shiny)

# Define UI for application that plots random distributions 
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Hello Shiny!"),

  # Sidebar with a slider input for number of observations
  sidebarPanel(
    sliderInput("obs", 
                "Number of observations:", 
                min = 0, 
                max = 1000, 
                value = 500)
  ),

  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("distPlot")
  )
))

server.R

library(shiny)

# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {

  # Function that generates a plot of the distribution. The function
  # is wrapped in a call to reactivePlot to indicate that:
  #
  #  1) It is "reactive" and therefore should be automatically 
  #     re-executed when inputs change
  #  2) Its output type is a plot 
  #
  output$distPlot <- reactivePlot(function() {

    # convert string input to an integer
    obs <- as.integer(input$obs)

    # generate an rnorm distribution and plot it
    dist <- rnorm(obs)
    hist(dist)
  })
})