SlideShare a Scribd company logo
Build your own Web Application
with R shiny
Yuanyuan Zhang
Outline
• Why R shiny?
• What is R shiny?
• How to build a R shiny?
• Interesting R shiny examples
Motivation
• Statisticians often employ their analysis in R
software.
• Presenting/sharing their results are often
done in a static format.
Makes it easy to build interactive web apps
straight from R.
R shiny package
Streaming CRAN data shiny app
Access the app from:
https://gallery.shinyapps.io/087-crandash/
Covid-19 Tracker
Access the app from:
https://vac-lshtm.shinyapps.io/ncov_tracker/?_ga=2.94987164.1289489377.1620129325-
21034900.1616759223
Shiny Decisions
https://sparktuga.shinyapps.io/ShinyDecisions/?_ga=2.2819120.1289489377.
1620129325-21034900.1616759223
Calculator interface:
https://antoinesoetewey.shinyapps.io/statistics-202
• Interactivity – transform the analysis results
into interactive web pages.
• Flexibility – R shiny have multiple functions.
• Creativity – You can always built your own
“shiny” with your own design. Fully
customizable and extensible.
Difference from BI tools(Tableau): Cheap(No cost
needed), and basic R knowledge is needed.
What is a Shiny App?
• A Shiny app is a web page (UI) connected to a
computer/server running a live R session (Shiny Server).
• Users can manipulate the UI, the shiny server will run the R
script to update the UI displays at the same time.
Shiny Structure
• Shiny Apps can be developed with the following template in R:
app.R:
> library(shiny)
> ui <- fluidPage()
> server <- function(input,output)
> shinyApp(ui,server)
• ui: Nested R functions that assemble an HTML user interface for the 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.
Example
• > library(shiny)
> ui <- fluidPage(
+ numericInput(inputId="n","Sample size",value=50),
+ plotOutput(outputId= "hist1"))
> server <- function(input, output) {
+ output$hist1 <- renderPlot({hist(rnorm(input$n))})
+ }
> shinyApp(ui,server)
• Access the current value of an input object with input$ < inputId >
• Access the developed output of an output object with output$ < outputId>
Different input functions are available:
Different output functions are available:
• Before:
• After:
• New code:
ui <- fluidPage(
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
numericInput(inputId="n","Sample size",value=50),submitButton(),
# adding the new div tag to the sidebar
tags$div(class="header", checked=NA,
tags$p(strong("Ready to take the Shiny tutorial? If so")),
tags$a(href="https://shiny.rstudio.com/tutorial/", "Click Here!")
)),
# Main panel for displaying outputs ----
mainPanel(
plotOutput(outputId= "hist1"))))
server <- function(input, output) {
output$hist1 <- renderPlot({hist(rnorm(input$n))})
}
shinyApp(ui,server)
Static HTML elements can be added with tags, a list of functions that parallel
common HTML tags, e.g. tags$a()
More details can be found : https://shiny.rstudio.com/articles/tag-glossary.html
Layout of UI
• Different panels are available:
• Common layouts
• Layer tabPanels on top of each other, and navigate between them
R shiny:
• Web interface for R
• Designed to deliver small apps
• Simple rules based layout structures
Step by step to build a shiny app
• Analyse the irish dataset by creating three tabsets in the main panel. The
first one contains a histogram per chosen variable (variable name/bin
size/color), while the second one displays a summary output of all
variables and the third one displays the data table. Variable selection is
obtained by a select input.
• Interface Design:
Sidebar
Panel (select
input for
different
variables)
Histogram Summary Data
• Step 1: Load the package and iris data
install.packages("shiny")
library(shiny)
iris
str(iris)
head(iris)
summary(iris)
• Step 2: Build the basic framework for R shiny
ui <- fluidPage(
titlePanel(title = "Shiny App for Iris!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
),
# Main panel for displaying outputs
mainPanel(
)
)
)
server <- function(input,output){}
shinyApp(ui,server)
• Step 3: Create the input structure in the sidebar panel
ui <- fluidPage(
titlePanel(title = "Shiny App for Iris!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
selectInput(inputId="var","Select the variables",choices =
c("Sepal.Length"=1,"Sepal.Width"=2,"Petal.Length"=3,"Petal.Width"=4),selected=3,selectize = FALSE),
sliderInput(inputId="bin","Select the number of bins for histogram",min=5, max=25,value=10),
radioButtons(inputId="colour",label="Select the colour of the histogram",choices =
c("blue","yellow","red"),selected="yellow")
),
# Main panel for displaying outputs ----
mainPanel(
)
)
)
server <- function(input,output){}
shinyApp(ui,server)
• Step 4: Create tabsets with outputs in the UI main panel
ui <- fluidPage(
titlePanel(title = "Shiny App for Iris!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
selectInput(inputId="var","Select the variables",choices =
c("Sepal.Length"=1,"Sepal.Width"=2,"Petal.Length"=3,"Petal.Width"=4),selected=3,selectize = FALSE),
sliderInput(inputId="bin","Select the number of bins for histogram",min=5, max=25,value=10),
radioButtons(inputId="colour",label="Select the colour of the histogram",choices =
c("blue","yellow","red"),selected="yellow")
),
# Main panel for displaying outputs ----
mainPanel(
tabsetPanel(type="tab",
tabPanel("Histogram",textOutput(outputId="text"),plotOutput(outputId="hist")),
tabPanel("Data",DTOutput(outputId ="Data_Iris")),
tabPanel("Summary",tableOutput(outputId="summary"))
)
)
)
)
server <- function(input,output){}
shinyApp(ui,server)
• Step 5: Write up the server part
ui <- fluidPage(
titlePanel(title = "Shiny App for Iris!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
selectInput(inputId="var","Select the variables",choices = c("Sepal.Length"=1,"Sepal.Width"=2,"Petal.Length"=3,"Petal.Width"=4),selected=3,selectize =
FALSE),
sliderInput(inputId="bin","Select the number of bins for histogram",min=5, max=25,value=10),
radioButtons(inputId="colour",label="Select the colour of the histogram",choices = c("blue","yellow","red"),selected="yellow")
),
# Main panel for displaying outputs ----
mainPanel(
tabsetPanel(type="tab",
tabPanel("Histogram",textOutput(outputId="text"),plotOutput(outputId="hist")),
tabPanel("Data",DTOutput(outputId ="Data_Iris")),
tabPanel("Summary", verbatimTextOutput(outputId="summary"))
))))
server <- function(input,output){
output$Data_Iris <- renderDT({
datatable(iris)
})
output$text <- renderText({
col=as.numeric(input$var)
paste("The variable names you choose here is ", names(iris[col]))
})
output$hist <- renderPlot({
col=as.numeric(input$var)
hist(iris[,col],col=input$colour,xlim=c(0,max(iris[,col])),breaks=seq(0,max(iris[,col]),l=input$bin+1),xlab=names(iris[col]),main="Histogram of Iris
dataset")
})
output$summary <-renderPrint({
summary(iris)
}) }
shinyApp(ui,server)
Extension to dashboard shells
• Build-in UI framework of Shiny is rather simple .
• The layout of interface is neat but plain.
• Customize your UI with HTML, CSS and Javascript widgets (Time
consuming).
shinydashboard
package
Basics of shinydashboard
install.packages("shinydashboard")
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody() )
server <- function(input, output) { }
shinyApp(ui, server)
Load the packages and data
install.packages("shinydashboard")
install.packages("Dt")
library(shiny)
library(shinydashboard)
head(iris)
head(mtcars)
ui = dashboardPage(skin="green",
dashboardHeader(title="Shiny dashboard"),
dashboardSidebar(
sidebarMenu(
menuItem("Iris", tabName = "iris", icon = icon("tree")),
menuItem("Cars", tabName = "cars", icon = icon("car"))
)
),
dashboardBody(
tabItems(
tabItem("iris",
box(selectInput("variables","Variables:",choices=c("Sepal.Width","Petal.Length","Petal.Width")),width=4),
box(plotOutput("Correlation_iris"),width=8)
)
,
tabItem("cars",
fluidPage(
h1("Cars data table:"),
DTOutput(outputId ="Data_car")
)
)
)
)
)
server = function(input, output) {
output$Correlation_iris <- renderPlot({
plot(iris$Sepal.Length,iris[[input$variables]],xlab="Sepal Length",ylab="Variables")
})
output$Data_car <- renderDT({ mtcars
})
}
shinyApp(ui,server )
Learn more here:
• R shiny tutorial: https://shiny.rstudio.com/tutorial/
https://shiny.rstudio.com/tutorial/written-tutorial/
https://shiny.rstudio.com/gallery/
• R shiny dashboard :
https://rstudio.github.io/shinydashboard/get_started.html
https://rstudio.github.io/shinydashboard/structure.html
• Popular shiny app examples:
https://blog.rstudio.com/2020/07/13/winners-of-the-2nd-shiny-contest/
shiny_v1.pptx

