Welcome to the course! Building Web Applications in R with Shiny - - PowerPoint PPT Presentation

welcome to the course building web applications in r with
SMART_READER_LITE
LIVE PREVIEW

Welcome to the course! Building Web Applications in R with Shiny - - PowerPoint PPT Presentation

BUILDING WEB APPLICATIONS IN R WITH SHINY Welcome to the course! Building Web Applications in R with Shiny Building Web Applications in R with Shiny Background You are familiar with R as a programming language. You are familiar with


slide-1
SLIDE 1

BUILDING WEB APPLICATIONS IN R WITH SHINY

Welcome to the course!

slide-2
SLIDE 2

Building Web Applications in R with Shiny

slide-3
SLIDE 3

Building Web Applications in R with Shiny

Background

  • You are familiar with R as a programming language.
  • You are familiar with the Tidyverse, specifically

ggplot2 and dplyr.

slide-4
SLIDE 4

Building Web Applications in R with Shiny

Help

shiny.rstudio.com/ www.rstudio.com/ resources/cheatsheets/

slide-5
SLIDE 5

Building Web Applications in R with Shiny

Tips

  • Always run the entire script, not just up to the point

where you’re developing code.

  • Sometimes the best way to see what’s wrong is to run

the app and review the error.

  • Watch out for commas!
slide-6
SLIDE 6

Building Web Applications in R with Shiny

Anatomy of a Shiny app

library(shiny) ui <- fluidPage() server <- function(input, output) {} shinyApp(ui = ui, server = server)

User interface controls the layout and appearance of app Server function contains instructions needed to build app shinyApp() Creates the Shiny app object

slide-7
SLIDE 7

Building Web Applications in R with Shiny

Data

Let’s build a simple movie browser app!

movies.Rdata Data from IMDB and Rotten Tomatoes on random sample of 651 movies released in the US between 1970 and 2014

slide-8
SLIDE 8

Building Web Applications in R with Shiny

Revisit

library(shiny) library("movies.Rdata") ui <- fluidPage() server <- function(input, output) {} shinyApp(ui = ui, server = server)

Data used for this app

slide-9
SLIDE 9

BUILDING WEB APPLICATIONS IN R WITH SHINY

Let’s practice!

slide-10
SLIDE 10

BUILDING WEB APPLICATIONS IN R WITH SHINY

User interface

slide-11
SLIDE 11

Building Web Applications in R with Shiny

library(shiny) library("movies.Rdata") ui <- fluidPage() server <- function(input, output) {} shinyApp(ui = ui, server = server)

Anatomy of a Shiny app

User interface

  • Inputs defined and laid out
  • Outputs laid out

Server function

  • Outputs calculated
  • Any other calculations

needed for outputs are performed

slide-12
SLIDE 12

Building Web Applications in R with Shiny

ggplot(data = movies, aes_string(x = input$x, y = input$y)) + geom_point()

server ui

slide-13
SLIDE 13

Building Web Applications in R with Shiny

slide-14
SLIDE 14

Building Web Applications in R with Shiny

# Define UI for application that plots features of movies ui <- fluidPage( # Sidebar layout with a input and output definitions sidebarLayout( # Inputs: Select variables to plot sidebarPanel( # Select variable for y-axis selectInput(inputId = "y", label = "Y-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score"), # Select variable for x-axis selectInput(inputId = "x", label = "X-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "critics_score") ), # Output: Show scatterplot mainPanel( plotOutput(outputId = "scatterplot") ) ) )

slide-15
SLIDE 15

Building Web Applications in R with Shiny

# Define UI for application that plots features of movies ui <- fluidPage( # Sidebar layout with a input and output definitions sidebarLayout( # Inputs: Select variables to plot sidebarPanel( # Select variable for y-axis selectInput(inputId = "y", label = "Y-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score"), # Select variable for x-axis selectInput(inputId = "x", label = "X-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "critics_score") ), # Output: Show scatterplot mainPanel( plotOutput(outputId = "scatterplot") ) ) )

Create fluid page layout

slide-16
SLIDE 16

Building Web Applications in R with Shiny

# Define UI for application that plots features of movies ui <- fluidPage( # Sidebar layout with a input and output definitions sidebarLayout( # Inputs: Select variables to plot sidebarPanel( # Select variable for y-axis selectInput(inputId = "y", label = "Y-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score"), # Select variable for x-axis selectInput(inputId = "x", label = "X-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "critics_score") ), # Output: Show scatterplot mainPanel( plotOutput(outputId = "scatterplot") ) ) )

Create a layout with a sidebar and main area

slide-17
SLIDE 17

Building Web Applications in R with Shiny

sidebarPanel mainPanel

slide-18
SLIDE 18

Building Web Applications in R with Shiny

# Define UI for application that plots features of movies ui <- fluidPage( # Sidebar layout with a input and output definitions sidebarLayout( # Inputs: Select variables to plot sidebarPanel( # Select variable for y-axis selectInput(inputId = "y", label = "Y-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score"), # Select variable for x-axis selectInput(inputId = "x", label = "X-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "critics_score") ), # Output: Show scatterplot mainPanel( plotOutput(outputId = "scatterplot") ) ) )

Create a sidebar panel containing input controls

slide-19
SLIDE 19

Building Web Applications in R with Shiny

# Define UI for application that plots features of movies ui <- fluidPage( # Sidebar layout with a input and output definitions sidebarLayout( # Inputs: Select variables to plot sidebarPanel( # Select variable for y-axis selectInput(inputId = "y", label = "Y-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score"), # Select variable for x-axis selectInput(inputId = "x", label = "X-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "critics_score") ), # Output: Show scatterplot mainPanel( plotOutput(outputId = "scatterplot") ) ) )

