Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui...

258
slides at: www.calvin.edu/~rpruim/talks/ Interactive Web Applications with RStudio and Shiny Randall Pruim Calvin College December 2015 Big Data + Hadoop MeetUp Calvin College

Transcript of Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui...

Page 1: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Interactive Web Applications with RStudio and Shiny

Randall PruimCalvin College

December 2015 Big Data + Hadoop MeetUpCalvin College

Page 2: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

GarrettHELLO

my name is

Randy

Page 3: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Copyright 2015 RStudio | All Rights Reserved Follow @rstudio

Data Scientist and Master Instructor September 2015 Email: [email protected]

Garrett Grolemund

Interactive Shiny Applicationsbuilt on Big Data

SShiny© 2015 RStudio, Inc. All rights reserved.

Slides at: bit.ly/rday-nyc-strata15

Page 4: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Page 5: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

www.rstudio.com/resources/cheatsheets/The Shiny Cheat Sheet

Page 6: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

shiny.rstudio.comThe Shiny Development Center

Page 7: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

Shiny Showcase

Page 8: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

GarrettHELLO

my name is

Randy

Page 9: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

• shiny• R package that provides toolkit for creating shiny apps in R

• install.packages(“shiny”)

• shiny-server • put apps on the web (free and pro versions available)

• shiny.io• free public hosting for up to 5 shiny apps

• pro accounts available for a fee

slides at: www.calvin.edu/~rpruim/talks/

What is Shiny?

Every Shiny app is maintained by a computer running R

Page 10: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Every Shiny app is maintained by a computer running R

What is Shiny?

Page 11: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

• shiny• R package that provides toolkit for creating shiny apps in R

• install.packages(“shiny”)

• shiny-server • put apps on the web (free and pro versions available)

• shiny.io• RStudio can host your apps (free and pro accounts)

slides at: www.calvin.edu/~rpruim/talks/

What is Shiny?

Page 12: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Your Turn1. Log into the RStudio server and open a new R Script.

http://rstudio.calvin.edu

2. Create a new Project

Page 13: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Run Your App

Page 14: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Run Your App

Page 15: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Close your app

Page 16: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Outline

5. Big Data

4. Sharing

2. Reactivity

3. Interactive Plots

1. Components of an app

Page 17: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Components of an app

Page 18: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny)ui <- fluidPage()

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

App templateThe shortest viable shiny app

Communication b/w UI and server

Page 19: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Starting from Scratchslides at: www.calvin.edu/~rpruim/talks/

1. Delete ui.R and server.R

2. Open a new R Script [File > New > RScript]

3. Write the code below in your R script and save as app.R

4. Hit Run App

Build or purchase your own Shiny Server at www.rstudio.com/products/shiny-server/

Interactive Web Apps with shiny Cheat Sheet

RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • [email protected] • 844-448-1212 • rstudio.com

Building an App - Complete the template by adding arguments to fluidPage() and a body to the server function.

Basics

Learn more at shiny.rstudio.com/tutorial • shiny 0.12.0 • Updated: 6/15

learn more at shiny.rstudio.com

A Shiny app is a web page (UI) connected to a computer running a live R session (Server)

Users can manipulate the UI, which will cause the server to update the UI’s displays (by running R code).

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

App template

Outputs - render*() and *Output() functions work together to add R output to the UI

Begin writing a new app with this template. Preview the app by running the code at the R command line.

Inputs - collect values from the user

actionButton(inputId, label, icon, …)

actionLink(inputId, label, icon, …)

checkboxGroupInput(inputId, label, choices, selected, inline)

checkboxInput(inputId, label, value)

dateInput(inputId, label, value, min, max, format, startview, weekstart, language)

dateRangeInput(inputId, label, start, end, min, max, format, startview, weekstart, language, separator)

fileInput(inputId, label, multiple, accept)

numericInput(inputId, label, value, min, max, step)

passwordInput(inputId, label, value)

radioButtons(inputId, label, choices, selected, inline)

selectInput(inputId, label, choices, selected, multiple, selectize, width, size) (also selectizeInput())

sliderInput(inputId, label, min, max, value, step, round, format, locale, ticks, animate, width, sep, pre, post)

submitButton(text, icon) (Prevents reactions across entire app)

textInput(inputId, label, value)

Access the current value of an input object with input$<inputId>. Input values are reactive.

Share your app

The easiest way to share your app is to host it on shinyapps.io, a cloud based service from RStudio

1.Create a free or professional account at http://shinyapps.io

2.Click the Publish icon in the RStudio IDE (>=0.99) or run: rsconnect::deployApp("<path to directory>")

!"

• ui - nested R functions that assemble an HTML user interface for your app

• server - a function with instructions on how to build and rebuild the R objects displayed in the UI

• shinyApp - combines ui and server into a functioning app. Wrap with runApp() if calling from a sourced script or inside a function.

DT::renderDataTable(expr, options, callback, escape, env, quoted)

renderImage(expr, env, quoted, deleteFile)

renderPlot(expr, width, height, res, …, env, quoted, func)

renderPrint(expr, env, quoted, func, width)

renderTable(expr,…, env, quoted, func)

renderText(expr, env, quoted, func)

renderUI(expr, env, quoted, func)

dataTableOutput(outputId, icon, …)

imageOutput(outputId, width, height, click, dblclick, hover, hoverDelay, hoverDelayType, brush, clickId, hoverId, inline)

plotOutput(outputId, width, height, click, dblclick, hover, hoverDelay, hoverDelayType, brush, clickId, hoverId, inline)

verbatimTextOutput(outputId)

tableOutput(outputId)

textOutput(outputId, container, inline)

uiOutput(outputId, inline, container, …) htmlOutput(outputId, inline, container, …)

library(shiny)

ui <- fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") )

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

shinyApp(ui = ui, server = server)

Add inputs to the UI with *Input() functions

Add outputs with *Output() functions

Tell server how to render outputs with R in the server function. To do this: 1. Refer to outputs with output$<id> 2. Refer to inputs with input$<id> 3. Wrap code in a render*() function before

saving to output

&

Save your template as app.R. Alternatively, split your template into two files named ui.R and server.R.

library(shiny) ui <- fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") ) server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$n)) }) } shinyApp(ui = ui, server = server)

# ui.R fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") )

# server.R function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$n)) }) }

ui.R contains everything you would save to ui.

server.R ends with the function you would save

to server.