More Related Content

Similar to shiny_v1.pptx

Start your app the better way with Styled System
Start your app the better way with Styled SystemStart your app the better way with Styled System
Start your app the better way with Styled System
Hsin-Hao Tang
 
Web components with Angular
Web components with AngularWeb components with Angular
Web components with Angular
Ana Cidre
 
Creating sub zero dashboard plugin for apex with google
Creating sub zero dashboard plugin for apex with googleCreating sub zero dashboard plugin for apex with google
Creating sub zero dashboard plugin for apex with google
Roel Hartman
 
Building Multi-Tenant and SaaS products in PHP - CloudConf 2015
Building Multi-Tenant and SaaS products in PHP - CloudConf 2015Building Multi-Tenant and SaaS products in PHP - CloudConf 2015
Building Multi-Tenant and SaaS products in PHP - CloudConf 2015
Innomatic Platform
 
Digital analytics with R - Sydney Users of R Forum - May 2015
Digital analytics with R - Sydney Users of R Forum - May 2015Digital analytics with R - Sydney Users of R Forum - May 2015
Digital analytics with R - Sydney Users of R Forum - May 2015
Johann de Boer
 
Cordova: Making Native Mobile Apps With Your Web Skills
Cordova: Making Native Mobile Apps With Your Web SkillsCordova: Making Native Mobile Apps With Your Web Skills
Cordova: Making Native Mobile Apps With Your Web Skills
Clay Ewing
 
