Download - Building interactive web app with shiny

Transcript
Page 1: Building interactive web app with shiny

Building Interactive Web APP with ShinySupstat Inc.

Page 2: Building interactive web app with shiny

OutlineIntroduction to shiny

Main functions of shiny

Advanced features of shiny

Share APP with others

Case study

·

·

·

·

·

2/30

Page 3: Building interactive web app with shiny

Introduction to shiny

3/30

Page 4: Building interactive web app with shiny

Why build web applicationR is great, and the Internet is great.

Everyone has a browser.

Communicate your results dynamically.

Create custom analytics tools to explore data.

·

·

·

·

4/30

Page 5: Building interactive web app with shiny

Introduction to shinyOpen Sourced by RStudio November 2012

Not the first to get R in the browser (rApache, Rserve, Rook)

Default widgets and settings make it easy to generate apps

Don't need to know HTML, CSS and javascript to get started

Twitter Bootstrap for default UI - looks good

Web sockets for communication between client and server

Reactive Programming model

Works on Windows, Mac, Linux

·

·

·

·

·

·

·

·

5/30

Page 6: Building interactive web app with shiny

Introduction to shinyReady to use shiny

Install R from CRAN

Useful to have Chrome, Firefox, Safari...

Rstudio or other texteditor

Install Shiny using R command: install.packages("shiny")

·

·

·

·

6/30

Page 7: Building interactive web app with shiny

A simple examplelibrary(shiny)

runExample("01_hello")

·

·

7/30

Page 8: Building interactive web app with shiny

A simple exampleWeb Application Layout

8/30

Page 9: Building interactive web app with shiny

A simple exampleWeb Application Layout

9/30

Page 10: Building interactive web app with shiny

ui.R : Controls the look of the Applibrary(shiny)# Define UI for application that plots random distributions shinyUI(pageWithSidebar( # Application title headerPanel("Hello Shiny!"), # Sidebar with a slider input for number of observations sidebarPanel( sliderInput("obs", "Number of observations:", min = 0, max = 1000, value = 500) ), # Show a plot of the generated distribution mainPanel( plotOutput("distPlot") )))

10/30

Page 11: Building interactive web app with shiny

server.R : Specifies what R is doing# Define server logic required to generate and plot a random distributionshinyServer(function(input, output) {

# Expression that generates a plot of the distribution. The expression # is wrapped in a call to renderPlot to indicate that: # # 1) It is "reactive" and therefore should be automatically # re-executed when inputs change # 2) Its output type is a plot # output$distPlot <- renderPlot({ # generate an rnorm distribution and plot it dist <- rnorm(input$obs) hist(dist) })})

11/30

Page 12: Building interactive web app with shiny

Relationship of ui.R and server.R

12/30

Page 13: Building interactive web app with shiny

Relationship of ui.R and server.R

13/30

Page 14: Building interactive web app with shiny

Reactive ProgrammingSimilar to a spreadsheet. If you have a cell that is a function of another cell, e.g. "=A1*2", then anytime A1 is updated the cell that contains that function is automatically updated.

14/30

Page 15: Building interactive web app with shiny

Reactive ProgrammingSimilar to a spreadsheet. If you have a cell that is a function of another cell, e.g. "=A1*2", then anytime A1 is updated the cell that contains that function is automatically updated.

15/30

Page 16: Building interactive web app with shiny

Reactive ProgrammingSimilar to a spreadsheet. If you have a cell that is a function of another cell, e.g. "=A1*2", then anytime A1 is updated the cell that contains that function is automatically updated.

16/30

Page 17: Building interactive web app with shiny

Main functions

17/30

Page 18: Building interactive web app with shiny

Functions for InputsliderInput: Constructs a slider widget to select a numeric value from a range.

selectInput: Create a select list that can be used to choose a single or multiple items from a listof values.

numericInput: Create an input control for entry of numeric values

checkboxInput: Create a checkbox that can be used to specify logical values.

