250 northern ave, boston, ma 02210 email: [email protected] … · studio cc by rstudio 2015 follow...

114
250 Northern Ave, Boston, MA 02210 Phone: 844-448-1212 Email: [email protected] Web: http://www.rstudio.com © 2014 RStudio, Inc. All rights reserved. Follow @rstudioapp All Training materials are provided "as is" and without warranty and RStudio disclaims any and all express and implied warranties including without limitation the implied warranties of title, fitness for a particular purpose, merchantability and noninfringement. The Training Materials are licensed under the Creative Commons Attribution-Noncommercial 3.0 United States License. To view a copy of this license, visithttp:// creativecommons.org/licenses/by-nc/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Follow @rstudio CC by RStudio 2015

Upload: others

Post on 17-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

250 Northern Ave, Boston, MA 02210 Phone: 844-448-1212

Email: [email protected] Web: http://www.rstudio.com

© 2014 RStudio, Inc. All rights reserved. Follow @rstudioapp

All Training materials are provided "as is" and without warranty and RStudio disclaims any and all express and implied warranties including without limitation the implied warranties of title, fitness for a particular purpose, merchantability and noninfringement.

The Training Materials are licensed under the Creative Commons Attribution-Noncommercial 3.0 United States License. To view a copy of this license, visithttp://creativecommons.org/licenses/by-nc/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

Follow @rstudioCC by RStudio 2015

Page 2: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Studio

CC by RStudio 2015 Follow @rstudio

Data Scientist and Master Instructor May 2015 Email: [email protected] Twitter: @StatGarrett

Garrett Grolemund

How to start with Shiny, Part 2

250 Northern Ave, Boston, MA 02210 Phone: 844-448-1212

Email: [email protected] Web: http://www.rstudio.com

How to customize reactions

Page 3: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

bit.ly/shiny-quickstart-2Code and slides at:

Page 4: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

www.rstudio.com/products/shiny/shiny-user-showcase/

Shiny Showcase

Page 5: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

How to start with Shiny

© CC 2015 RStudio, Inc.

1. How to build a Shiny app (www.rstudio.com/resources/webinars/)

2. How to customize reactions (Today)

3. How to customize appearance (June 3)

Slides at: bit.ly/shiny-quickstart-2

Page 6: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

The story so far

Page 7: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

Every Shiny app is maintained by a computer running R

Page 8: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

library(shiny)ui <- fluidPage()

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

App templateThe shortest viable shiny app

Page 9: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

library(shiny)

ui <- fluidPage(

)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

Page 10: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 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(ui = ui, server = server)

Page 11: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 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 12: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 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) { output$hist <-

}

shinyApp(ui = ui, server = server)

1

Page 13: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 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) { output$hist <- renderPlot({

}) }

shinyApp(ui = ui, server = server)

2

Page 14: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 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) { output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

3

Page 15: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 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) { output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 16: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

http://www.rstudio.com/resources/webinars/

Shiny Server (Pro)

!"

Sharing apps

Page 17: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

What is Reactivity?

Page 18: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

Think Excel.

Page 19: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

50 = F4 + 1

Page 20: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

50 51

Page 21: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

50100 51101

Page 22: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

100999 1011000

Page 23: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

100010019991000

Page 24: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

100010019991000

input$x output$y

Page 25: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

input$x output$y

run(this)

Page 26: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

output$yinput$x

run(this)

expression()

Page 27: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

input$x expression()

run(this)

Update

output$y

Page 28: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Begin with

reactive values

Page 29: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

Syntax

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

this input will provide a value saved as input$num

Page 30: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

The input value changes whenever a user changes the input.

input$num = 25

input$num = 50

input$num = 75

Input values

Page 31: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

Reactive values work together with reactive functions. You cannot call a reactive value from outside of one.

# hist(rnorm(100, input$num))

renderPlot({ hist(rnorm(100, input$num)) })$

Page 32: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 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) { output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 33: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 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) { output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 34: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 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) { output$hist <- hist(rnorm(input$num)) }