U-SQL Query Execution and Performance Tuning
U-SQL Query Execution and Performance TuningU-SQL Query Execution and Performance Tuning
U-SQL Query Execution and Performance Tuning
Michael Rys
 
MOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentMOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app development
anistar sung
 
How to grow GraphQL and remove SQLAlchemy and REST API from a high-load Pytho...
How to grow GraphQL and remove SQLAlchemy and REST API from a high-load Pytho...How to grow GraphQL and remove SQLAlchemy and REST API from a high-load Pytho...
How to grow GraphQL and remove SQLAlchemy and REST API from a high-load Pytho...
Oleksandr Tarasenko
 
A Tour of Building Web Applications with R Shiny
A Tour of Building Web Applications with R Shiny A Tour of Building Web Applications with R Shiny
A Tour of Building Web Applications with R Shiny
Wendy Chen Dubois
 
Design Summit - UI Roadmap - Dan Clarizio, Martin Povolny
Design Summit - UI Roadmap - Dan Clarizio, Martin PovolnyDesign Summit - UI Roadmap - Dan Clarizio, Martin Povolny
Design Summit - UI Roadmap - Dan Clarizio, Martin Povolny
ManageIQ
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
Wim Godden
 
mobl
moblmobl
mobl
zefhemel
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Windows Developer
 
IFESFinal58
IFESFinal58IFESFinal58
IFESFinal58anish h
 
Building a Single Page Application using Ember.js ... for fun and profit
Building a Single Page Application using Ember.js ... for fun and profitBuilding a Single Page Application using Ember.js ... for fun and profit
Building a Single Page Application using Ember.js ... for fun and profit
Ben Limmer
 
German introduction to sp framework
German   introduction to sp frameworkGerman   introduction to sp framework
German introduction to sp framework
Bob German
 
Laravel5 Introduction and essentials
Laravel5 Introduction and essentialsLaravel5 Introduction and essentials
Laravel5 Introduction and essentials
Pramod Kadam
 
