250 northern ave, boston, ma 02210 email: …...studio cc by rstudio 2015 follow @rstudio data...

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

Upload: others

Post on 17-Jan-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

Follow @rstudioCC by RStudio 2015

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

Studio

CC by RStudio 2015 Follow @rstudio

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

Garrett Grolemund

How to start with Shiny, Part 1

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

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

How to build a Shiny App

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

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

© CC 2015 RStudio, Inc.

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

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

How to start with Shiny

© CC 2015 RStudio, Inc.

1. How to build a Shiny app (Today)

2. How to customize reactions (May 27)

3. How to customize appearance (June 3)

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

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

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

© CC 2015 RStudio, Inc.

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

Shiny Showcase

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

Learn R

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

© CC 2015 RStudio, Inc.

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

More atwww.rstudio.com/resources/training/online-learning/

DataCamp

Books Videos Interactive tutorials

shop.oreilly.com/product/0636920028574.do

adv-r.had.co.nz/ shop.oreilly.com/product/0636920034834.do

shop.oreilly.com/product/0636920035992.do

www.datacamp.com

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

Understand the architecture

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

© CC 2015 RStudio, Inc.

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

Every Shiny app is maintained by a computer running R

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

© CC 2015 RStudio, Inc.

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

Every Shiny app is maintained by a computer running R

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

© CC 2015 RStudio, Inc.

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

Server Instructions User Interface (UI)

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

Use the template

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

© CC 2015 RStudio, Inc.

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

library(shiny)ui <- fluidPage()

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

App templateThe shortest viable shiny app

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

© CC 2015 RStudio, Inc.

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

Close an app

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

Add elements to your app as arguments to fluidPage()

© CC 2015 RStudio, Inc.

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

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

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

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

Build your app around

Inputs and Outputs

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

© CC 2015 RStudio, Inc.

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

Build your app around inputs and outputs

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

© CC 2015 RStudio, Inc.

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

Build your app around inputs and outputs

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

© CC 2015 RStudio, Inc.

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

Add elements to your app as arguments to fluidPage()

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

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

Inputs

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

© CC 2015 RStudio, Inc.

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

Create an input with an *Input() function.

<div class="form-group shiny-input-container"> <label class="control-label" for="num">Choose a number</label> <input class="js-range-slider" id="num" data-min="1" data-max="100" data-from="25" data-step="1" data-grid="true" data-grid-num="9.9" data-grid-snap="false" data-prettify-separator="," data-keyboard="true" data-keyboard-step="1.01010101010101"/> </div>

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

© CC 2015 RStudio, Inc.

library(shiny) ui <- fluidPage(

)

server <- function(input, output) {}

shinyApp(server = server, ui = ui)

Create an input with an input function.

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

© CC 2015 RStudio, Inc.

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

server <- function(input, output) {}

shinyApp(server = server, ui = ui)

Create an input with an input function.

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

© CC 2015 RStudio, Inc.

actionButton()submitButton()

checkboxInput() checkboxGroupInput() dateInput()

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

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

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

© CC 2015 RStudio, Inc.

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

Syntax

label to display

input name (for internal use)

Notice: Id not ID

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

input specific arguments

?sliderInput

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

Outputs

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

© CC 2015 RStudio, Inc.

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

Build your app around inputs and outputs

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

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

© CC 2015 RStudio, Inc.

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

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

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

© CC 2015 RStudio, Inc.

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

*Output()

plotOutput("hist")

name to give to the output object

the type of output to display

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

© CC 2015 RStudio, Inc.

library(shiny)

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

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

Comma between arguments

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

© CC 2015 RStudio, Inc.

library(shiny)

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

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

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

© CC 2015 RStudio, Inc.

library(shiny)

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

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

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

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

© CC 2015 RStudio, Inc.

library(shiny)

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

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

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

You must build the object in the server function

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

Display reactive results with an *Output() function

RecapBegin each app with the templatelibrary(shiny)

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

© CC 2015 RStudio, Inc.

Add elements as arguments to fluidPage()Hello World

Create reactive inputs with an *Input() function

Assemble outputs from inputs in the server function

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

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

Tell the

server how to assemble inputs into outputs

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

© CC 2015 RStudio, Inc.

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

server <- function(input, output) {

}

Use 3 rules to write the server function

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

© CC 2015 RStudio, Inc.

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

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

}

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

Save objects to display to output$1output$hist

plotOutput("hist")