No need to call shinyApp().

works with

Save each app as a directory that contains an app.R file (or a server.R file and a ui.R file) plus optional extra files.app-nameapp.R

#

DESCRIPTIONREADME<other files>www

$$$

The directory name is the name of the app (optional) defines objects available to both ui.R and server.R (optional) used in showcase mode (optional) data, scripts, etc. (optional) directory of files to share with web browsers (images, CSS, .js, etc.) Must be named "www"

Launch apps with runApp(<path to

directory>)

$.rglobal.R$

More cheat sheets at http://www.rstudio.com/resources/cheatsheets/

Page 20: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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 21: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Inputs

Build or purchase your own Shiny Server at www.rstudio.com/products/shiny-server/

Interactive Web Apps with shiny Cheat Sheet

RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • [email protected] • 844-448-1212 • rstudio.com

Building an App - Complete the template by adding arguments to fluidPage() and a body to the server function.

Basics

Learn more at shiny.rstudio.com/tutorial • shiny 0.12.0 • Updated: 6/15

learn more at shiny.rstudio.com

A Shiny app is a web page (UI) connected to a computer running a live R session (Server)

Users can manipulate the UI, which will cause the server to update the UI’s displays (by running R code).

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

App template

Outputs - render*() and *Output() functions work together to add R output to the UI

Begin writing a new app with this template. Preview the app by running the code at the R command line.

Inputs - collect values from the user

actionButton(inputId, label, icon, …)

actionLink(inputId, label, icon, …)

checkboxGroupInput(inputId, label, choices, selected, inline)

checkboxInput(inputId, label, value)

dateInput(inputId, label, value, min, max, format, startview, weekstart, language)

dateRangeInput(inputId, label, start, end, min, max, format, startview, weekstart, language, separator)

fileInput(inputId, label, multiple, accept)

numericInput(inputId, label, value, min, max, step)

passwordInput(inputId, label, value)

radioButtons(inputId, label, choices, selected, inline)

selectInput(inputId, label, choices, selected, multiple, selectize, width, size) (also selectizeInput())

sliderInput(inputId, label, min, max, value, step, round, format, locale, ticks, animate, width, sep, pre, post)

submitButton(text, icon) (Prevents reactions across entire app)

textInput(inputId, label, value)

Access the current value of an input object with input$<inputId>. Input values are reactive.

Share your app

The easiest way to share your app is to host it on shinyapps.io, a cloud based service from RStudio

1.Create a free or professional account at http://shinyapps.io

2.Click the Publish icon in the RStudio IDE (>=0.99) or run: rsconnect::deployApp("<path to directory>")

!"

• ui - nested R functions that assemble an HTML user interface for your app

• server - a function with instructions on how to build and rebuild the R objects displayed in the UI

• shinyApp - combines ui and server into a functioning app. Wrap with runApp() if calling from a sourced script or inside a function.

DT::renderDataTable(expr, options, callback, escape, env, quoted)

renderImage(expr, env, quoted, deleteFile)

renderPlot(expr, width, height, res, …, env, quoted, func)

renderPrint(expr, env, quoted, func, width)

renderTable(expr,…, env, quoted, func)

renderText(expr, env, quoted, func)

renderUI(expr, env, quoted, func)

dataTableOutput(outputId, icon, …)

imageOutput(outputId, width, height, click, dblclick, hover, hoverDelay, hoverDelayType, brush, clickId, hoverId, inline)

plotOutput(outputId, width, height, click, dblclick, hover, hoverDelay, hoverDelayType, brush, clickId, hoverId, inline)

verbatimTextOutput(outputId)

tableOutput(outputId)

textOutput(outputId, container, inline)

uiOutput(outputId, inline, container, …) htmlOutput(outputId, inline, container, …)

library(shiny)

ui <- fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") )

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

shinyApp(ui = ui, server = server)

Add inputs to the UI with *Input() functions

Add outputs with *Output() functions

Tell server how to render outputs with R in the server function. To do this: 1. Refer to outputs with output$<id> 2. Refer to inputs with input$<id> 3. Wrap code in a render*() function before

saving to output

&

Save your template as app.R. Alternatively, split your template into two files named ui.R and server.R.

library(shiny) ui <- fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") ) server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$n)) }) } shinyApp(ui = ui, server = server)

# ui.R fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") )

# server.R function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$n)) }) }

ui.R contains everything you would save to ui.

server.R ends with the function you would save

to server.

No need to call shinyApp().

works with

Save each app as a directory that contains an app.R file (or a server.R file and a ui.R file) plus optional extra files.app-nameapp.R

#

DESCRIPTIONREADME<other files>www

$$$

The directory name is the name of the app (optional) defines objects available to both ui.R and server.R (optional) used in showcase mode (optional) data, scripts, etc. (optional) directory of files to share with web browsers (images, CSS, .js, etc.) Must be named "www"

Launch apps with runApp(<path to

directory>)

$.rglobal.R$

More cheat sheets at http://www.rstudio.com/resources/cheatsheets/

Build or purchase your own Shiny Server at www.rstudio.com/products/shiny-server/

Interactive Web Apps with shiny Cheat Sheet

RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • [email protected] • 844-448-1212 • rstudio.com

Building an App - Complete the template by adding arguments to fluidPage() and a body to the server function.

Basics

Learn more at shiny.rstudio.com/tutorial • shiny 0.12.0 • Updated: 6/15

learn more at shiny.rstudio.com

A Shiny app is a web page (UI) connected to a computer running a live R session (Server)

Users can manipulate the UI, which will cause the server to update the UI’s displays (by running R code).

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

App template

Outputs - render*() and *Output() functions work together to add R output to the UI

Begin writing a new app with this template. Preview the app by running the code at the R command line.

Inputs - collect values from the user

actionButton(inputId, label, icon, …)

actionLink(inputId, label, icon, …)

checkboxGroupInput(inputId, label, choices, selected, inline)

checkboxInput(inputId, label, value)

dateInput(inputId, label, value, min, max, format, startview, weekstart, language)

dateRangeInput(inputId, label, start, end, min, max, format, startview, weekstart, language, separator)

fileInput(inputId, label, multiple, accept)

numericInput(inputId, label, value, min, max, step)

passwordInput(inputId, label, value)

radioButtons(inputId, label, choices, selected, inline)

selectInput(inputId, label, choices, selected, multiple, selectize, width, size) (also selectizeInput())

