top of page
Search

This is a helpful package in R programming


Build a stunning web app using Shiny package in R programming. Building web app in R is really simple if you understand its architecture and basic app template.

Prerequisite: You need little bit of R programming, html, css and js knowledge you can always check my other blogs to get little knowledge in R programming

Yeah! I got this while i was exploring R programming, and this got my eye🤩, you know, you can make web applications using R programming. Okay so i have been working in R programming from past 2 months roughly, and i was really surprised with how much you can do in this simple but great programming language. There is a package called as Shiny in R. Which is used to make web application using R.

It is always recommended to understand everything in R programming its functions and everything before getting started with shiny app but as i am web-dev enthusiast i was excited to show you guys how a data science focus programming language can build a great web app using a simple package called Shiny.

In this article, we are going to explore Shiny package and see how it really works in creating web app. Lets see from how to install the package to how to use this package in creating web app.

 

Table of content:

1. What is Shiny Package?
2. Why to use Shiny package?
3. Exploring Shiny Package.
4. Structure of shiny app
5. Working on creating web app using Shiny
 

1. What is Shiny Package?


Shiny is a package in R programming that will help you to build a interactive we applications straight from R. You can host your web app, build a dashboard you can also use html, css and javascript in R using Shiny package, isn't it a great combination🤩🤩. computational power of R programming and modern web.

So basically like any other package you import at start of the R programming shiny package is also import in a same way. In shiny apps you can do any thing using inbuilt functions. That's it there is not much to understand what shiny is?. As its concept is so simple the usage of shiny package is also not so complex. You just need a basic template of the shiny app structure and little bit knowledge for functions in shiny and you are good to go.



2. Why to use Shiny package?


So, you know we can select any line and run it in R studio, but what if we can use it to show it in web application and the user can only see what the web application shows like this.

As you can see here, Shiny apps can give you the input and output in input the user selects the the data column and view its visualization on the right side. but shiny app is not limited to this you can checkout this official site of R where there are examples of amazing user showcase created using R.


Shiny is good at providing insights presenting graphical result in minimal interaction. letting user upload there own data, generating report etc.

  • If you see non R user which don't know much about R can also upload there data and see it data visualization and standard data manipulation which makes it easier for data scientist to work on other challenging task.

  • Shiny apps has numerous possibilities it can be used for creating a prototype by the data workers as many business work on different technologies it becomes easier to show how the application will look and is it worth to invest in this projects or not. and there are more to it you can deploy shiny applications on the go using R studio connect and many hosting providers without the help of any developer teams.



3. Exploring Shiny Package


So lets start with installing shiny in our R studio and lets see what shiny app can do

library(shiny) #this will import the library in the project if not installed you can always install it using following
install.packages("shiny")

Now, you are ready to start creating your first shiny application which will may stat your journey from R programmer to shiny developer

Lets see what shiny package provide us

shiny package contains al sort of functions for creating a UI

  • UI layouts (absolutePanel(), navbarPage(), ...)

  • UI inputs (actionButton(), dateInput(), radioButton(), ...)

  • UI outputs (htmlOutput(), plotOutput(), ....)

  • Interface builder functions (for custom html, css and js)

  • many more



4. Structure of shiny app


Shiny apps are created in a single script called as app.R. This script lives in any project directory eg. project1/app.R

Shiny app consist of :

  • UI object

  • server function

  • a call to shiny app function

lets see how the basic structure of each one of this component looks:

1. UI object
library(shiny)
#Define UI for our app
ui<-fluidPage(
    #Our app title goes here
    titlePanel("Hello guys!"),
    
    #we are using sidebarlayout here. there is split layout also
    #In sidebar layout there will be input and output definitions
    sidebarLayout(
    
    #for input
    sidebarPanel(
    "Sidebar Panel"
    #you take any ui inputs here
    
    ),
    
    #for output
    mainPanel(
    "Main Panel"
    #you can take any ui outputs here
    )
    )
) 

Output:

2. Server function
server<- function(input, output)
{
#here your code
#where it can be any plot of the data column or anything
#I have shown a example below if you want to understand more
}
3. A call to shiny app
library()shiny

ui<-...#ui object which we demonstrated above

server<-...#server function which we demonstrated above

shinyApp(ui=ui, server=server)

Every shiny app has the same structure containing a ui object a server function and a call to shiny app with a name app.R in a unique directory every new app you create should be in different directory and will be run using following snippet.

runApp("Directory name in which app.R exist") #run the app using the directory name

So, now we know the basic structure of shiny app lets make our first small working web app using shiny package in r programming using the above structure as template and add just few lines of code in it.

Structure of shiny app:

library(shiny)
ui <- fluidPage
(
    titlePanel("Title panel"),

    sidebarLayout
    (
    sidebarPanel("Sidebar Panel"),
    mainPanel("Main Panel")
    )
)

server <- function(input, output) {

   
}

# Run the application 
shinyApp(ui = ui, server = server)


5. Working on creating web app using Shiny


Step 1: Create a directory with any name i am giving my directory name as "project1" and create a R file with app.R name.


Step 2: Now lets add the above template from section 4 "structure of shiny app" in our app.R file


Step 3: lets add a sliderInput in the sidebarPanel with min value as 1 and max value as 100 and slider at 30 by default with label as number <decide>

sliderInput(

    inputId="bins", #this id will be used in server function
    label="number of bins",
    min=1,
    max=100,
    value=30,
    
),

Step 4: lets add the output in the mainPanel we are going to show histogram as our output.

plotOutput(outputId="distPlot") #note this id will be used in server function

Lets see how are UI looks now, although the data and output is not yet added

Output:

As you can see only sliderInput is showing now we have to add our server functio of showing the hist plot for the input.


Step 5: lets add our most important part of this web app i.e. server function

output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        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')
    })

Output:

Here is the small web application created using R shiny package.



Summary:

In this article we have learn what is shiny in R and why we should use it, we have learned how to implement shiny package functions to create small web application and also seen how to create a input and output graph in web app.

Note: This is a basic tutorial of what you can do using Shiny package in R programming more advance tutorial will be there in future.
 

Thanks for reading my article. I hope you like it and learned something new today. Please give your comment down below. If you have any doubt or any problem related to this or any other article. I'll be posting more articles related to R.

You can always checkout my other article here.


Tags:

5 comments

Recent Posts

See All
Favorite Links
Recent posts
bottom of page