shinyApp(ui = ui, server = server)

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

Page 35: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num

output$hist <- renderPlot({ hist(rnorm(input$num)) })

Think of reactivity in R as a two step process

1 Reactive values notifythe functions that use them when they become invalid

Page 36: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

output$hist <- renderPlot({ hist(rnorm(input$num)) })

input$num

© CC 2015 RStudio, Inc.

Think of reactivity in R as a two step process

1 Reactive values notifythe functions that use them when they become invalid

2 The objects created by reactive functions respond(different objects respond differently)

Page 37: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Recap: Reactive values

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

You can only call a reactive value from a function that is designed to work with one#

The input list is a list of reactive values. The values show the current state of the inputs.

input$num = 25

Reactive values act as the data streams that flow through your app.input$x expression()

run(this)

Update

output$y

Reactive values notify. The objects created by reactive functions respond.

input$num

output$hist

Page 38: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Reactive toolkit

(7 indispensible functions)

Page 39: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

Reactive functions

1 Use a code chunk to build (and rebuild) an object

2 The object will respond to changes in a set of reactive values

• Which reactive values will the object respond to?

• What code will the function use?

Page 40: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Display output with render*()

Page 41: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

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 © CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

Render functions build output to display in the app

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

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

Page 42: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

renderPlot( { hist(rnorm(input$num)) })

render*()

object will respond to every reactive value in the code

code used to build (and rebuild) object

Builds reactive output to display in UI

Page 43: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

renderPlot( { hist(rnorm(input$num)) })

render*()Builds reactive output to display in UI

When notified that it is invalid, the object created by a render*() function will rerun the

entire block of code associated with it

Page 44: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 01-two-inputs

library(shiny)

ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), textInput(inputId = "title", label = "Write a title", value = "Histogram of Random Normal Values"), plotOutput("hist") )

server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num), main = input$title) }) }

shinyApp(ui = ui, server = server)

Page 45: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 01-two-inputs

library(shiny)

ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), textInput(inputId = "title", label = "Write a title", value = "Histogram of Random Normal Values"), plotOutput("hist") )

server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num), main = input$title) }) }

shinyApp(ui = ui, server = server)

Page 46: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num

output$hist <- renderPlot({ hist(rnorm(input$num), main = input$title) })

input$title

Page 47: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num input$title

output$hist <- renderPlot({ hist(rnorm(input$num), main = input$title) })

Page 48: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Recap: render*()

© CC 2015 RStudio, Inc.

Always save the result to output$output$

Slides at: bit.ly/shiny-quickstart-2

render*() functions make objects to display

render*() makes an observer object that has a block of code associated with it

output$hist <- renderPlot({ hist(rnorm(input$num), main = input$title) })

The object will rerun the entire code block to update itself whenever it is invalidated

renderPlot( { hist(rnorm(input$num)) })

Page 49: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Modularize code with reactive()

Page 50: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 02-two-outputs

library(shiny)

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