sliderInput(inputId, label, min, max, value, step, round, format, locale, ticks, animate, width, sep, pre, post)

submitButton(text, icon) (Prevents reactions across entire app)

textInput(inputId, label, value)

Access the current value of an input object with input$<inputId>. Input values are reactive.

Share your app

The easiest way to share your app is to host it on shinyapps.io, a cloud based service from RStudio

1.Create a free or professional account at http://shinyapps.io

2.Click the Publish icon in the RStudio IDE (>=0.99) or run: rsconnect::deployApp("<path to directory>")

!"

• ui - nested R functions that assemble an HTML user interface for your app

• server - a function with instructions on how to build and rebuild the R objects displayed in the UI

• shinyApp - combines ui and server into a functioning app. Wrap with runApp() if calling from a sourced script or inside a function.

DT::renderDataTable(expr, options, callback, escape, env, quoted)

renderImage(expr, env, quoted, deleteFile)

renderPlot(expr, width, height, res, …, env, quoted, func)

renderPrint(expr, env, quoted, func, width)

renderTable(expr,…, env, quoted, func)

renderText(expr, env, quoted, func)

renderUI(expr, env, quoted, func)

dataTableOutput(outputId, icon, …)

imageOutput(outputId, width, height, click, dblclick, hover, hoverDelay, hoverDelayType, brush, clickId, hoverId, inline)

plotOutput(outputId, width, height, click, dblclick, hover, hoverDelay, hoverDelayType, brush, clickId, hoverId, inline)

verbatimTextOutput(outputId)

tableOutput(outputId)

textOutput(outputId, container, inline)

uiOutput(outputId, inline, container, …) htmlOutput(outputId, inline, container, …)

library(shiny)

ui <- fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") )

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

shinyApp(ui = ui, server = server)

Add inputs to the UI with *Input() functions

Add outputs with *Output() functions

Tell server how to render outputs with R in the server function. To do this: 1. Refer to outputs with output$<id> 2. Refer to inputs with input$<id> 3. Wrap code in a render*() function before

saving to output

&

Save your template as app.R. Alternatively, split your template into two files named ui.R and server.R.

library(shiny) ui <- fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") ) server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$n)) }) } shinyApp(ui = ui, server = server)

# ui.R fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") )

# server.R function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$n)) }) }

ui.R contains everything you would save to ui.

server.R ends with the function you would save

to server.

No need to call shinyApp().

works with

Save each app as a directory that contains an app.R file (or a server.R file and a ui.R file) plus optional extra files.app-nameapp.R

#

DESCRIPTIONREADME<other files>www

$$$

The directory name is the name of the app (optional) defines objects available to both ui.R and server.R (optional) used in showcase mode (optional) data, scripts, etc. (optional) directory of files to share with web browsers (images, CSS, .js, etc.) Must be named "www"

Launch apps with runApp(<path to

directory>)

$.rglobal.R$

More cheat sheets at http://www.rstudio.com/resources/cheatsheets/

Page 22: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny)ui <- fluidPage( sliderInput("num", “Choose a number”, 1, 100, 25))server <- function(input, output) {}shinyApp(ui = ui, server = server)

Inputscollect a value from your user.

unique Id displayed text

Page 23: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Syntax

input name (used by server)

Notice: Id not ID

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

input specific arguments

?sliderInput

Page 24: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Syntax

label to display

input name (used by server)

Notice: Id not ID

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

input specific arguments

?sliderInput

Page 25: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Build or purchase your own Shiny Server at www.rstudio.com/products/shiny-server/

Interactive Web Apps with shiny Cheat Sheet

RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • [email protected] • 844-448-1212 • rstudio.com

Building an App - Complete the template by adding arguments to fluidPage() and a body to the server function.

Basics

Learn more at shiny.rstudio.com/tutorial • shiny 0.12.0 • Updated: 6/15

learn more at shiny.rstudio.com

A Shiny app is a web page (UI) connected to a computer running a live R session (Server)

Users can manipulate the UI, which will cause the server to update the UI’s displays (by running R code).

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

App template

Outputs - render*() and *Output() functions work together to add R output to the UI

Begin writing a new app with this template. Preview the app by running the code at the R command line.

Inputs - collect values from the user

actionButton(inputId, label, icon, …)

actionLink(inputId, label, icon, …)

checkboxGroupInput(inputId, label, choices, selected, inline)

checkboxInput(inputId, label, value)

dateInput(inputId, label, value, min, max, format, startview, weekstart, language)

dateRangeInput(inputId, label, start, end, min, max, format, startview, weekstart, language, separator)

fileInput(inputId, label, multiple, accept)

numericInput(inputId, label, value, min, max, step)

passwordInput(inputId, label, value)

radioButtons(inputId, label, choices, selected, inline)

selectInput(inputId, label, choices, selected, multiple, selectize, width, size) (also selectizeInput())

sliderInput(inputId, label, min, max, value, step, round, format, locale, ticks, animate, width, sep, pre, post)

submitButton(text, icon) (Prevents reactions across entire app)

textInput(inputId, label, value)

Access the current value of an input object with input$<inputId>. Input values are reactive.

Share your app

The easiest way to share your app is to host it on shinyapps.io, a cloud based service from RStudio

1.Create a free or professional account at http://shinyapps.io

2.Click the Publish icon in the RStudio IDE (>=0.99) or run: rsconnect::deployApp("<path to directory>")

!"

• ui - nested R functions that assemble an HTML user interface for your app

• server - a function with instructions on how to build and rebuild the R objects displayed in the UI

• shinyApp - combines ui and server into a functioning app. Wrap with runApp() if calling from a sourced script or inside a function.

DT::renderDataTable(expr, options, callback, escape, env, quoted)

renderImage(expr, env, quoted, deleteFile)

renderPlot(expr, width, height, res, …, env, quoted, func)

renderPrint(expr, env, quoted, func, width)

renderTable(expr,…, env, quoted, func)

renderText(expr, env, quoted, func)

renderUI(expr, env, quoted, func)

dataTableOutput(outputId, icon, …)

imageOutput(outputId, width, height, click, dblclick, hover, hoverDelay, hoverDelayType, brush, clickId, hoverId, inline)

plotOutput(outputId, width, height, click, dblclick, hover, hoverDelay, hoverDelayType, brush, clickId, hoverId, inline)

verbatimTextOutput(outputId)

