Blog posts on Data Science, Machine Learning, Data Mining, Artificial Intelligence, Spark Machine Learning

Thursday, March 20, 2014

Build Web applications using Shiny R

Ever since I’ve started working on R , I always wondered how I can present
the results of my statistical models as web applications. After doing some
research over the internet I’ve come across – ShinyR – a new package
from RStudio which can be used to develop interactive web applications with R.
Before going into how to build web apps using R, let me give you some overview
about ShinyR.

Features:
  • No JavaScript/HTML knowledge required. 
  • Can build interactive web applications which can load the data dynamically.
  • Pre-Built output widgets for displaying plots, tables interactively are available.
  • Works in any R environment.
  • Shiny Apps can be hosted locally/on self-hosted Shiny Server/Rstudio-hosted                              Shiny Server – in today’s post we will see hosting locally.

 Building a shiny App


Shiny application have two components: a user-interface definition and server script,
 and any additional data, scripts, or other resources required to support the
application.
UI script (ui.R): In this script we define the UI layout of the webpage. We can make
 use of inbuilt UI controls/widgets for displaying output and for creating UI layout..
Server Script (server.R): In this script, the actual business logic/ code for statistical
models, plotting the output is defined. It accepts inputs  from UI layer and compute
outputs send back the response to the UI layer. The basic task of a Shiny server
script is to define the relationship between inputs and outputs.

Understanding UI.R script:

library(shiny) #load shiny package
ds = read.csv("inputdata.csv") #load data
x = names(ds) # Define UI for application that plots distributions
shinyUI(pageWithSidebar(
# Application title
headerPanel("Demo Shiny Application"),
#sidebar panel to the left side of the page for input controls, slider & dropdown
sidebarPanel(
sliderInput('Year', 'Year', min=2010, max=2012,
value=min(2010,2012), step=1, round=0),
selectInput('select', 'Select Criteria', c(x[3],x[9],x[10],x[11],x[12],x[26]))
),
#main panel for displaying outputs, Histograms, summary
mainPanel(
plotOutput('plot'),
verbatimTextOutput("summary")
 ) ))




Notice in particular, in the above code sidebarPanel and mainPanel functions
 are called with two arguments - corresponding to the two inputs and
 two outputs displayed.

In the above image we can see the layout which contains a slider with year range,
where based on the year selection the dataset will be taken into consideration.
Dropdownlist containing varaibles, placeholder to display the graphs, table to
display the summaries of the models.

Understanding Server.R


In the above script, we can observe that all the logic for the exploratory analysis is
written in server.R script. It contains a main function called ShinyServer() 
wherein we can write reactive functions,reactive plots, outputs. The outputs
 are send back to the UI layer.
In the above code, I have written logic which contains data fetching, 
modification, setting the dataset based on the year selected in the 
slider (reactive function), displaying histogram based on the selection 
on slider and dropdownlist,writing summary to a table.

Deploying the application:

Once we are done with the application, next step is to deploy the application. 
Using Shiny, we have two different options to move the application to Production.
  • ** Create a local- self-hosted Shiny Server on Linux platform, here.
  • ** Host on the R-Studio's Shiny server, explained here
Deploying locally:

As mentioned in the beginning of the post, in order to deploy locally, we make
use of GIST. The steps are quite simple:
  • ** Upload the Server.R & UI.R files to the Gist.Github.com website.
  • ** Uploading the files gives you an ID - in my case - I got "8942533"
  • ** After finishing upload, open R and run the below code:
               library('shiny')
               shiny::runGist('8942533')
  • The model will be shown as web application will be displayed
 the browser with inputs for selection & outputs to display.