server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num)) }) output$stats <- renderPrint({ summary(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 51: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 02-two-outputs

library(shiny)

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

server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num)) }) output$stats <- renderPrint({ summary(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 52: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num

output$hist <- renderPlot({ hist(rnorm(input$num)) })

output$stats <- renderPrint({ summary(rnorm(input$num)) })

Page 53: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num

output$hist <- renderPlot({ hist(rnorm(input$num)) })

output$stats <- renderPrint({ summary(rnorm(input$num)) })

Page 54: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num

output$hist <- renderPlot({ hist(rnorm(input$num)) })

output$stats <- renderPrint({ summary(rnorm(input$num)) })

Can these describe the same data?

Page 55: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num

output$hist <- renderPlot({ hist(data) })

output$stats <- renderPrint({ summary(data) })

data <-? rnorm(input$num)

Page 56: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

data <- reactive( { rnorm(input$num) })

reactive()Builds a reactive object (reactive expression)

object will respond to every reactive value in the code

code used to build (and rebuild) object

Page 57: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

A reactive expression is special in two ways

1 You call a reactive expression like a function

data()

Page 58: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 02-two-outputs

library(shiny)

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

server <- function(input, output) {

output$hist <- renderPlot({ hist(rnorm(input$num)) }) output$stats <- renderPrint({ summary(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 59: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 02-two-outputs

library(shiny)

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

server <- function(input, output) { data <- reactive({ rnorm(input$num) }) output$hist <- renderPlot({ hist(rnorm(input$num)) }) output$stats <- renderPrint({ summary(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 60: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 03-reactive

library(shiny)

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

server <- function(input, output) { data <- reactive({ rnorm(input$num) }) output$hist <- renderPlot({ hist(data()) }) output$stats <- renderPrint({ summary(data()) }) }

shinyApp(ui = ui, server = server)

Page 61: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num

output$hist <- renderPlot({ hist(data()) })

output$stats <- renderPrint({ summary(data()) })

data <- reactive({ rnorm(input$num)})

Page 62: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num

output$hist <- renderPlot({ hist(data()) })

output$stats <- renderPrint({ summary(data()) })

data <- reactive({ rnorm(input$num)})

Page 63: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

A reactive expression is special in two ways

1 You call a reactive expression like a function

2 Reactive expressions cache their values (the expression will return its most recent value, unless it has become invalidated)

data()

Page 64: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Recap: reactive()

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

data() Call a reactive expression like a function

reactive() makes an object to use (in downstream code)

data <- reactive({ rnorm(input$num)})

Reactive expressions are themselves reactive. Use them to modularize your apps.

input$num

output$hist <- renderPlot({ hist(data()) })

output$stats <- renderPrint({ summary(data()) })

data <- reactive({ rnorm(input$num)})

Reactive expressions cache their values to avoid unnecessary computation2

Page 65: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Prevent reactions with isolate()

Page 66: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 01-two-inputs

library(shiny)

ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), textInput(inputId = "title", label = "Write a title", value = "Histogram of Random Normal Values"), plotOutput("hist") )

server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num), main = input$title) }) }

shinyApp(ui = ui, server = server)

Can we prevent the title field from updating the plot?

Page 67: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

isolate({ rnorm(input$num) })

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

isolate()Returns the result as a non-reactive value

object will NOT respond to any reactive value in the

code

code used to build object

Page 68: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 01-two-inputs

library(shiny)

ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), textInput(inputId = "title", label = "Write a title", value = "Histogram of Random Normal Values"), plotOutput("hist") )

server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num), main = input$title) }) }

shinyApp(ui = ui, server = server)

Page 69: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 04-isolate

library(shiny)

ui <- fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), textInput(inputId = "title", label = "Write a title", value = "Histogram of Random Normal Values"), plotOutput("hist") )

server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num), main = isolate({input$title})) }) }

shinyApp(ui = ui, server = server)

Page 70: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num input$title

output$hist <- renderPlot({ hist(rnorm(input$num), main = isolate(input$title)) })

Page 71: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num input$title

output$hist <- renderPlot({ hist(rnorm(input$num), main = isolate(input$title)) })

Page 72: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

input$num input$title

output$hist <- renderPlot({ hist(rnorm(input$num), main = isolate(input$title)) })

Page 73: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Recap: isolate()

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

○ isolate() makes an non-reactive object

$ Use isolate() to treat reactive values like normal R values

Page 74: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Trigger code with observeEvent()

Page 75: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

output$yinput$x expression()

Page 76: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

run(this)

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

input$x expression() output$y

Page 77: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 2015 RStudio, Inc. All rights reserved.

Slides at: bit.ly/shiny-quickstart-2

Action buttons

actionButton(inputId = "go", label = "Click Me!")

input name (for internal use)

input function

label to display

Notice: Id not ID

Page 78: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 05-actionButton

library(shiny)

ui <- fluidPage( actionButton(inputId = "clicks", label = "Click me") )

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

Page 79: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

