24 lines of code to build a Shiny web application from scratch

Web applications have become very popular in the data industry to give access to self-service analytics tools. In a nutshell, these applications enable users to run without any technical knowledge.

A good-looking web application. Source: unsplash.com

Extremely flexible, these standalone tools may contain basic features (e.g. data visualization) as well as advanced functionalities (e.g. label training data for machine learning).

Having a background in web development is not required for data experts. High-level libraries were developed for the most commonly used programming languages in the industry: Shiny (R) and Bokeh (Python).

Below in this article, you will find a total of 24 explained lines of code to build a Shiny application from scratch: it can be used as a starting point for your projects.

Shiny was first released in 2012 and requires no specific knowledge in web development, but I assume you have some working knowledge of R. If not, check out my previous article which has a quick reminder on how to install R packages, and you are good to go!

You may find the script files used in this article on this GitHub repository.

Let’s dive in the technical details.

The application code is separated into two scripts: environment.R and app.R, the former having just a few lines to load packages and declare variables used later.

The environment required to run the app.

The shiny package contains all the required functions to build a web application, simply put, it is the package that makes everything possible. On top of that, shinydashboard provides high level functions to create an attractive user interface.

The main script app.R contains the following operations, to create the web application:

  1. Define the user interface, using shinydashboard functions
  2. Define the server function, which contains the logic behind the user interface (e.g. code triggered after a button is hit)
  3. Build and run the app

In this example, the server functionis empty as this app as no functionalities yet. It just sets up everything required to create an app from scratch. A good-looking app that does nothing 🙃.

The code to build and run the app.

Before having a look at the result, let’s describe with more details each stage of the script:

1. The user interface

The user interface is made of a dashboard , containing a (at the top), a (on the left) and a (on the right). The defined when creating the header is displayed in the upper left-hand corner of the web page. Inside the was created a , which is empty for the moment but still has to have a unique string identifier: ID_UI_SIDEBARFollowing the same logic, inside the was created an which may basically contain any UI element. This also has its unique string identifier: ID_UI_BODY

A few words about UI elements identifiers: they are essential for the server to be able to refer to each UI element, and that’s why they have to be unique. For example, when implementing a given action triggered when a button is hit, the button’s identifier is used as an input for the server to detect the hit and perform the relevant action.

2. The server

As mentioned, the server is an empty function in this example, so there is not much to say here. This is where data experts writes their data processing code, with slight modifications so that it is to inputs from the user interface. This part of the application will be discussed in a different article, where the application actually does something 🙃.

3. Running the app

The web application is created and ran using shinyApp and runApp functions, or clicking button in RStudio.

The Shiny web application runs in the Viewer pan of RStudio, by default in the lower right-hand corner

Okay… I maybe should have added at least one UI element, but still, this web application already looks nice, doesn’t it ?

Conclusion

24 lines of code are enough to start a Shiny web application project, thanks to fantastic packages shiny and shinydashboard.

Still, there is a lot more to cover but this is a first step into R Shiny development, which is a real opportunity for businesses to create valuable analytics tools.

Thanks for reading!

🇫🇷 Data scientist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store