A Data Science Central Community
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.
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.
library(shiny) #load shiny package
ds = read.csv("inputdata.csv") #load data
x = names(ds) # Define UI for application that plots distributions
# Application title
headerPanel("Demo Shiny Application"),
#sidebar panel to the left side of the page for input controls, slider & dropdown
sliderInput('Year', 'Year', min=2010, max=2012,
value=min(2010,2012), step=1, round=0),
selectInput('select', 'Select Criteria', c(x,x,x,x,x,x))
#main panel for displaying outputs, Histograms, 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.
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.
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.
As mentioned in the beginning of the post, in order to deploy locally, we make
use of GIST. The steps are quite simple:
Please go through more posts @ www.dataperspective.info