tableOutput(outputId)

textOutput(outputId, container, inline)

uiOutput(outputId, inline, container, …) htmlOutput(outputId, inline, container, …)

library(shiny)

ui <- fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") )

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

shinyApp(ui = ui, server = server)

Add inputs to the UI with *Input() functions

Add outputs with *Output() functions

Tell server how to render outputs with R in the server function. To do this: 1. Refer to outputs with output$<id> 2. Refer to inputs with input$<id> 3. Wrap code in a render*() function before

saving to output

&

Save your template as app.R. Alternatively, split your template into two files named ui.R and server.R.

library(shiny) ui <- fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") ) server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$n)) }) } shinyApp(ui = ui, server = server)

# ui.R fluidPage( numericInput(inputId = "n", "Sample size", value = 25), plotOutput(outputId = "hist") )

# server.R function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$n)) }) }

ui.R contains everything you would save to ui.

server.R ends with the function you would save

to server.

No need to call shinyApp().

works with

Save each app as a directory that contains an app.R file (or a server.R file and a ui.R file) plus optional extra files.app-nameapp.R

#

DESCRIPTIONREADME<other files>www

$$$

The directory name is the name of the app (optional) defines objects available to both ui.R and server.R (optional) used in showcase mode (optional) data, scripts, etc. (optional) directory of files to share with web browsers (images, CSS, .js, etc.) Must be named "www"

Launch apps with runApp(<path to directory>)

$.rglobal.R$

More cheat sheets at http://www.rstudio.com/resources/cheatsheets/

Outputsdisplay output from R.

Page 26: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny) ui <- fluidPage( sliderInput("num", "", 1, 10, 5), )

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

Outputsdisplay output from R.

Build outputs in 3 steps:

Page 27: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny) ui <- fluidPage( sliderInput("num", "", 1, 100, 25), plotOutput("bar") ) server <- function(input, output) {}

shinyApp(ui = ui, server = server)

Outputsdisplay output from R.

Build outputs in 3 steps:

Add a *Output() function to ui (places output)1.

Page 28: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

*Output()

plotOutput(outputId = "bar")

Page 29: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

*Output()

plotOutput(outputId = "bar")

the type of output to display

Page 30: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

*Output()

plotOutput(outputId = "bar")

name to give to the output object

the type of output to display

Page 31: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny) ui <- fluidPage( sliderInput("num", "", 1, 100, 25), plotOutput("bar") ) server <- function(input, output) { renderPlot({ barplot(50, ylim = c(0, 100)) }) } shinyApp(ui = ui, server = server)

Outputsdisplay output from R.

Build outputs in 3 steps:

Add a *Output() function to ui (places output)1.Make with render*() function in server (builds output)2.

3.

Page 32: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

renderPlot({ barplot(50, ylim = c(0, 100)) })

render*()Builds reactive output to display in UI

Page 33: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

renderPlot({ barplot(50, ylim = c(0, 100)) })

render*()Builds reactive output to display in UI

type of object to build

Page 34: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

renderPlot({ barplot(50, ylim = c(0, 100)) })

render*()

code block that builds the object

Builds reactive output to display in UI

type of object to build

Page 35: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny) ui <- fluidPage( sliderInput("num", "", 1, 100, 25), plotOutput("bar") ) server <- function(input, output) { output$bar <- renderPlot({ barplot(50, ylim = c(0, 100)) }) } shinyApp(ui = ui, server = server)

Outputsdisplay output from R.

Build outputs in 3 steps:

Add a *Output() function to ui (places output)1.Make with render*() function in server (builds output)2.Save to output$ list (stores output)3.

Page 36: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny) ui <- fluidPage( sliderInput("num", "", 1, 100, 25), plotOutput("bar") ) server <- function(input, output) { output$bar <- renderPlot({ barplot(50, ylim = c(0, 100)) }) } shinyApp(ui = ui, server = server)

Outputsdisplay output from R.

Build outputs in 3 steps:

Add a *Output() function to ui (places output)1.Make with render*() function in server (builds output)2.Save to output$ list (stores output)3.

Match names

Page 37: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Your TurnMake a new app that contains:

1. A slider that goes from 1 to 100

2. A histogram 100 random normal values

hist(rnorm(100)) # base

histogram( ~ rnorm(100)) # lattice

qplot(rnorm(100)) # ggplot2

Page 38: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny)ui <- fluidPage( sliderInput("num", "", 1, 100, 25), plotOutput("hist"))server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(100)) })}shinyApp(ui = ui, server = server)

To do: add interaction between

slider and plot

Page 39: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny)ui <- fluidPage()

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

App templateThe shortest viable shiny app

Communication b/w UI and server

Page 40: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

ReactionsThe input$ list stores the current value of

each input object under its name.

sliderInput(inputId = "num", …)

input$num

Page 41: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny) ui <- fluidPage( sliderInput("num", "", 1, 100, 25), plotOutput("bar") ) server <- function(input, output) { output$bar <- renderPlot({ barplot(input$num, ylim=c(0, 100)) }) } shinyApp(ui = ui, server = server)

1.2.

An input value

ReactionsShiny will update an output whenever an input value changes if the output uses the input value in its render function.

Page 42: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

1.2.

ReactionsShiny will update an output whenever an input value changes if the output uses the input value in its render function.

library(shiny) ui <- fluidPage( sliderInput("num", "", 1, 100, 25), plotOutput("bar") ) server <- function(input, output) { output$bar <- renderPlot({ input$num barplot(50, ylim=c(0,100)) })}

shinyApp(ui = ui, server = server)

An input value

Page 43: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Your TurnChange your app to make the number of random normal values in the histogram react to the value of the slider.

library(shiny) ui <- fluidPage( sliderInput("num", "", 1, 100, 25), plotOutput("bar") ) server <- function(input, output) { output$bar <- renderPlot({ barplot(input$num, ylim=c(0, 100)) }) } shinyApp(ui = ui, server = server)

Page 44: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

library(shiny)ui <- fluidPage( sliderInput("num", "", 1, 100, 25), plotOutput("hist"))server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num)) })}shinyApp(ui = ui, server = server)

Page 45: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Recap

Page 46: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

RecapBegin each app with the templatelibrary(shiny)

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

Page 47: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

Page 48: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

Page 49: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Recap

Display R results with an *Output() function

Begin 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

Page 50: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Recap

Display R results with an *Output() function

