I
NTRODUCTION TOS
HINYW
EBA
PPLICATIONS Julie SchollerM Éc E n
What is Shiny?
• R package
• Web application framework for R
• interactive web page
• interactivity with R
• Great for sharing R program with someone not knowing R
Two parts
• a web page that shows the app to the user
• a computer that powers the app
• your own computer
• a server
Shiny Template
library(shiny)
#User Interface ui <- fluidpage()
#Server code (R)
server <- function(input,output){}
shinyApp(ui = ui, server = server) Very Important
• name of the file app.R
• in its own folder
Alternate Method
• useful when the app is complex and involves lots of code
Two Files in the same folder
• UI: ui.R
• Server: server.R
Layout
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(# always two arguments sidebarPanel("sidebar panel"), mainPanel("main panel")
) )
Layout
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(
position = "right",# change sidebar position sidebarPanel("sidebar panel"),
mainPanel("main panel") )
)
Text in Panel
mainPanel(
h1("Big Title"),
p("p creates a paragraph of text."), h3("Third level title"),
code("text similar to computer code") )
All UI functions are simply HTML wrappers
ui <- fluidPage(
titlePanel("title panel"), sidebarLayout(
sidebarPanel("sidebar panel"), mainPanel("main panel")
) )
print(ui)
## <div class="container-fluid">
## <h2>title panel</h2>
## <div class="row">
## <div class="col-sm-4">
## <form class="well">sidebar panel</form>
## </div>
## <div class="col-sm-8">main panel</div>
## </div>
## </div>
Reactivity
For reactivity, app needs inputs and outputs Steps
• Add input to the UI
• Add an R object (output) to the UI
• Tell Shiny how to build the object in the server function
Reactivity
The object will be reactive if the code that builds it calls a widget value.
Add inputs to the UI
Example with iris
sidebarPanel(
selectInput("var", # name to access the widget’s value label = "Choose a variable to display", choices = list("Sepal Length",
"Sepal Width",
"Petal Length",
"Petal Width"), selected = "Sepal Length") )
More widgets
List of function
• actionButton: action Button
• checkboxGroupInput: group of check boxes
• checkboxInput: single check box
• dateInput: calendar to aid date selection
• dateRangeInput: pair of calendars for selecting a date range
• fileInput: file upload control wizard
• helpText: help text that can be added to an input form
• numericInput: field to enter numbers
• radioButtons: set of radio buttons
• selectInput: box with choices to select from
• sliderInput: slider bar
• submitButton: submit button
• textInput: field to enter text Link to Widget Gallery
Add an R object (output) to the UI
mainPanel(
textOutput("selected_var") )
List of Output functions
• dataTableOutput: DataTable
• tableOutput: table
• plotOutput: plot
• htmlOutput: raw HTML
• imageOutput: image
• textOutput: text
• uiOutput: raw HTML
• verbatimTextOutput: text
Provide R code to build the object
server <- function(input, output) { output$selected_var <- renderText({
paste("You have selected", input$var) })
}
Provide R code to build the object
server <- function(input, output) { output$selected_var <- renderText({
paste("You have selected", input$var) })
}
List of Render functions
• renderDataTable: DataTable
• renderImage: images (saved as a link to a source file)
• renderPlot: plots
• renderPrint: any printed output
• renderTable: data frame, matrix, other table like structures
• renderText: character strings
• renderUI: a Shiny tag object or HTML
Add a Plot - UI
ui <- fluidPage(
titlePanel("Final App"), sidebarLayout(
sidebarPanel(
selectInput("var",
label = "Choose a variable to display", choices = list("Sepal Length",
"Sepal Width",
"Petal Length",
"Petal Width"), selected = "Sepal Length") ),
mainPanel(
textOutput("selected_var"), plotOutput("plot_output") )
) )
Add a Plot - Server
server <- function(input, output) { output$selected_var <- renderText({
paste("Distribution of", input$var, "by Species") })
output$plot_output <- renderPlot({
variable<- switch(input$var,
"Sepal Length" = iris$Sepal.Length,
"Sepal Width" = iris$Sepal.Width,
"Petal Length" = iris$Petal.Length,
"Petal Width" = iris$Petal.Width )
data<-data.frame(variable=variable, Species=iris$Species) boxplot(data$variable~data$Species,las=1) })
}
switch: useful companion to multiple choice Shiny widgets
Result with a Plot - App
Summary
In the UI
• *Inputfunction: create input
• *Output function: place reactive objects in your Shiny app In the server
• render* function: tell Shiny how to build the objects
• surround R expressions by curly braces, {}, in eachrender*
function
• save yourrender\*expressions in the output list, with one entry for each reactive object in your app, and
• create reactivity by including an input value in arender\*
expression
Don’t forget
• The directory that app.R appears in will become the working directory of the Shiny app
• Shiny will run code placed at the start of app.R, before the server function, only once during the life of the app.
• Shiny will run code placed inside server function multiple times, which can slow down the app.
• use of reactive function
Reactivity
• input$var is a reactive value
• output$selected_var depends on input$var
• if input$varchanges, thenoutput$selected_varreacts
• All inputs are automatically reactive, so if you use any input inside arender* function, the output will re-render any time input changes
Reactive contexts
• You can define your own reactive variables
• Reactive values can only be used inside reactive contexts
• Any render*function is a reactive context
• Usereactive({...}) to assign a reactive variable
• Useobserve({...}) to access a reactive variable Remember
• reactive variable means anything that depends on it gets re-executed automatically
Reactive contexts - Assign a variable
#WRONG
server <- function(input, output) { x <- input$num + 1
}
#Right
server <- function(input, output) { x <- reactive({
input$num + 1 }) }
Reactive contexts - See a variable
#WRONG
server <- function(input, output) { print(input$num)
}
server <- function(input, output) { observe({
print(input$num) })
}
Sharing Shiny Apps
• as R script
• simplest
• works only if the users have R on their own computer
• as a web page
• more user friendly
• users only need web browser
• how
• host the app yourself (see with your IT departement)
• use RStudio support: Shinyapps.io, Shiny Server (Pro)
Around Shiny
• Shiny in RMarkdown document Header
---
title: "Shiny Document"
output: html_document runtime: shiny
---
in chunk
numericInput("rows", "How many cars?", 5) renderTable({
head(cars, input$rows) })
• ShinyDashboard