Shiny development and deployment

Basics of shiny app development and deployment

Featured image

Do you analyze and generate plots and stats in R statistical programming language??

Ever wondered how all the analysis you have done, the plots you have generated and the results of the numbers you have crunched can be delivered to the viewers in a more presentable and an interactive way??

This is where Shiny comes to the rescue… Developed by Winston Chang, a software engineer at RStudio, and is the developer behind many other well known R packages including ggplot2, and devtools packages.

Read more about his work on the RStudio resources section and his contributions to the R universe on the R collaborators documentation.

This blog tries to provide with a basic overview, of stepping into the arena of shiny app development rather than an in detail technical guidance for developing a shiny app. Here, I will outline the key components which are the basis for any shiny application and the mastering of which will provide a strong foundation towards building more sophisticated applications later on.

What is Shiny?

Shiny is an open source package given to all of us R fanatics by the generous Hadley Wickham and his RStudio team. Shiny package provides a web application framework to create interactive web applications (visualization) called Shiny apps.

Shiny provides automatic, reactive binding between inputs and outputs (which we wil look into in the latter part of this blog) and also awesome pre-built widgets which make it possible to build attractive and effective applications with minimal effort especially for those of us who come from a non-software engineering background.

Core ingredients of Shiny

Any Shiny application is brough to life via two key components;

  1. UI (User Interface)
  2. Server
  3. A call for the shinyApp function

UI and Server

Let’s go into these components in compendiary detail.

1. UI

This is a file which will be saved as ui.R when we are bundling the application.

As the name itself suggests, it creates the user interface in a shiny application. It adds interactivity to the shiny app by receiving input from the user and accordingly displaying the generated output on the screen in a dynamic manner.

If you are interested in finding out the mechanics of how the reactivity of the Shiny application works, a good reference is the Reactivity - An Overview documentation by the RStudio team itself.

Sample UI Code

2. Server

This is a file which will be saved as server.R when we are bundling the application.

This is where the magic happens. The user interface configured in the ui.R file is basically a wrapping around the process defined within the server.R file.

In other terms, this is the file where the logic behind the application is coded and the pipelines are stored. Pipelines here means the series of steps to convert the input given by user into the desired output to be displayed via the user interface we have defined earlier.

Sample Server Code

3. The call for the shinyApp

The shinyApp function creates Shiny app objects from an explicit UI/server pair.

Enough about the overview. Let’s try to dive into the development of a shiny app now…

Initiating a Shiny setup

The initiation simply means the generation of the ui and server templates of the application through R/RStudio.

For this follow through these simple steps.

1. Create a new project in RStudio

Go to “File” tab and click “New Project”.

Setting up Shiny

2. Select the type as Shiny web application.

When prompted to this screen, select the type as Shiny web application.

Setting up Shiny

3. This will generate two R files as above with names server.R and ui.R

Once these steps are done, the next step is to code each of these files seperately with the user interface of desire within the ui.R and the fundamental logic and pipelines coded within the server.R.

Given below are the basic steps to create a simple ui.R file and a server.R file to generate a simple shiny app as a starting point.

An important fact to notice is that before the release of verion 0.10.2 of R, Shiny did not support single-file apps and the ui object and server function needed to be contained in separate scripts called ui.R and server.R as we are discussing here. But we can also include both the ui and server components in the same file which makes it convenient to share the application among the developers for testing and developers.

Please note that the components and the code given here are basic and depending on the complexity of the application intended to be built, more dynamic components and ionteractivity can be added to the ui and server file accordingly. As for this article, a basic ui and server logic are explained.

Coding the User Interface file

If you still haven’t installed the Shiny package, open an R session, connect to the internet, and run:

Installing Shiny

In-case this command does not work, try the following command which it will access the github repo for the shiny package and install it for you. For this you need to have devtools package installed.

Installing Shiny

After you have gotten the essential packages installed and loaded (using the library(“shiny”) command), the next is to put the UI elements in place to start building our app.

When creating a shiny application, the best way to ensure that the application interface runs without any lags and hassle even on different devices containing different screen aspect ratios is to use fluid page. This ensures that the page is laid out dynamically.

If you want to have an inspiration as to how general shiny apps look like, the Shiny package has eleven built-in examples, which you can run within the console since they are all self contained applications.

For now let us focus on a simple Shiny app written in the official documentation on Shiny for the ease of understanding the key concepts.

Here is the ui object for the Hello Shiny example:

UI Code of Hello Shiny Example

The user interface can be broadly divided into three categories:

Coding the server file

Server Code of Hello Shiny Example

When looking at the server function, the Hello Shiny server function is basic and comprehensible. As per the logic of the server function, the script executes some calculations and the resulting histogram plot is generated along with the user defined number of bins, which was taken as input from the user interface.

Calling the shiny app using the shinyApp function.

In your server.R file we will need to start with loading the Shiny package and end with a call to shinyApp to execute the written application:

Code for the shinyApp part

You can run a Shiny app by giving the name of its directory to the function runApp. For example if your Shiny app is in a directory called my_app, run it with the following code:

Code for running the created application

Or else, you can just click on the Run App button to run the script and execute the application.

Code for running the created application

How the final application looks like

The generated Shiny app looks as follows:

Displaying the generated app

Recap of the pointers we have gone through

To create your own Shiny app:

Future steps

Once the application of your desire is created as above (Ofcourse with more complexity and elagence than this sample app), the next step would be to the world to view it and use it, be it within the corporation or general public.

This is where the deployment phase of the application comes in and we will be discussing more on how and what entails this endeavor of app deployment on another post.

Till then, Happy Shiny Coding folks…!!!