Begin 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 51: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Recap: Server

Page 52: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

Page 53: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Recap: Server

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

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

Page 54: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Recap: Server

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

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

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

Page 55: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Recap: Server

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 56: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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 57: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Reactivity

Page 58: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Think Excel.

Page 59: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Page 60: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

50

Page 61: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

50

Page 62: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

50 = F4 + 1

Page 63: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

50 51

Page 64: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

50 51

Page 65: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

50100 51101

Page 66: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

100 101

Page 67: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

100999 1011000

Page 68: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

1000999

Page 69: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

100010019991000

Page 70: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

100010019991000

Page 71: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

100010019991000

Page 72: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

100010019991000

input$x output$y

Page 73: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

100010019991000

input$x output$y

Page 74: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x output$y

Page 75: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x output$y

run(this)

Page 76: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

output$yinput$x

run(this)

Page 77: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

output$yinput$x

run(this)

expression()

Page 78: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x expression()

run(this)

output$y

Page 79: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x expression()

run(this)

Update

output$y

Page 80: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x expression()

run(this)

Update

output$y

Page 81: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x expression()

run(this)

Update

output$y

Prevent reactions isolate()

Trigger arbitrary code observeEvent()

observe()

Delay reactions eventReactive()

Create your own reactive values reactiveValues()

*Input()

Modularize reactions reactive()

Render reactive output

render*()

Page 82: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x expression()

run(this)

Update

output$y

Prevent reactions isolate()

Trigger arbitrary code observeEvent()

observe()

Delay reactions eventReactive()

Create your own reactive values reactiveValues()

*Input()

Modularize reactions reactive()

Render reactive output

render*()

Reactive functions

Page 83: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x expression()

run(this)

Update

output$y

Prevent reactions isolate()

Trigger arbitrary code observeEvent()

observe()

Delay reactions eventReactive()

Create your own reactive values reactiveValues()

*Input()

Modularize reactions reactive()

Render reactive output

render*()

Prevent reactions isolate()

Trigger arbitrary code observeEvent()

observe()

Delay reactions eventReactive()

Create your own reactive values reactiveValues()

*Input()

Modularize reactions reactive()

Render reactive output

render*()

Reactive functions

Page 84: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

You cannot call an input value (reactive value) from outside of a reactive function.

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

Page 85: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

You cannot call an input value (reactive value) from outside of a reactive function.

# hist(rnorm(input$num))

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

Page 86: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x output$y

Think of reactivity in R as a two step process

Page 87: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x output$y

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

Think of reactivity in R as a two step process

Page 88: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x output$y

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

Think of reactivity in R as a two step process

Page 89: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x output$y

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

Think of reactivity in R as a two step process

2 Objects respond How the object responds depends on which reactive function created it.

Page 90: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

input$x output$y

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

Think of reactivity in R as a two step process

2 Objects respond How the object responds depends on which reactive function created it.

Page 91: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

input$x output$y

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

Think of reactivity in R as a two step process

2 Objects respond How the object responds depends on which reactive function created it.

> hist(rnorm(input$num))

Page 92: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

render*()

Builds an object that:

When notified by:

Page 93: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

render*()

Reruns code chunk (saves results to output$)

Builds an object that:

When notified by:

Page 94: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

render*()

any reactive value in the code chunk

Reruns code chunk (saves results to output$)

Builds an object that:

When notified by:

Page 95: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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 R output

renderTable() A table

renderText() A character string

renderUI() a Shiny UI element

Each function builds a different type of output.

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

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

Page 96: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Use…render*() to make an object to display in the UI.

Page 97: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Your Turnslides at: www.calvin.edu/~rpruim/talks/

Use renderPrint() and verbatimTextOutput() to add a summary() of rnorm(input$num) to your app, e.g.

summary(rnorm(input$num))

Page 98: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

ui <- fluidPage( sliderInput("num", “Choose a #”, 1, 100, 50), plotOutput("hist"), verbatimTextOutput("sum"))server <- function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num)) }) output$sum <- renderPrint({ summary(rnorm(input$num)) })}shinyApp(ui = ui, server = server)

Page 99: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

Page 100: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

Page 101: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

Page 102: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

Page 103: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

> hist(rnorm(input$num))

Page 104: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

> hist(rnorm(input$num))

Page 105: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

> hist(rnorm(input$num))

Page 106: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

> hist(rnorm(input$num)) > summary(rnorm(input$num))

Page 107: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

> hist(rnorm(input$num)) > summary(rnorm(input$num))

Page 108: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

> hist(rnorm(input$num)) > summary(rnorm(input$num))

Page 109: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

What is odd

about this app?

> hist(rnorm(input$num)) > summary(rnorm(input$num))

Page 110: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

Do these describe the same data?

Page 111: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

reactive()Makes a reactive object that you can use in downstream code.

Page 112: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

reactive()

notifies objects that use it that they are invalidBuilds an object that:

Makes a reactive object that you can use in downstream code.

Page 113: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

reactive()

notifies objects that use it that they are invalidBuilds an object that:

any reactive value in the code chunkWhen notified by:

Makes a reactive object that you can use in downstream code.

Page 114: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

A reactive expression is special in two ways

1 You call a reactive expression like a function

data()

Page 115: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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 116: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

Page 117: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

Page 118: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

Page 119: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

Page 120: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

Page 121: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

> hist(data()))

Page 122: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

> hist(data()))

> rnorm(input$num)

Page 123: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

> hist(data()))

> rnorm(input$num)

Page 124: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

> hist(data()))

> rnorm(input$num)

Page 125: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

> hist(data())) > summary(data())

> rnorm(input$num)

Page 126: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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

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

input$num

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

> hist(data())) > summary(data())

> rnorm(input$num)

Page 127: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Your TurnUse reactive() to pass the same data to the histogram and the summary.

Ensure that you can predict how the app will work.

Page 128: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

