## A Tale of Two Files

Shiny applets are defined by two files, ui.R and server.R. These two files are enough to create a simple shiny applet.

• ui.R defines the page layout and user interface
• server.R contains the R code to create any output

## ui.R

library(shiny)

# A simple/common user interface template
shinyUI(fluidPage(

# Application title
titlePanel("Title"),

sidebarPanel(
# Define some inputs here
),

mainPanel(
# output (from the server) go here
)

))

## ui.R

• Defines the shinyUI() function, which contains elements that describe the HTML page

• Typical applets are constructed using the pageWithSidebar() function, which creates a side bar generally used for input and a main panel used for output

• Elements are hierarchical functions: components in the sidebar go into the sidebarPanel() function, separated by commas

• Input variables are named in ui.R and then referenced in server.R

## User Interface Example

User interface code for creating Example_01.r:

ui <- fluidPage(
# Application title
titlePanel("Summary Statistics"),
# Sidebar panel
sidebarLayout(
sidebarPanel(
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
# Numeric option to change n parameter in head() function
numericInput('headcount', 'How many rows would you like to display',
6, min = 1, max = 100)

),
# Main panel
mainPanel(
verbatimTextOutput("sum") # Displays the summary text
)
),
tableOutput('contents') # Displays table
)

## server.R

library(shiny)

# Server side logic
shinyServer(function(input, output) {
# do something
})

## server.R

• Defines the shinyServer(), which is a function with input and output variables.
• Contains all of the R code to generate any output
• (Later) can also be used to modify inputs
• Generally much longer than ui.R - all the interesting stuff happens in server.R

## Server Example

Server code for creating Example_01.r:

server <- function(input, output) {

# Define dataframe to be accessible for later use
dataframe <- reactive({
dataset <- input$data if (is.null(dataset)) return(NULL) read.csv(dataset$datapath, header=T)
})
# Renders summary output
output$sum <- renderPrint({ summary(dataframe()) }) # Renders table preview with input parameter output$contents <- renderTable({
head(dataInput(), n = input$obs) }) Whenever either the dataset or the number of observations changes, this function will be re-executed and the output will change. ## Your Turn Explore the code for the Example_01.r applet. 1. What is the name of the R variable that contains the dataset name? 2. Instead of displaying the summary, create a histogram of the seasonal at bats using the MLB Stats.csv dataset. Remember to load ggplot2 3. Name your shiny app accordingly. ## Answers ### 1. dataframe # In server dataframe <- reactive({ dataset <- input$data
if (is.null(dataset))
return(NULL)
read.csv(dataset$datapath, header=T) }) ### 2. # In user interface plotOutput("histplot") # In server output$histplot <- renderPlot({
qplot(dataframe()[,8], main = "Histogram of At Bats for MLB Data Set")+
labs(x = "Number of At Bats Per Season", y = "Frequency")
})