The R Shiny framework is a package from RStudio that makes it incredibly easy to build interactive web applications with R. R Shiny is awesome in the sense that it allows you to create highly effective data reports and visualizations where the user can explore a data set. Along with Shiny elements, you can use HTML elements to stylize your content in your application.
Shiny App Structure
Shiny applications are divided into two parts: the User Interface (UI) and the Server. The UI is responsible for the app presentation, while the server is responsible for the app logic.
The UI controls what is being displayed on the application page and how the components are laid out. This may include text and other markdown elements, graphics, widgets that take in user input, or plots.
The Server controls the data that will be displayed through the UI. The server will be where you load in and wrangle data, then define your outputs (i.e. plots) using input from the UI.
Building a Shiny app
There are four essential elements that are specified in the UI and server scripts.
1.Add inputs (UI)
2.Add outputs (UI/server)
3.Update layouts (UI)
4.Update outputs (server)
There are tons of different inputs that can be used in your app. These are what the user interacts with to input directions to the app. Some examples of common inputs are below:
•textInput(): used in Hello app; allows user to type text
•sliderInput(): creates a numeric slider bar
•selectInput(): creates a drop-down list
•dateRangeInput(): allows user to select two dates from a calender
There are two types of outputs: render functions and output functions. Render functions are used in the server to create images, text, plots, etc.
To get started, make sure you have the shiny package installed on your system. You can download shiny from CRAN using the command install.packages(“shiny”). The RStudio IDE contains a basic shiny application template you can use to create your first shiny application. To access this template, click on the new document icon in the top left corner of the IDE and select “Shiny Web App…”. This will create the R script file app.R which contains the basic components of a shiny application.
How is Shiny different from traditional applications?
Lets us take an example of a weather application, whenever the user refreshes/loads the page or change any input, it should update the whole page or part of the page using JS. This adds load to the server-side for processing. Shiny allows the user to isolate or render(or reload) elements in the app which reduces server load. Scrolling through pages was easy in traditional web applications but was difficult with Shiny apps. The structure of the code plays the main role in understanding and debugging the code. This feature is crucial for shiny apps with respect to other applications.
The user interface can be broadly divided into three categories:
•Title Panel: The content in the title panel is displayed as metadata, as in top left corner of above image which generally provides name of the application and some other relevant information.
•Sidebar Layout: Sidebar layout takes input from the user in various forms like text input, checkbox input, radio button input, drop down input, etc. It is represented in dark background in left section of the above image.
•Main Panel: It is part of screen where the output(s) generated as a result of performing a set of operations on input(s) at the server.R is / are displayed
Advantages and Disadvantages of Shiny
There are plenty of other data visualization tools out there. So, to help you compare what differentiates Shiny and what you can and cannot do with Shiny, let’s look at the advantages and disadvantages of using shiny.
•Efficient Response Time: The response time of shiny app is very small, making it possible to deliver real time output(s) for the given input.
•Complete Automation of the app: A shiny app can be automated to perform a set of operations to produce the desire output based on input.
•Advance Analytics: Shiny app is very powerful and can be used to visualize even the most complex of data like 3D plots, maps, etc.
•Cost effective: The paid versions of shinyapps.io and shiny servers provide a cost effective scalable solution for deployment of shiny apps online.
•Open Source: Building and getting a shiny app online is free of cost, if you wish to deploy your app on the free version of shinyapps.io
•Requires timely updates: As the functions used in the app gets outdated sometimes with newer package versions, it becomes necessary to update your shiny app time to time.
•No selective access and permissions: There’s no provision for blocking someone’s access to your app or proving selective access. Once the app is live on the web, it is freely accessible by everyone
•Restriction on traffic in free version: In free version of shinyapps.io, you only get 25 active hours of your app per month per account.
The Shiny package is free and open source, and is designed primarily to run Shiny applications locally. To share Shiny applications with others, you can send them your application source as a GitHub gist, R package, or zip file (see details). We’re also working on a Shiny server that is designed to provide enterprise-grade application hosting, which we’ll offer as a subscription-based hosting service and/or commercial software package.