Shiny Interactive Data Analysis - Reed College · function creates renderDataTable() An interactive...
Transcript of Shiny Interactive Data Analysis - Reed College · function creates renderDataTable() An interactive...
![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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/1.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/3.jpg)
# 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/4.jpg)
# 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/17.jpg)
© 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/18.jpg)
© 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/19.jpg)
© 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/25.jpg)
© 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/26.jpg)
© 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/27.jpg)
© 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/28.jpg)
© 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/33.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/34.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/39.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/40.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/41.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/42.jpg)
© 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/43.jpg)
© 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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/44.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/45.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/46.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/47.jpg)
Learn more
![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](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ede58dfad6a402d6669ab53/html5/thumbnails/49.jpg)
Special thanks to Garrett Grolemund with RStudio for many of these slides