Mahbubul Majumder, PhD
Nov 4, 2014
A web application framework for R
Can be used to create data product
D3 plots can be included in shiny application
Can be easily shared in various ways
install.packages("shiny")
library(shiny)
You need two R
source files saved in a folder say myShinyApp
. Source files should be named
server.R
ui.R
server.R
contains the R
codes to
ui.R
contains the R
codes to
Two ways to run your application
runApp("path-to/myShinyApp")
Run App
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
# Expression that generates a histogram. The expression is
# wrapped in a call to renderPlot to indicate that:
#
# 1) It is "reactive" and therefore should re-execute automatically
# when inputs change
# 2) Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Hello Shiny!"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
functions | widget |
---|---|
actionButton | Action Button |
checkboxGroupInput | A group of check boxes |
checkboxInput | A single check box |
dateInput | A calendar to aid date selection |
dateRangeInput | A pair of calendars for selecting a date range |
fileInput | A file upload control wizard |
helpText | A Help text that can be added to an input form |
numericInput | A field to enter numbers |
radioButtons | A set of radio buttons |
selectInput | A box with choices to select from |
sliderInput | A slider bar |
submitButton | A submit button |
textInput | A field to enter text |
Sidebar Layout sidebarLayout()
Grid Layout
Tabsets
Navlists or Navbar
For more information visit shiny documentation about layout
http://shiny.rstudio.com/articles/layout-guide.html
Example of a navigation bar
example of tabsets
bootstrap.css
shinyUI(fluidPage(theme = "bootstrap.css",
titlePanel("My Application")
# application UI
))
We will create an application to explore US crime data from 1969 to 2005
The application that we made is already hosted on Github. To launch this application, run the following codes from R
command prompt
library(shiny)
shiny::runGitHub('usa-crime', 'mamajumder')
Before running this make sure packages ggplot2
, dplyr
, scales
and ggmap
are already installed.
The source codes and the data for this application can be downloaded from
https://github.com/mamajumder/usa-crime
The application is deployed on RStudio. To view the application we created visit
https://mahbub.shinyapps.io/usa-crime/
ggvis
%>%
for multiple layersinstall.packages("ggvis")
library(ggvis)
input_slider()
: slider inputinput_checkbox()
: a check-boxinput_checkboxgroup()
: a group of check boxesinput_numeric()
: a spin boxinput_radiobuttons()
: pick one from a set optionsinput_select()
: create a drop-down text boxinput_text()
: arbitrary text inputmtcars %>% ggvis(x= ~wt) %>%
layer_densities(
stroke := input_radiobuttons(c("Purple","Orange","steelblue"),
label="Line color"),
fill := input_select(c("Purple","Orange","steelblue"),
label="Fill color")
)
library(ggvis)
mtcars %>% ggvis(x = ~wt) %>%
layer_densities(
adjust = input_slider(.1, 2, value = 1, step = .1, label = "Bandwidth adjustment"),
kernel = input_select(
c("Gaussian" = "gaussian",
"Epanechnikov" = "epanechnikov",
"Rectangular" = "rectangular",
"Triangular" = "triangular",
"Biweight" = "biweight",
"Cosine" = "cosine",
"Optcosine" = "optcosine"),
label = "Kernel")
)
ggvis
can do limited things. But what it does, it requires less effort.
shiny
To view the shiny application we created to explore US crime data visit
https://mahbub.shinyapps.io/usa-crime/
Shiny web site
http://shiny.rstudio.com/
Shiny cheat sheet, a very useful reference guide. Check for updates.
http://shiny.rstudio.com/articles/cheatsheet.html
Overview of R
package ggvis
http://ggvis.rstudio.com/