Building interactive web app with shiny

Post on 29-Nov-2014

3.310 views 0 download

description

It is a talk given by Vivian Zhang, CTO of SupStat Inc which is a leading Data Analytic consulting firm based in New York City, Shanghai and Beijing. NYC Open Data meetup are honored to host this event on Mar 24th,2014. You can find more information at www.meetup.com/nyc-open-data and www.nycopendata.com

Transcript of Building interactive web app with shiny

Building Interactive Web APP with ShinySupstat Inc.

OutlineIntroduction to shiny

Main functions of shiny

Advanced features of shiny

Share APP with others

Case study

·

·

·

·

·

2/30

Introduction to shiny

3/30

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

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

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

A simple examplelibrary(shiny)

runExample("01_hello")

·

·

7/30

A simple exampleWeb Application Layout

8/30

A simple exampleWeb Application Layout

9/30

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

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

Relationship of ui.R and server.R

12/30

Relationship of ui.R and server.R

13/30

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

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

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

Main functions

17/30

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

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

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

Advanced features

21/30

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

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

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

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

Share with others

26/30

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

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

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

30/30