shiny interactive data analysis - reed college...function creates renderdatatable() an interactive...

49
Studio Dean Young ( [email protected] ) and Chester Ismay ( [email protected] ) Slides available at http://tinyurl.com/shinyslides Shiny Interactive Data Analysis How to build a Shiny App

Upload: others

Post on 23-Jan-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Studio

Dean Young ([email protected]) and Chester Ismay ([email protected]) Slides available at http://tinyurl.com/shinyslides

Shiny Interactive Data AnalysisHow to build a Shiny App

Page 2: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Learn R

Page 3: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

# Assignment of values to objectsnum_rows <- 10name <- “Chester”temp <— c(0, 10, 52, 100)vec <- rnorm(100)

# Simple function callmean(temp)[1] 40.5

R basics

Page 4: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

# Function definitioncube.it <- function(x) { cube <- x * x * x return(cube)}

# Function callcube.it(7)[1] 343

Writing functions

Page 5: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Understand the architecture

Page 6: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Every Shiny app is maintained by a computer running R

Page 7: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Every Shiny app is maintained by a computer running R

Page 8: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Server Instructions User Interface (UI)

Page 9: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Use the template

Page 10: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

library(shiny)ui <- fluidPage()

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

App templateThe shortest viable shiny app

Page 11: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Add elements to your app as arguments to fluidPage()

library(shiny)ui <- fluidPage("Hello World")

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

Page 12: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Inputs and Outputs

Page 13: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Build your app around inputs and outputs

Page 14: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Build your app around inputs and outputs

Page 15: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Add elements to your app as arguments to fluidPage()

ui <- fluidPage( # *Input() functions, # *Output() functions)

Page 16: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Inputs

Page 17: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

© CC 2015 RStudio, Inc.

library(shiny) ui <- fluidPage(

)

server <- function(input, output) {}

shinyApp(server = server, ui = ui)

Create an input with an input function.

Page 18: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

© CC 2015 RStudio, Inc.

library(shiny) ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100) )

server <- function(input, output) {}

shinyApp(server = server, ui = ui)

Create an input with an input function.

Page 19: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

© CC 2015 RStudio, Inc.

actionButton()submitButton()

checkboxInput() checkboxGroupInput() dateInput()

dateRangeInput() fileInput() numericInput() passwordInput()

radioButtons() selectInput() sliderInput() textInput()

Page 20: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Syntax

label to display

input name (for internal use)

Notice: Id not ID

sliderInput(inputId = "num", label = "Choose a number", …)

input specific arguments

?sliderInput

Page 21: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Outputs

Page 22: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Build your app around inputs and outputs

Page 23: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Function InsertsdataTableOutput() an interactive tablehtmlOutput() raw HTMLimageOutput() imageplotOutput() plottableOutput() tabletextOutput() textuiOutput() a Shiny UI elementverbatimTextOutput() text

Page 24: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

To display output, add it to fluidPage() with an *Output() function

*Output()

plotOutput(outputId = "hist")

name to give to the output object

the type of output to display

Page 25: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

© CC 2015 RStudio, Inc.

library(shiny)

ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), plotOutput("hist") )

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

Comma between arguments

Page 26: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

© CC 2015 RStudio, Inc.

library(shiny)

ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), plotOutput("hist") )

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

Page 27: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

© CC 2015 RStudio, Inc.

library(shiny)

ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), plotOutput("hist") )

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

* Output() adds a space in the ui for an R object.

Page 28: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

© CC 2015 RStudio, Inc.

library(shiny)

ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), plotOutput("hist") )

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

* Output() adds a space in the ui for an R object.

You must build the object in the server function

Page 29: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Display reactive results with an *Output() function

RecapBegin each app with the templatelibrary(shiny)

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

Add elements as arguments to fluidPage()Hello World

Create reactive inputs with an *Input() function

Use the server function to assemble inputs into outputs

Page 30: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Tell the

server how to assemble inputs into outputs

Page 31: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

server <- function(input, output) {

}

Use 3 rules to write the server function

Page 32: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Save objects to display to output$1server <- function(input, output) { output$hist <- # code

}

Page 33: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Save objects to display to output$1output$hist

plotOutput("hist")

Page 34: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Build objects to display with render*()

server <- function(input, output) { output$hist <- renderPlot({

})}

2

Page 35: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

function creates

renderDataTable() An interactive table

renderImage() An image (saved as a link to a source file)

renderPlot() A plot

renderPrint() A code block of printed output

renderTable() A table

renderText() A character string

renderUI() a Shiny UI element

Use the render*() function that creates the type of output you wish to make.

(from a data frame, matrix, or other table-like structure)

(from a data frame, matrix, or other table-like structure)

Page 36: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

renderPlot({ hist(vec) })

render*()

code block that builds the object

Builds reactive output to display in UI

type of object to build

Page 37: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Build objects to display with render*()

server <- function(input, output) { output$hist <- renderPlot({ hist(vec, breaks = input$num) })}

2

Page 38: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

sliderInput(inputId = "num",…)

input$num

Use input values with input$3

Page 39: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

The input value changes whenever a user changes the input.

input$num = 25

input$num = 50

input$num = 75

Input values

Page 40: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Use input values with input$

server <- function(input, output) { output$hist <- renderPlot({ hist(vec, breaks = input$num) })}

3

Page 41: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Reactivity automatically occurs whenever you use an input value to render an output object

Reactivity 101

function(input, output) { output$hist <- renderPlot({ hist(vec, breaks = input$num) })})

Page 42: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

© CC 2015 RStudio, Inc.

input$num

renderPlot({ hist(vec,breaks=input$num) })

Page 43: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

© CC 2015 RStudio, Inc.

input$num

renderPlot({ hist(vec,breaks=input$num) })

Page 44: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Recap: Server

Create reactivity by using Inputs to build rendered Outputs

1. Save the output that you build to output$output$hist <-

2. Build the output with a render*() functionrenderPlot({ hist(rnorm(input$num)) })

3. Access input values with input$input$num

Use the server function to assemble inputs into outputs. Follow 3 rules:

Page 45: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Practice

Page 46: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

ZIP file containing R code and slides: http://tinyurl.com/2016shiny

Page 47: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Learn more

Page 48: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

shiny.rstudio.comThe Shiny Development Center

Page 49: Shiny Interactive Data Analysis - Reed College...function creates renderDataTable() An interactive table renderImage() An image (saved as a link to a source file) renderPlot() A plot

Special thanks to Garrett Grolemund with RStudio for many of these slides