an r shiny demo for ida mooc facilitation, developing data products

28
Developing Data Products A DEMO ON SHINY IDA MOOC TOH WEI ZHONG SEPTEMBER 2015

Upload: wei-zhong-toh

Post on 21-Feb-2017

565 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Developing Data ProductsA DEMO ON SHINY

IDA MOOC

TOH WEI ZHONG

SEPTEMBER 2015

A little bit about meGraduated from NUS, Computational Biology

◦ Statistics and computing onto biology and healthcare

◦ E.g. –omics

Data Scientist in NCS◦ Smart Nation projects (defense and public safety)

Agenda for this eveningAn introduction to shiny

◦ What is it about?

◦ Motivation

Basic concepts

A quick coding demo

An introduction to shiny

Shiny: Web application framework for RWeb application: any software application in which the client runs in a web browser

Can be constructed from a variety of languages and APIs◦ HTML, CSS, JavaScript

◦ Ruby, Ruby on Rails, PHP, etc. etc.

R and web applications? What’s the link?Science / communications

◦ Data visualization

◦ Interactivity on results

◦ E.g. Imagine scientific publications having a simple app to communicate results – phenomenon like autism/anti-vax shouldn’t even occur

◦ Compared to a static report / paper

R and web applications? What’s the link?Industrial

◦ POC (to sell analytics, dashboard as endpoint)

◦ Sell◦ “Tableau cannot do this type of modelling.”

◦ “SAS cannot do this type of visualization.”

◦ “We can.”

◦ “Wow!” factor

◦ Show senior mgmt. {deck + code} or {deck + app}?

Use-casesQ&A engine – “Ask a Doctor”

Call center dashboard (call volumes, repeated queries (market basket analysis), agent performances)

… anything with analytics as frontend, shiny can serve as backend

Basic concepts

Server.R and UI.RShiny apps fundamentally have 2 components

◦ Server.R

◦ UI.R

Server.R consists all R commands to be executed in the app

UI.R dictates how the UI appears

UI.RThe simpler half of the pair (Server.R and UI.R)

Has total control over how the UI looks like

Some of the things you can add:◦ Text and images

◦ Sliderbars

◦ Radio buttons

◦ Checkboxes

◦ Hyperlinks

UI.RshinyUI(fluidPage(

titlePanel(“This is the title”),

mainPanel(

tabsetPanel(

tabPanel(“One”),

tabPanel(“Two”)

)

)

))

UI.RshinyUI(fluidPage(

titlePanel(“This is the title”),

mainPanel(

tabsetPanel(

tabPanel(“One”,

sliderInput("id", "This is a slider", min = 1, max = 10, value = 5))),

tabPanel(“Two”)

)

)

))

User inputEach time we add a UI component that takes in input, we assign it to an ID.

The definition of the UI component includes an ID, which we define, eg.◦ sliderInput("id1", "This is a slider", min = 1, max = 10, value = 5)))

◦ checkboxInput(“id2”, “Check this?”, TRUE)

◦ radioButtons(“id3”, “Which one?", c(“This”= “this”, “That” = “that”)

Server.RWhat server.R does is to take all the inputs from the user (via the UI components), and execute the necessary

Essentially, the basic task of server.R is to define the relationship between the inputs and outputs

shinyServer(function(input,output){

output$plot <- renderPlot({

boxplot(mtcars[,input$id3])

})

})

shinyUI(fluidPage(

titlePanel("This is the title"),

mainPanel(

tabsetPanel(

tabPanel("One",

sliderInput("id", "This is a slider", min = 1, max = 10, value = 5),

checkboxInput("id2", "Check this?", TRUE),

radioButtons("id3", "Which variable of mtcars?", c("mpg"= "mpg", "disp" = "disp")),

plotOutput("plot")),

tabPanel("Two")

)

)

))

Server.R

UI.R

What’s happening?1. User select which radio button

2. User’s option is stored in “id3”

3. Server.R takes the value of “id3”, use it to generate a boxplot (render)

4. Generating and surfacing the boxplot are two different steps. To surface the boxplot on the UI, UI.R output the boxplot

Server.R Generate Render (renderPlot, renderTable)

UI.R Surface / actualize Output (plotOutput, tableOutput)

Coding demo

Tab panels

Slider bars

File import

Hyperlinks

CheckboxesRadio buttons

Images

Main & Sidebar panels

Text

Table output

Thanks! Questions?HT TP://SHINY.RSTUDIO.COM/GALLERY/

HT TPS://GITHUB.COM/TOHWEIZHONG/IDA -MOOC-SHINY

[email protected]

HT TPS://SG.L INKEDIN.COM/IN/TOHWEIZHONG