ui <- fluidPage( sliderInput("num", “Choose a #”, 1, 100, 50), plotOutput("hist"), verbatimTextOutput("sum"))server <- function(input, output) { data <- reactive({ rnorm(input$num) }) output$hist <- renderPlot({ hist(data()) }) output$sum <- renderPrint({ summary(data()) })}shinyApp(ui = ui, server = server)

Page 129: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Use…

reactive() to make an object to use in downstream code.

input$num

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

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

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

render() to make an object to display in the UI.

Page 130: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

ui <- fluidPage( sliderInput("num", “Choose a #”, 1, 100, 50), plotOutput("hist"), verbatimTextOutput("sum"))server <- function(input, output) { data <- reactive({ rnorm(input$num) }) output$hist <- renderPlot({ hist(data()) }) output$sum <- renderPrint({ summary(data()) })}shinyApp(ui = ui, server = server)

Can we delay the reactions?

Page 131: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Makes a reactive object non-reactive.

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

isolate()

Page 132: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Makes a reactive object non-reactive.

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

isolate()

does nothingBuilds an object that:

Page 133: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Makes a reactive object non-reactive.

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

isolate()

does nothingBuilds an object that:

any reactive value wrapped by isolate

When notified by:

Page 134: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

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

○○

Page 135: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

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

○○

Page 136: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

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

○○

Page 137: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num

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

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

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

○○

Page 138: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Use…

reactive() to make an object to use in downstream code.

input$num

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

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

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

render() to make an object to display in the UI.

isolate() to return a non-reactive object.○

Page 139: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

eventReactive()Let’s you control when an expression is invalidated

Page 140: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

eventReactive()

notifies objects that use it that they are invalidBuilds an object that:

Let’s you control when an expression is invalidated

Page 141: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

eventReactive()

notifies objects that use it that they are invalidBuilds an object that:

this or these reactive value(s) and no others

When notified by:

Let’s you control when an expression is invalidated

Page 142: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num input$go

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

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

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

Page 143: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num input$go

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

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

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

Page 144: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num input$go

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

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

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

Page 145: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num input$go

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

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

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

Page 146: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num input$go

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

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

> hist(data()))

> rnorm(input$num)

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

Page 147: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num input$go

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

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

> hist(data()))

> rnorm(input$num)

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

Page 148: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

input$num input$go

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

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

> hist(data())) > summary(data())

> rnorm(input$num)

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

Page 149: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Action buttons

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

The value of an action button increases by one each time it is pressed.

Page 150: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Action buttons

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

input function

The value of an action button increases by one each time it is pressed.

Page 151: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Action buttons

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

input function

input name (for internal use)

Notice: Id not ID

The value of an action button increases by one each time it is pressed.

Page 152: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Action buttons

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

input function

label to display

input name (for internal use)

Notice: Id not ID

The value of an action button increases by one each time it is pressed.

Page 153: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Your Turn

slides at: bit.ly/rday-nyc-strata15

Add an actionButton() to the app. Then replace reactive() with eventReactive() so that the app only responds when the button is clicked.

Ensure that you can predict how the app will work.

Page 154: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

ui <- fluidPage( sliderInput("num", “Choose a number", 1, 100, 50), actionButton("go", "Update"), plotOutput("hist"), verbatimTextOutput("sum"))server <- function(input, output) { data <- eventReactive(input$go, {rnorm(input$num)}) output$hist <- renderPlot({ hist(data()) }) output$sum <- renderPrint({ summary(data()) })}shinyApp(ui = ui, server = server)

Page 155: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Use…

reactive() to make an object to use in downstream code.

input$num

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

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

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

render() to make an object to display in the UI.

isolate() to return a non-reactive object.○

eventReactive() to delay a reaction.Update

Page 156: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

demo

Page 157: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

ui <- fluidPage( sliderInput("num", "Slide Me", 1, 100, 50), actionButton("norm", "Normal Data"), actionButton("unif", "Uniform Data"), plotOutput("hist"), verbatimTextOutput("sum"))server <- function(input, output) { rv <- reactiveValues(data = rnorm(50)) observeEvent(input$norm, {rv$data <- rnorm(input$num)}) observeEvent(input$unif, {rv$data <- runif(input$num)}) output$hist <- renderPlot({hist(rv$data)}) output$sum <- renderPrint({summary(rv$data)})}shinyApp(ui = ui, server = server)

Page 158: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

ui <- fluidPage( sliderInput("num", "Slide Me", 1, 100, 50), actionButton("norm", "Normal Data"), actionButton("unif", "Uniform Data"), plotOutput("hist"), verbatimTextOutput("sum"))server <- function(input, output) { rv <- reactiveValues(data = rnorm(50)) observeEvent(input$norm, {rv$data <- rnorm(input$num)}) observeEvent(input$unif, {rv$data <- runif(input$num)}) output$hist <- renderPlot({hist(rv$data)}) output$sum <- renderPrint({summary(rv$data)})}shinyApp(ui = ui, server = server)

Page 159: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

observeEvent(input$norm, {rv$data <- rnorm(input$num)})

observeEvent()Triggers code to run.

Page 160: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

observeEvent(input$norm, {rv$data <- rnorm(input$num)})

observeEvent()

runs the code block (on the server side)Builds an object that:

Triggers code to run.

Page 161: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

observeEvent(input$norm, {rv$data <- rnorm(input$num)})

observeEvent()

runs the code block (on the server side)Builds an object that:

this or these reactive value(s) and no others

When notified by:

Triggers code to run.

Page 162: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

Page 163: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

Page 164: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

> rv$data <-+ rnorm(input$num)

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

Page 165: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

> rv$data <-+ rnorm(input$num)

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> rv$data <-+ runif(input$num)

Page 166: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

ui <- fluidPage( sliderInput("num", "Slide Me", 1, 100, 50), actionButton("norm", "Normal Data"), actionButton("unif", "Uniform Data"), plotOutput("hist"), verbatimTextOutput("sum"))server <- function(input, output) { rv <- reactiveValues(data = rnorm(50)) observeEvent(input$norm, {rv$data <- rnorm(input$num)}) observeEvent(input$unif, {rv$data <- runif(input$num)}) output$hist <- renderPlot({hist(rv$data)}) output$sum <- renderPrint({summary(rv$data)})}shinyApp(ui = ui, server = server)

Page 167: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

The input list contains values that change whenever a user changes an input.

input$num = 25

input$num = 50

input$num = 75

Reactive values

Page 168: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

The input list contains values that change whenever a user changes an input.

input$num = 25

input$num = 50

input$num = 75

Reactive values

You c

annot s

et the

se

value

s in yo

ur co

de

Page 169: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

reactiveValues()Creates a list of reactive values that you can manipulate

Page 170: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

reactiveValues()Creates a list of reactive values that you can manipulate

(optional) elements to add to the list

Page 171: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

reactiveValues()

Builds a list of objects that: notify objects that use them that the objects are invalid

Creates a list of reactive values that you can manipulate

(optional) elements to add to the list

Page 172: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

reactiveValues()

When their own value changesWhen:

Builds a list of objects that: notify objects that use them that the objects are invalid

Creates a list of reactive values that you can manipulate

(optional) elements to add to the list

Page 173: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

ui <- fluidPage( sliderInput("num", "Slide Me", 1, 100, 50), actionButton("norm", "Normal Data"), actionButton("unif", "Uniform Data"), plotOutput("hist"), verbatimTextOutput("sum"))server <- function(input, output) { rv <- reactiveValues(data = rnorm(50)) observeEvent(input$norm, {rv$data <- rnorm(input$num)}) observeEvent(input$unif, {rv$data <- runif(input$num)}) output$hist <- renderPlot({hist(rv$data)}) output$sum <- renderPrint({summary(rv$data)})}shinyApp(ui = ui, server = server)

Page 174: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

rv$datarnorm(50)

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

Page 175: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

rv$datarnorm(50)

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

> rv$data <-+ rnorm(input$num)

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

Page 176: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

rv$datarnorm(50)

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

> rv$data <-+ rnorm(input$num)

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

rv$datarnorm(

input$num)

Page 177: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

rv$datarnorm(50)

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

> rv$data <-+ rnorm(input$num)

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

rv$datarnorm(

input$num)

Page 178: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

> rv$data <- + rnorm(input$num)

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

rv$datarnorm(

input$num)

Page 179: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

> rv$data <- + rnorm(input$num)

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> hist(data()))

rv$datarnorm(

input$num)

Page 180: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

> rv$data <- + rnorm(input$num)

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> hist(data())) > summary(data())

rv$datarnorm(

input$num)

Page 181: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> rv$data <- + rnorm(input$num)

> hist(data())) > summary(data())