Building Angular 2.0 applications with TypeScript
Building Angular 2.0 applications with TypeScriptBuilding Angular 2.0 applications with TypeScript
Building Angular 2.0 applications with TypeScript
MSDEVMTL
 
Introduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in RIntroduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in R
Paul Richards
 

Similar to shiny_v1.pptx (20)

Start your app the better way with Styled System
Start your app the better way with Styled SystemStart your app the better way with Styled System
Start your app the better way with Styled System
 
Web components with Angular
Web components with AngularWeb components with Angular
Web components with Angular
 
Creating sub zero dashboard plugin for apex with google
Creating sub zero dashboard plugin for apex with googleCreating sub zero dashboard plugin for apex with google
Creating sub zero dashboard plugin for apex with google
 
Building Multi-Tenant and SaaS products in PHP - CloudConf 2015
Building Multi-Tenant and SaaS products in PHP - CloudConf 2015Building Multi-Tenant and SaaS products in PHP - CloudConf 2015
Building Multi-Tenant and SaaS products in PHP - CloudConf 2015
 
Digital analytics with R - Sydney Users of R Forum - May 2015
Digital analytics with R - Sydney Users of R Forum - May 2015Digital analytics with R - Sydney Users of R Forum - May 2015
Digital analytics with R - Sydney Users of R Forum - May 2015
 
Cordova: Making Native Mobile Apps With Your Web Skills
Cordova: Making Native Mobile Apps With Your Web SkillsCordova: Making Native Mobile Apps With Your Web Skills
Cordova: Making Native Mobile Apps With Your Web Skills
 
U-SQL Query Execution and Performance Tuning
U-SQL Query Execution and Performance TuningU-SQL Query Execution and Performance Tuning
U-SQL Query Execution and Performance Tuning
 
MOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentMOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app development
 
How to grow GraphQL and remove SQLAlchemy and REST API from a high-load Pytho...
How to grow GraphQL and remove SQLAlchemy and REST API from a high-load Pytho...How to grow GraphQL and remove SQLAlchemy and REST API from a high-load Pytho...
How to grow GraphQL and remove SQLAlchemy and REST API from a high-load Pytho...
 
A Tour of Building Web Applications with R Shiny
A Tour of Building Web Applications with R Shiny A Tour of Building Web Applications with R Shiny
A Tour of Building Web Applications with R Shiny
 
Design Summit - UI Roadmap - Dan Clarizio, Martin Povolny
Design Summit - UI Roadmap - Dan Clarizio, Martin PovolnyDesign Summit - UI Roadmap - Dan Clarizio, Martin Povolny
Design Summit - UI Roadmap - Dan Clarizio, Martin Povolny
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 
mobl
moblmobl
mobl
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
 
IFESFinal58
IFESFinal58IFESFinal58
IFESFinal58
 
Building a Single Page Application using Ember.js ... for fun and profit
Building a Single Page Application using Ember.js ... for fun and profitBuilding a Single Page Application using Ember.js ... for fun and profit
Building a Single Page Application using Ember.js ... for fun and profit
 
German introduction to sp framework
German   introduction to sp frameworkGerman   introduction to sp framework
German introduction to sp framework
 
Laravel5 Introduction and essentials
Laravel5 Introduction and essentialsLaravel5 Introduction and essentials
Laravel5 Introduction and essentials
 
Building Angular 2.0 applications with TypeScript
Building Angular 2.0 applications with TypeScriptBuilding Angular 2.0 applications with TypeScript
Building Angular 2.0 applications with TypeScript
 
Introduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in RIntroduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in R
 

Recently uploaded

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
ShivajiThube2
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 