radioButtons: Create a set of radio buttons used to select an item from a list.

textInput: Create an input control for entry of unstructured text values

·

runExample("01_hello")-

·

runExample("02_text")-

·

runExample("02_text")-

·

runExample("04_mpg")-

·

runExample("06_tabsets")-

·

runExample("02_text")-

18/30

Page 19: Building interactive web app with shiny

Functions for Processingreactive: Wraps a normal expression to create a reactive expression.

renderPlot: Renders a reactive plot that is suitable for assigning to an output slot.

renderPrint: Makes a reactive version of the given function that captures any printed output.

renderTable: Creates a reactive table that is suitable for assigning to an output slot

renderText: Makes a reactive version of the given function that also uses cat to turn its resultinto a single element character vector.

·

runExample("02_text")-

·

runExample("01_hello")-

·

runExample("02_text")-

·

runExample("02_text")-

·

runExample("03_reactivity")-

19/30

Page 20: Building interactive web app with shiny

Functions for OutputplotOutput: Render a renderPlot within an application page.

tableOutput: Render a renderTable within an application page.

verbatimTextOutput: Render a reactive output variable as verbatim text within an applicationpage.

textOutput: Render a reactive output variable as text within an application page.

·

runExample("01_hello")-

·

runExample("02_text")-

·

runExample("02_text")-

·

runExample("02_text")-

20/30

Page 21: Building interactive web app with shiny

Advanced features

21/30

Page 22: Building interactive web app with shiny

Tab PanelsTabsets are created by calling the tabsetPanel function with a list of tabs created by the tabPanelfunction. Each tab panel is provided a list of output elements which are rendered vertically withinthe tab.

·

runExample("06_tabsets")-

22/30

Page 23: Building interactive web app with shiny

UI EnhancementsWe added a helpText control to provide additional clarifying text alongside our input controls.

We added a submitButton control to indicate that we don’t want a live connection betweeninputs and outputs. This is especially useful if computing output is computationally expensive.

We added h4 elements into the output pane. Shiny offers a variety of functions for including HTMLelements directly in pagesS.

·

·

·

runExample("07_widgets")-

23/30

Page 24: Building interactive web app with shiny

Uploading FilesFile upload controls are created by using the fileInput function in your ui.R file.

access the uploaded data similarly to other types of input: by referring to input$inputId.

The file contents can be accessed by reading the file named by the datapath column

·

·

·

runExample("09_upload")-

24/30

Page 25: Building interactive web app with shiny

Dynamic UIThe conditionalPanel function, which is used in ui.R and wraps a set of UI elements that needto be dynamically shown/hidden

The renderUI function, which is used in server.R in conjunction with the htmlOutput function inui.R, lets you generate calls to UI functions and make the results appear in a predetermined placein the UI

·

·

runGist("https://gist.github.com/wch/4034323")-

25/30

Page 26: Building interactive web app with shiny

Share with others

26/30

Page 27: Building interactive web app with shiny

Sharing Apps to Run LocallyGist One easy way is to put your code on gist.github.com, a code pasteboard service from GitHub.Both server.R and ui.R must be included in the same gist, and you must use their properfilenames. See https://gist.github.com/3239667 for an example.

GitHub repository If your project is stored in a git repository on GitHub, then others can downloadand run your app directly. An example repository is at https://github.com/rstudio/shiny_example.

·

·

27/30

Page 28: Building interactive web app with shiny

Deploying Over the WebSelf-hosted Shiny Server With our Shiny Server software, you can deploy Shiny applications overthe web so that users need only a web browser and your application’s URL. You’ll need a Linuxserver and Shiny Server.

RStudio-hosted Shiny Server Want to deploy over the web but prefer not to run your own server?We’re currently beta testing a subscription-based hosting service for Shiny.

·

·

28/30

Page 29: Building interactive web app with shiny

creat a app which could upload csv data and build linear regression model

30/30