rv$datarnorm(

input$num)

Page 182: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

rv$datarnorm(

input$num)

Page 183: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> rv$data <-+ runif(input$num)

rv$datarnorm(

input$num)

Page 184: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> rv$data <-+ runif(input$num)

rv$datarnorm(

input$num)

rv$datarunif(

input$num)

Page 185: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> rv$data <-+ runif(input$num)

rv$datarnorm(

input$num)

rv$datarunif(

input$num)

Page 186: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> rv$data <- + runif(input$num)

rv$datarunif(

input$num)

Page 187: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> rv$data <- + runif(input$num)

> hist(data()))

rv$datarunif(

input$num)

Page 188: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

observeEvent( input$unif, { rv$data <- runif(input$num) })

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

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

input$unifinput$norm

observeEvent( input$norm, { rv$data <- rnorm(input$num) })

> rv$data <- + runif(input$num)

> hist(data())) > summary(data())

rv$datarunif(

input$num)

Page 189: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

reactive() to make an object to use in downstream code.

input$num

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

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

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

render() to make an object to display in the UI.

isolate() to return a non-reactive object.○

observeEvent() to trigger code.

eventReactive() to delay a reaction.Update

reactiveValues() to make your own reactive values.rv$data <-

Page 190: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

observe({rv$data <- rnorm(input$num)})

observe()Also triggers code to run on server.

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

Page 191: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

observe({rv$data <- rnorm(input$num)})

observe()

runs the code block (on the server side)Builds an object that:

Also triggers code to run on server. Uses same syntax as render*(), reactive(), and isolate()

Page 192: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

observe({rv$data <- rnorm(input$num)})

observe()

runs the code block (on the server side)Builds an object that:

any reactive value in the code blockWhen notified by:

Also triggers code to run on server. Uses same syntax as render*(), reactive(), and isolate()

Page 193: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Use…

reactive() to make an object to use in downstream code.

input$num

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

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

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

render() to make an object to display in the UI.

isolate() to return a non-reactive object.○

observeEvent() or observe() to trigger code.

eventReactive() to delay a reaction.Update

reactiveValues() to make your own reactive values.rv$data <-

Page 194: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Recap

input$x expression()

run(this)

Update

output$y

Prevent reactions isolate()

Trigger arbitrary code observeEvent()

observe()

Delay reactions eventReactive()

Create your own reactive values reactiveValues()

*Input()

Modularize reactions reactive()

Render reactive output

render*()

Page 195: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Interactive plots

Page 196: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Plots and images can be both outputs and inputs.

Page 197: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Plots and images can be both outputs and inputs.

Plots and images can be both outputs and inputs.

Page 198: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

demos from the Shiny Gallery

Page 199: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

To collect input values, add click, dblclick, hover, or brush arguments.

plotOutput()

plotOutput(…, click = "myclick")

Page 200: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

To collect input values, add click, dblclick, hover, or brush arguments.

plotOutput()

plotOutput(…, click = "myclick")

input$myclick

stores value as

Page 201: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Your TurnRun the app on the following slide or this fancier version:• shiny.calvin.edu/rpruim/ShinyDemos/InteractivePlots/

Then explore how the values of • clicked, • dblclicked, • hovered, and • brushed

change as you manipulate the plot with you mouse.

Page 202: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

ui <- fluidPage( plotOutput("plot", click = "click", dblclick = "dblclick", hover = "hover", brush = "brush"), fluidRow( column(3, "Clicked", verbatimTextOutput("clicked")), column(3, "Double Clicked", verbatimTextOutput("dblclicked")), column(3, "Hovered", verbatimTextOutput("hovered")), column(3, "Brushed", verbatimTextOutput("brushed")) )) server <- function(input, output) { output$plot <- renderPlot(qplot(wt, mpg, data = mtcars)) output$clicked <- renderPrint(input$click) output$dblclicked <- renderPrint(input$dblclick) output$hovered <- renderPrint(input$hover) output$brushed <- renderPrint(input$brush) } shinyApp(ui, server)

fancy version @ shiny.calvin.edu/rpruim/ShinyDemos/InteractivePlots/

Page 203: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

plotOutput()plotOutput(…, click = "click", dblclick = "dblclick", hover = "hover", brush = "brushed")

Location of double click (in x and y coordinates)Location of stationary mouse (in x and y)Bounding coordinates of brush box (in x and y)

Location of mouse click (in x and y coordinates)

Page 204: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

nearPoints(mtcars, input$click, xvar = "wt", yvar = "mpg", threshold = 5)

Returns a data frame of points near a clicknearPoints()