observeEvent(input$clicks, { print(input$clicks) })

observeEvent()

code block to run whenever observer is invalidated

Triggers code to run on server

note: observer treats this code as if it has been isolated with isolate()

reactive value(s) to respond to

(observer invalidates ONLY when this value changes)

Page 80: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 05-actionButton

library(shiny)

ui <- fluidPage( actionButton(inputId = "clicks", label = "Click me") )

server <- function(input, output) { observeEvent(input$clicks, { print(as.numeric(input$clicks)) }) }

shinyApp(ui = ui, server = server)

Page 81: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

Action buttons articlehttp://shiny.rstudio.com/articles/action-buttons.html

Page 82: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

code block to run whenever observer is

invalidated

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

observe({ print(input$clicks) })

observe()Also triggers code to run on server.

Uses same syntax as render*(), reactive(), and isolate()

observer will respond to every reactive value in the

code

Page 83: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Recap: observeEvent()

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

observeEvent() triggers code to run on the server

Specify precisely which reactive values should invalidate the observer

observeEvent(input$clicks, { print(input$clicks) })

reactive value(s) to respond to

Use observe() for a more implicit syntaxobserve()

Page 84: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Delay reactions with eventReactive()

Page 85: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 07-eventReactive

library(shiny)

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

plotOutput("hist") )