slide-20
SLIDE 20

Building Web Applications in R with Shiny

# Define UI for application that plots features of movies ui <- fluidPage( # Sidebar layout with a input and output definitions sidebarLayout( # Inputs: Select variables to plot sidebarPanel(

# Select variable for y-axis selectInput(inputId = "y", label = "Y-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score"),

# Select variable for x-axis selectInput(inputId = "x", label = "X-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "critics_score") ), # Output: Show scatterplot mainPanel( plotOutput(outputId = "scatterplot") ) ) )

slide-21
SLIDE 21

Building Web Applications in R with Shiny

# Define UI for application that plots features of movies ui <- fluidPage( # Sidebar layout with a input and output definitions sidebarLayout( # Inputs: Select variables to plot sidebarPanel( # Select variable for y-axis selectInput(inputId = "y", label = "Y-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score"), # Select variable for x-axis selectInput(inputId = "x", label = "X-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "critics_score") ), # Output: Show scatterplot mainPanel( plotOutput(outputId = "scatterplot") ) ) )

Create a main panel containing

  • utput elements that get created

in the server function

slide-22
SLIDE 22

BUILDING WEB APPLICATIONS IN R WITH SHINY

Let's practice!

slide-23
SLIDE 23

BUILDING WEB APPLICATIONS IN R WITH SHINY

Server function

slide-24
SLIDE 24

Building Web Applications in R with Shiny

slide-25
SLIDE 25

Building Web Applications in R with Shiny

# Define server function required to create the scatterplot server <- function(input, output) { # Create scatterplot object the plotOutput function is expecting

  • utput$scatterplot <- renderPlot({

ggplot(data = movies, aes_string(x = input$x, y = input$y)) + geom_point() }) }

slide-26
SLIDE 26

Building Web Applications in R with Shiny

# Define server function required to create the scatterplot server <- function(input, output) { # Create the scatterplot object the plotOutput function is expecting

  • utput$scatterplot <- renderPlot({

ggplot(data = movies, aes_string(x = input$x, y = input$y)) + geom_point() }) }

Contains instructions needed to build app

slide-27
SLIDE 27

Building Web Applications in R with Shiny

# Define server function required to create the scatterplot server <- function(input, output) { # Create the scatterplot object the plotOutput function is expecting

  • utput$scatterplot <- renderPlot({

ggplot(data = movies, aes_string(x = input$x, y = input$y)) + geom_point() }) }

Specifies how the plot

  • utput should be updated
slide-28
SLIDE 28

Building Web Applications in R with Shiny

# Define server function required to create the scatterplot server <- function(input, output) { # Create the scatterplot object the plotOutput function is expecting

  • utput$scatterplot <- renderPlot({

ggplot(data = movies, aes_string(x = input$x, y = input$y)) + geom_point() }) }

Good ol’ ggplot2 code, with inputs from UI

slide-29
SLIDE 29

Building Web Applications in R with Shiny ui <- fluidPage(

. . . # Select variable for y-axis selectInput(inputId = "y", label = "Y-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "audience_score"), # Select variable for x-axis selectInput(inputId = "x", label = "X-axis:", choices = c("imdb_rating", "imdb_num_votes", "critics_score", "audience_score", "runtime"), selected = "critics_score") . . . )

server <- function(input, output) {

# Create the scatterplot object the plotOutput function is expecting

  • utput$scatterplot <- renderPlot({

ggplot(data = movies, aes_string(x = input$x, y = input$y)) + geom_point() }) }

slide-30
SLIDE 30

Building Web Applications in R with Shiny

Rules of server functions

  • 1. Save objects to display to output$xx
  • 2. Build objects to display with render*()
  • 3. Use input values with input$xx
slide-31
SLIDE 31

Building Web Applications in R with Shiny

works with

slide-32
SLIDE 32

Building Web Applications in R with Shiny server <- function(input, output) {

# Create the scatterplot object the plotOutput function is expecting

  • utput$scatterplot <- renderPlot({

ggplot(data = movies, aes_string(x = input$x, y = input$y)) + geom_point() }) }

ui <- fluidPage(

. . . # Output: Show scatterplot mainPanel( plotOutput(outputId = "scatterplot") . . . )

slide-33
SLIDE 33

Building Web Applications in R with Shiny

Reactivity

input$x

  • utput$y

expression() Update

Render reactive output render*() Create your

  • wn reactive values

*Input()

slide-34
SLIDE 34

Building Web Applications in R with Shiny

Putting all the pieces together

# Create the Shiny app object shinyApp(ui = ui, server = server)

slide-35
SLIDE 35

BUILDING WEB APPLICATIONS IN R WITH SHINY

Let's practice!

slide-36
SLIDE 36

BUILDING WEB APPLICATIONS IN R WITH SHINY

Recap

slide-37
SLIDE 37

Building Web Applications in R with Shiny

Every Shiny app has a webpage that the user visits, and behind this webpage there is a computer that serves this webpage by running R.

slide-38
SLIDE 38

Building Web Applications in R with Shiny

When running your app locally, the computer serving your app is your computer.

slide-39
SLIDE 39

Building Web Applications in R with Shiny

When your app is deployed, the computer serving your app is a web server.

slide-40
SLIDE 40

Building Web Applications in R with Shiny

Server instructions User interface

slide-41
SLIDE 41

Building Web Applications in R with Shiny

Change display

slide-42
SLIDE 42

Building Web Applications in R with Shiny

Close an app

slide-43
SLIDE 43

BUILDING WEB APPLICATIONS IN R WITH SHINY

On to the next chapter!