Page 205: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

nearPoints(mtcars, input$click, xvar = "wt", yvar = "mpg", threshold = 5)

Returns a data frame of points near a clicknearPoints()

data frame to return subset of (should match plot)

Page 206: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

nearPoints(mtcars, input$click, xvar = "wt", yvar = "mpg", threshold = 5)

Returns a data frame of points near a clicknearPoints()

data frame to return subset of (should match plot)

click input object

Page 207: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

nearPoints(mtcars, input$click, xvar = "wt", yvar = "mpg", threshold = 5)

Returns a data frame of points near a clicknearPoints()

data frame to return subset of (should match plot)

click input object

x variable in plot(not needed with ggplot2)

Page 208: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

nearPoints(mtcars, input$click, xvar = "wt", yvar = "mpg", threshold = 5)

Returns a data frame of points near a clicknearPoints()

data frame to return subset of (should match plot)

click input object

x variable in plot(not needed with ggplot2)

y variable in plot(not needed with ggplot2)

Page 209: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

nearPoints(mtcars, input$click, xvar = "wt", yvar = "mpg", threshold = 5)

Returns a data frame of points near a clicknearPoints()

data frame to return subset of (should match plot)

click input object

x variable in plot(not needed with ggplot2)

y variable in plot(not needed with ggplot2)

include points that fall within this many pixels of click

Page 210: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

brushedPoints(mtcars, input$brush, xvar = "wt", yvar = "mpg")

Returns a data frame of points within a brushed area

brushedPoints()

Page 211: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

brushedPoints(mtcars, input$brush, xvar = "wt", yvar = "mpg")

Returns a data frame of points within a brushed area

brushedPoints()

data frame to return subset of (should match plot)

Page 212: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

brushedPoints(mtcars, input$brush, xvar = "wt", yvar = "mpg")

Returns a data frame of points within a brushed area

brushedPoints()

data frame to return subset of (should match plot)

brush input object

Page 213: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

brushedPoints(mtcars, input$brush, xvar = "wt", yvar = "mpg")

Returns a data frame of points within a brushed area

brushedPoints()

data frame to return subset of (should match plot)

brush input object

x variable in plot(not needed with ggplot2)

Page 214: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

brushedPoints(mtcars, input$brush, xvar = "wt", yvar = "mpg")

Returns a data frame of points within a brushed area

brushedPoints()

data frame to return subset of (should match plot)

brush input object

x variable in plot(not needed with ggplot2)

y variable in plot(not needed with ggplot2)

Page 215: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Learn moreshiny.rstudio.com/articles

Page 216: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Share your app

%

Page 217: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

app.R

How to save your appOne directory with every file the app needs:• app.R (your script which ends with a call to shinyApp())• datasets, images, css, helper scripts, etc.

Page 218: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

app.R

How to save your appOne directory with every file the app needs:• app.R (your script which ends with a call to shinyApp())• datasets, images, css, helper scripts, etc.

You m

ust u

se th

is

exac

t nam

e (ap

p.R)

Page 219: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

shinyapps.io

%by

Page 220: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Property of

Shinyapps.ioA server maintained by RStudio

• easy to use• secure• scalable

Page 221: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Hassle-free cloud hosting for Shinywww.shinyapps.io

Page 222: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Hassle-free cloud hosting for Shinyshinyapps.io

Page 223: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Page 224: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Build your own server

%&

Page 225: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Property of

YOU 45 day evaluation free trial

Shiny Server Prowww.rstudio.com/products/shiny/shiny-server/

• Secure access LDAP, GoogleAuth, SSL, and more

• Performance fine tune at app and server level

• Management monitor and control resource use

• Support direct priority support

'

'

'

'

Page 226: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

WorkerWorkerWorkerWorkerWorker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Instance Instance

Application

Page 227: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Application

Page 228: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Application

Page 229: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Application

Page 230: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Application

Page 231: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Worker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Application

Page 232: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Worker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Application

Page 233: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Worker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Application

Page 234: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Worker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Worker

Instance

Application

Page 235: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Worker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Worker

Instance

Application

Page 236: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

WorkerWorker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Worker

Instance

Application

Page 237: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

WorkerWorkerWorker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Worker

Instance

Application

Page 238: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

WorkerWorkerWorker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Worker

Instance

Application

Page 239: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

WorkerWorkerWorker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Worker

Instance

Application

Page 240: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

WorkerWorkerWorker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Worker

Instance

Application

Page 241: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

WorkerWorkerWorker

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Worker

Instance

Worker

Instance

Application

Page 242: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Connections(End users)

Connections(End users)

Connections(End users)

Connections(End users)

Application

Page 243: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Advice for Big Data

Page 244: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

General Strategy1. Store data in out of memory warehouse2. Use an R Package to interact with warehouse

dplyr

Page 245: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Big Data and Shiny

Avoid unnecessary repetitions1.

Page 246: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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 247: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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)

WorkerWorker Worker

InstanceApplication

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

user user user user user

Page 248: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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)

WorkerWorker Worker

InstanceApplication

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

user user user user user

Code outside the server function will be run once

per R worker

Page 249: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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)

WorkerWorker Worker

InstanceApplication

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

user user user user user

Code outside the server function will be run once

per R worker

Code inside the server function will be run once per connection

Page 250: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

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)

WorkerWorker Worker

InstanceApplication

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

Update

user user user user user

Code outside the server function will be run once

per R worker

Code inside the server function will be run once per connection

Code inside of a reactive function will be run once

per reaction

Page 251: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Big Data and Shiny

Avoid unnecessary repetitions1.

Cache expensive operations with reactive expressions2.

Page 252: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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 253: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Big Data and Shiny

Avoid unnecessary repetitions1.

Cache expensive operations with reactive expressions2.

Delay expensive operations3.

Page 254: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

input$x expression()

run(this)

Update

output$y

Delay reactions eventReactive()

Page 255: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

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

eventReactive()

this or these reactive value(s) and no others

notifies objects that use it that they are invalidBuilds an object that:

When notified by:

Page 256: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

Where next?

Page 257: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

Add static elements

Lay out elements

z

x

y

Style elements with CSS

Page 258: Interactive Web Applications with RStudio and Shinyfiles.meetup.com/14454172/ShinySlides.pdf · ui and ) ), ) )

slides at: www.calvin.edu/~rpruim/talks/

shiny.rstudio.comThe Shiny Development Center