shiny_v1.pptx

  • 1. Build your own Web Application with R shiny Yuanyuan Zhang
  • 2. Outline • Why R shiny? • What is R shiny? • How to build a R shiny? • Interesting R shiny examples
  • 3. Motivation • Statisticians often employ their analysis in R software. • Presenting/sharing their results are often done in a static format. Makes it easy to build interactive web apps straight from R. R shiny package
  • 5. Access the app from: https://gallery.shinyapps.io/087-crandash/
  • 7. Access the app from: https://vac-lshtm.shinyapps.io/ncov_tracker/?_ga=2.94987164.1289489377.1620129325- 21034900.1616759223
  • 8.
  • 11. • Interactivity – transform the analysis results into interactive web pages. • Flexibility – R shiny have multiple functions. • Creativity – You can always built your own “shiny” with your own design. Fully customizable and extensible. Difference from BI tools(Tableau): Cheap(No cost needed), and basic R knowledge is needed.
  • 12. What is a Shiny App? • A Shiny app is a web page (UI) connected to a computer/server running a live R session (Shiny Server). • Users can manipulate the UI, the shiny server will run the R script to update the UI displays at the same time.
  • 13. Shiny Structure • Shiny Apps can be developed with the following template in R: app.R: > library(shiny) > ui <- fluidPage() > server <- function(input,output) > shinyApp(ui,server) • ui: Nested R functions that assemble an HTML user interface for the 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.
  • 14. Example • > library(shiny) > ui <- fluidPage( + numericInput(inputId="n","Sample size",value=50), + plotOutput(outputId= "hist1")) > server <- function(input, output) { + output$hist1 <- renderPlot({hist(rnorm(input$n))}) + } > shinyApp(ui,server) • Access the current value of an input object with input$ < inputId > • Access the developed output of an output object with output$ < outputId>
  • 15. Different input functions are available:
  • 16. Different output functions are available:
  • 18. • New code: ui <- fluidPage( titlePanel("Hello Shiny!"), # Sidebar layout with input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( numericInput(inputId="n","Sample size",value=50),submitButton(), # adding the new div tag to the sidebar tags$div(class="header", checked=NA, tags$p(strong("Ready to take the Shiny tutorial? If so")), tags$a(href="https://shiny.rstudio.com/tutorial/", "Click Here!") )), # Main panel for displaying outputs ---- mainPanel( plotOutput(outputId= "hist1")))) server <- function(input, output) { output$hist1 <- renderPlot({hist(rnorm(input$n))}) } shinyApp(ui,server)
  • 19. Static HTML elements can be added with tags, a list of functions that parallel common HTML tags, e.g. tags$a() More details can be found : https://shiny.rstudio.com/articles/tag-glossary.html
  • 20. Layout of UI • Different panels are available: • Common layouts
  • 21. • Layer tabPanels on top of each other, and navigate between them R shiny: • Web interface for R • Designed to deliver small apps • Simple rules based layout structures
  • 22. Step by step to build a shiny app • Analyse the irish dataset by creating three tabsets in the main panel. The first one contains a histogram per chosen variable (variable name/bin size/color), while the second one displays a summary output of all variables and the third one displays the data table. Variable selection is obtained by a select input. • Interface Design: Sidebar Panel (select input for different variables) Histogram Summary Data
  • 23. • Step 1: Load the package and iris data install.packages("shiny") library(shiny) iris str(iris) head(iris) summary(iris)
  • 24. • Step 2: Build the basic framework for R shiny ui <- fluidPage( titlePanel(title = "Shiny App for Iris!"), # Sidebar layout with input and output definitions ---- sidebarLayout( # Sidebar panel for inputs sidebarPanel( ), # Main panel for displaying outputs mainPanel( ) ) ) server <- function(input,output){} shinyApp(ui,server)
  • 25. • Step 3: Create the input structure in the sidebar panel ui <- fluidPage( titlePanel(title = "Shiny App for Iris!"), # Sidebar layout with input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( selectInput(inputId="var","Select the variables",choices = c("Sepal.Length"=1,"Sepal.Width"=2,"Petal.Length"=3,"Petal.Width"=4),selected=3,selectize = FALSE), sliderInput(inputId="bin","Select the number of bins for histogram",min=5, max=25,value=10), radioButtons(inputId="colour",label="Select the colour of the histogram",choices = c("blue","yellow","red"),selected="yellow") ), # Main panel for displaying outputs ---- mainPanel( ) ) ) server <- function(input,output){} shinyApp(ui,server)
  • 26. • Step 4: Create tabsets with outputs in the UI main panel ui <- fluidPage( titlePanel(title = "Shiny App for Iris!"), # Sidebar layout with input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( selectInput(inputId="var","Select the variables",choices = c("Sepal.Length"=1,"Sepal.Width"=2,"Petal.Length"=3,"Petal.Width"=4),selected=3,selectize = FALSE), sliderInput(inputId="bin","Select the number of bins for histogram",min=5, max=25,value=10), radioButtons(inputId="colour",label="Select the colour of the histogram",choices = c("blue","yellow","red"),selected="yellow") ), # Main panel for displaying outputs ---- mainPanel( tabsetPanel(type="tab", tabPanel("Histogram",textOutput(outputId="text"),plotOutput(outputId="hist")), tabPanel("Data",DTOutput(outputId ="Data_Iris")), tabPanel("Summary",tableOutput(outputId="summary")) ) ) ) ) server <- function(input,output){} shinyApp(ui,server)
  • 27. • Step 5: Write up the server part ui <- fluidPage( titlePanel(title = "Shiny App for Iris!"), # Sidebar layout with input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( selectInput(inputId="var","Select the variables",choices = c("Sepal.Length"=1,"Sepal.Width"=2,"Petal.Length"=3,"Petal.Width"=4),selected=3,selectize = FALSE), sliderInput(inputId="bin","Select the number of bins for histogram",min=5, max=25,value=10), radioButtons(inputId="colour",label="Select the colour of the histogram",choices = c("blue","yellow","red"),selected="yellow") ), # Main panel for displaying outputs ---- mainPanel( tabsetPanel(type="tab", tabPanel("Histogram",textOutput(outputId="text"),plotOutput(outputId="hist")), tabPanel("Data",DTOutput(outputId ="Data_Iris")), tabPanel("Summary", verbatimTextOutput(outputId="summary")) )))) server <- function(input,output){ output$Data_Iris <- renderDT({ datatable(iris) }) output$text <- renderText({ col=as.numeric(input$var) paste("The variable names you choose here is ", names(iris[col])) }) output$hist <- renderPlot({ col=as.numeric(input$var) hist(iris[,col],col=input$colour,xlim=c(0,max(iris[,col])),breaks=seq(0,max(iris[,col]),l=input$bin+1),xlab=names(iris[col]),main="Histogram of Iris dataset") }) output$summary <-renderPrint({ summary(iris) }) } shinyApp(ui,server)
  • 28.
  • 29. Extension to dashboard shells • Build-in UI framework of Shiny is rather simple . • The layout of interface is neat but plain. • Customize your UI with HTML, CSS and Javascript widgets (Time consuming). shinydashboard package
  • 30. Basics of shinydashboard install.packages("shinydashboard") library(shiny) library(shinydashboard) ui <- dashboardPage( dashboardHeader(), dashboardSidebar(), dashboardBody() ) server <- function(input, output) { } shinyApp(ui, server)
  • 31.
  • 32. Load the packages and data install.packages("shinydashboard") install.packages("Dt") library(shiny) library(shinydashboard) head(iris) head(mtcars)
  • 33. ui = dashboardPage(skin="green", dashboardHeader(title="Shiny dashboard"), dashboardSidebar( sidebarMenu( menuItem("Iris", tabName = "iris", icon = icon("tree")), menuItem("Cars", tabName = "cars", icon = icon("car")) ) ), dashboardBody( tabItems( tabItem("iris", box(selectInput("variables","Variables:",choices=c("Sepal.Width","Petal.Length","Petal.Width")),width=4), box(plotOutput("Correlation_iris"),width=8) ) , tabItem("cars", fluidPage( h1("Cars data table:"), DTOutput(outputId ="Data_car") ) ) ) ) )
  • 34. server = function(input, output) { output$Correlation_iris <- renderPlot({ plot(iris$Sepal.Length,iris[[input$variables]],xlab="Sepal Length",ylab="Variables") }) output$Data_car <- renderDT({ mtcars }) } shinyApp(ui,server )
  • 35. Learn more here: • R shiny tutorial: https://shiny.rstudio.com/tutorial/ https://shiny.rstudio.com/tutorial/written-tutorial/ https://shiny.rstudio.com/gallery/ • R shiny dashboard : https://rstudio.github.io/shinydashboard/get_started.html https://rstudio.github.io/shinydashboard/structure.html • Popular shiny app examples: https://blog.rstudio.com/2020/07/13/winners-of-the-2nd-shiny-contest/