Introduction
Shiny
is a new package from RStudio that can be used to build interactive web
application straight from R. It may be sound so much surprisingly for R users
who have no experience in web development. Even you can build useful web
application with only few lines of code. You don’t need to know any HTML/CSS/JavaScript.
Shiny package provides fast bidirectional communication between the web browser
and R using the websockets package. Even
you can also use HTML, CSS and JavaScript in your Shiny code for more
flexibility. In fact you can do quite a lot with shiny: like display R Objects,
tables, Plots or anything else you do in R. To get more information about
shiny, you can visit Shiny Tutorial
website.
This
tutorial is a hands-on activity for learning how to build shiny apps. And how
to include yoy R code in shiny app.
Installation
Install.packages(“shiny”)
To ensure
you successfully installed Shiny, try running one of the demo apps.
library(shiny)
runExample(‘01_hello’)
Structure
of a Shiny App
Shiny app have two components
1. A user-interface script
2. A server script
The user-interface (ui)
script controls the layout and appearance of your app. It is a source file
named ui.R. While server.R script contains the
instructions that your computer needs to build your app
To start shiny app, go to File-> New File Name -> Shiny
Web app. It will give you two options.
First options is Single File, in this options only single file
is created where you write your shiny code. In second options you will get two
file ui.R and server.R.
If you selecting multiple
File options. You will see by default example is already programmed in both
scripts. Now we will see how to use Rscript code in shiny app.
Create Box Plot using Shiny
App
ui.R
library(shiny)
head(iris)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Iris"),
# Sidebar with a
slider input for the number of bins
sidebarLayout(
sidebarPanel(
h1("Boxplot of Iris Species")
),
# Show a plot of the
generated distribution
mainPanel(
plotOutput("myplot")
)
)
))
|
server.R
library(shiny)
library(ggplot2)
shinyServer(function(input,
output) {
output$myplot <- renderPlot({
p <- ggplot(data =iris, aes(x=Species,y=Sepal.Length,
colors=factor(Species)))
graph <- p+geom_boxplot(fill=heat.colors(3))
print(graph)
})
})
|
After
saving the file, RStudio should recognize the shiny app, you should see the
usual Run button at the top change to Run App.
If
you don’t see the Run button, it means you either have very old version of
RStudio, don’t have shiny installed. Click on Run button, and now your app
should run.
Output:
You
should see that console has some text printed in the form of listening on http://127.0.0.1: 3504. You will also notice that you cannot run
any commands in the console. This is because R is busy-your R sessions is
currently powering Shiny app and listening for user interface.
To
stop the application, click on stop button and also you can press Escape
button.
This
is a simple way to encode R code in shiny App. Similarly you can do lots of things which you do in R. you can also plot Data Frame and so on. If you want to learn
more about this package go to Shiny Tutorial website.
No comments:
Post a Comment