© CC 2015 RStudio, Inc.

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

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

© CC 2015 RStudio, Inc.

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

Build objects to display with render*()

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

})}

2

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

function creates

renderDataTable() An interactive table

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

renderPlot() A plot

renderPrint() A code block of printed output

renderTable() A table

renderText() A character string

renderUI() a Shiny UI element © CC 2015 RStudio, Inc.

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

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

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

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

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

© CC 2015 RStudio, Inc.

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

renderPlot({ hist(rnorm(100)) })

render*()

code block that builds the object

Builds reactive output to display in UI

type of object to build

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

© CC 2015 RStudio, Inc.

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

Build objects to display with render*()

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

2

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

© CC 2015 RStudio, Inc.

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

Build objects to display with render*()

server <- function(input, output) { output$hist <- renderPlot({ title <- "100 random normal values" hist(rnorm(100), main = title) })}

2

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

© CC 2015 RStudio, Inc.

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

Access input values with input$

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

3

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

© CC 2015 RStudio, Inc.

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

sliderInput(inputId = "num",…)

input$num

Access input values with input$3

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

© CC 2015 RStudio, Inc.

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

The input value changes whenever a user changes the input.

input$num = 25

input$num = 50

input$num = 75

Input values

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

© CC 2015 RStudio, Inc.

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

The input value changes whenever a user changes the input.

input$num = 25

input$num = 50

input$num = 75

Input values

Output

will auto

matica

lly up

date

if you

follo

w the 3

rules

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

Slides at: bit.ly/shinyLONDON

© CC 2015 RStudio, Inc.

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

Reactivity 101

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

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

© CC 2015 RStudio, Inc.

input$num

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

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

© CC 2015 RStudio, Inc.

input$num

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

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

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)) })

© CC 2015 RStudio, Inc.

3. Access input values with input$input$num

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

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

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

Share your app

!

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

© CC 2015 RStudio, Inc.

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

Every Shiny app is maintained by a computer running R

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

© CC 2015 RStudio, Inc.

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

Every Shiny app is maintained by a computer running R

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

app.R

© CC 2015 RStudio, Inc.

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

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

# ui.Rlibrary(shiny)fluidPage( sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100), plotOutput("hist"))

© CC 2015 RStudio, Inc.

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

# server.Rlibrary(shiny)function(input, output) { output$hist <- renderPlot({ hist(rnorm(input$num)) })}

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)

Two file apps

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

© CC 2015 RStudio, Inc.

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

Two file appsOne directory with two files:• server.R

You m

ust u

se th

ese

exac

t nam

es

• ui.R

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

© CC 2015 RStudio, Inc.

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

Launch an app

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

© CC 2015 RStudio, Inc.

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

Display options

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

Use shinyapps.io

!by

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

© CC 2015 RStudio, Inc.

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

Property of

Shinyapps.ioA server maintained by RStudio• free• easy to use• secure• scalable

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

© CC 2015 RStudio, Inc.

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

Property of

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

Getting started guideshiny.rstudio.com/articles/shinyapps.html

© CC 2015 RStudio, Inc.

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

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

© CC 2015 RStudio, Inc.

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

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

Build your own server

!"

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

www.rstudio.com/products/shiny/shiny-server/A back end program that builds a linux web server specifically designed to host Shiny apps.

© CC 2015 RStudio, Inc.

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

Shiny Server Free!

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

© CC 2015 RStudio, Inc.

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

Shiny Serverwww.rstudio.com/products/shiny/shiny-server/

Pro• 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

####

45 day evaluation free trial

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

Recap: Sharing

© CC 2015 RStudio, Inc.

library(shinyapps) 2. Install the shinyapps package

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

Save your app in its own directory as app.R, or ui.R and server.R

app.R

1. Sign up for a free shinyapps.io account

Build your own server with Shiny Server or Shiny Server Pro!"

Host apps at shinyapps.io by:!by

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

Learn more

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

You now how to

© CC 2015 RStudio, Inc.

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

Build an app Create interactions

app.R

!"

Share your apps

output$

input$render*()

input$num

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

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

*Input()

*Output()

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

How to start with Shiny

© CC 2015 RStudio, Inc.

1. How to build a Shiny app (Today)

2. How to customize reactions (May 27)

3. How to customize appearance (June 3)

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

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

shiny.rstudio.com

© CC 2015 RStudio, Inc.

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

The Shiny Development Center