server <- function(input, output) {

output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 86: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 07-eventReactive

library(shiny)

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

server <- function(input, output) {

output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Can we prevent the graph from updating

until we hit the button?

Page 87: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

data <- eventReactive(input$go, { rnorm(input$num) })

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

eventReactive()A reactive expression that only responds to specific values

code used to build (and rebuild) object

note: expression treats this code as if it has been isolated with isolate()

reactive value(s) to respond to

(expression invalidates ONLY when this value changes)

Page 88: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 07-eventReactive

library(shiny)

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

server <- function(input, output) {

output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 89: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 07-eventReactive

library(shiny)

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

server <- function(input, output) { data <- eventReactive(input$go, { }) output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 90: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 07-eventReactive

library(shiny)

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

server <- function(input, output) { data <- eventReactive(input$go, { }) output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 91: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 07-eventReactive

library(shiny)

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

server <- function(input, output) { data <- eventReactive(input$go, { rnorm(input$num) }) output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Page 92: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 07-eventReactive

library(shiny)

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

server <- function(input, output) { data <- eventReactive(input$go, { rnorm(input$num) }) output$hist <- renderPlot({ hist(data()) }) }

shinyApp(ui = ui, server = server)

Page 93: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

output$hist <- renderPlot({ hist(data()) })

© 2015 RStudio, Inc. All rights reserved.

input$num

data <- eventReactive(input$go, { rnorm(input$num) })

input$go

Page 94: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

output$hist <- renderPlot({ hist(data()) })

© 2015 RStudio, Inc. All rights reserved.

input$num

data <- eventReactive(input$go, { rnorm(input$num) })

input$go

Page 95: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Recap: eventReactive()

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

eventReactive(input$go, { rnorm(input$num) })

reactive value(s) to respond to

You can specify precisely which reactive values should invalidate the expression

eventReactive() creates a reactive expressiondata()

Use eventReactive() to delay reactionsUpdate

Page 96: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Manage state with reactiveValues()

Page 97: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© 2015 RStudio, Inc. All rights reserved.

Slides at: bit.ly/shiny-quickstart-2

The input value changes whenever a user changes the input.

input$num = 25

input$num = 50

input$num = 75

Input values

You c

annot s

et the

se

value

s in yo

ur co

de

Page 98: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

rv <- reactiveValues(data = rnorm(100))

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

reactiveValues()Creates a list of reactive values to manipulate programmatically

(optional) elements to add to the list

Page 99: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 08-reactiveValues

library(shiny)

ui <- fluidPage( actionButton(inputId = "norm", label = "Normal"), actionButton(inputId = "unif", label = "Uniform"), plotOutput("hist") )

server <- function(input, output) {

rv <- reactiveValues(data = rnorm(100))

observeEvent(input$norm, { rv$data <- rnorm(100) }) observeEvent(input$unif, { rv$data <- runif(100) })

output$hist <- renderPlot({ hist(rv$data) }) }

shinyApp(ui = ui, server = server)

Page 100: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

input$norm

© 2015 RStudio, Inc. All rights reserved.

output$hist <- renderPlot({ hist(rv$data) })

input$unifrv$datarv$datarnorm(100)

Page 101: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

input$norm

© 2015 RStudio, Inc. All rights reserved.

output$hist <- renderPlot({ hist(rv$data) })

input$unifrv$datarv$datarnorm(100)

Page 102: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

rv$datarnorm(100)rv$data

runif(100)input$unifinput$norm

© 2015 RStudio, Inc. All rights reserved.

output$hist <- renderPlot({ hist(rv$data) })

Page 103: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

input$norm

© 2015 RStudio, Inc. All rights reserved.

output$hist <- renderPlot({ hist(rv$data) })

input$unifrv$datarv$datarunif(100)

Page 104: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 08-reactiveValues

library(shiny)

ui <- fluidPage( actionButton(inputId = "norm", label = "Normal"), actionButton(inputId = "unif", label = "Uniform"), plotOutput("hist") )

server <- function(input, output) {

rv <- reactiveValues(data = rnorm(100))

observeEvent(input$norm, { rv$data <- rnorm(100) }) observeEvent(input$unif, { rv$data <- runif(100) })

output$hist <- renderPlot({ hist(rv$data) }) }

shinyApp(ui = ui, server = server)

Page 105: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

# 08-reactiveValues

library(shiny)

ui <- fluidPage( actionButton(inputId = "norm", label = "Normal"), actionButton(inputId = "unif", label = "Uniform"), plotOutput("hist") )

server <- function(input, output) {

rv <- reactiveValues(data = rnorm(100))

observeEvent(input$norm, { rv$data <- rnorm(100) }) observeEvent(input$unif, { rv$data <- runif(100) })

output$hist <- renderPlot({ hist(rv$data) }) }

shinyApp(ui = ui, server = server)

Page 106: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Recap: reactiveValues()

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

You can manipulate these values (usually with observeEvent())rv$data <-

reactiveValues() creates a list of reactive values

Page 107: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

input$x expression()

run(this)

Update

output$y

You now how to

Prevent reactions isolate()

Trigger arbitrary code observeEvent()

observe()

Delay reactions eventReactive()

Create your own reactive values reactiveValues()

Modularize reactions reactive()

Render reactive output

render*()

Page 108: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Parting

tips

Page 109: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Instance Instance

Application

WorkerWorkerWorkerWorkerWorker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Page 110: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

Reduce repetition

library(shiny)

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

server <- function(input, output) {

output$hist <- renderPlot({ hist(rnorm(input$num)) }) }

shinyApp(ui = ui, server = server)

Code outside the server function will be run once per R session (worker)

Code inside the server function will be run once per end user (connection)

Code inside a reactive function will be run once per reaction

(e.g. many times)

Place code where it will be re-run as little as necessary

Page 111: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

How can R possibly implement reactivity?http://shiny.rstudio.com/articles/understanding-reactivity.html

Page 112: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

Learn more

Page 113: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

How to start with Shiny

© CC 2015 RStudio, Inc.

1. How to build a Shiny app (www.rstudio.com/resources/webinars/)

2. How to customize reactions (Today)

3. How to customize appearance (June 3)

Slides at: bit.ly/shiny-quickstart-2

Page 114: 250 Northern Ave, Boston, MA 02210 Email: info@rstudio.com … · Studio CC by RStudio 2015 Follow @rstudio Data Scientist and Master Instructor May 2015 Email: garrett@rstudio.com

shiny.rstudio.com

© CC 2015 RStudio, Inc.

Slides at: bit.ly/shiny-quickstart-2

The Shiny Development Center