Dashboard Using R Shiny Application
By: Hassaan Ahmed
Tags: Dashboard, Rstudio, Shiny, Kaggle, Data Visualization

I decided to look at the change in patterns related to terrorism from 1970 to 2017 using the Kaggle dataset. I know the data is sad but interesting to work with.

About the DataSet:

The Global Terrorism dataset is an open source database which provides data for terrorist attacks from 1970 to 2017.

Geography: Worldwide

Time period: 1970–2017, except 1993

Unit of analysis: Attack

Variables: >100 variables on location, tactics, perpetrators, targets, and outcomes

Sources: Unclassified media articles (Note: Please interpret changes over time with caution. Global patterns are driven by diverse trends in particular regions, and data collection is influenced by fluctuations in access to media coverage over both time and place.)

Overview of R shiny Application:

Before we deep dive and see how a dashboard is created we should understand a little bit about the shiny application. Shiny is an open source R package that provides an elegant and powerful web framework for building web applications using R.

When you are in R studio environment you have to create a new file and select R shiny web application:

Create a new shiny application file

To begin we should install some packages if they are not installed:

Shiny Shinydashboard leaflet

Shiny Dashboard:

It is really important to know the structure and principles on which the dashboard is built.

This is called the User Interface (UI) part of the dashboard

Header Sidebar Body

Header:

Header as the name suggests describes or provide title to the dashboard. You have to use dashboardHeader function to create the header. the below code defined what should be written in the heading of the dashboard.

Code for header

Sidebar:

Sidebar of the dashboard appears on the left side of the dashboard. It is like a menu bar where you can put information for the user to select from. In my dashboard I created two menu items. As you can see that one is the tab with the name “Dashboard” and the other is a link to the kaggle dataset. When you click it will open the link source of the data.

Codes for Sidebar

Below is the visualization of the sidebar menu:

Body:

This is the most important part of the dashboard as it contains all the charts, graphs and maps which you want to visualize. It consist of rows which are determines in which row your data is visualized. These rows are called fluidRow. Inside fluid row you place boxes and determine the kind of options you want to put in that box using selectInput function. In my dashboard I have total of three rows. The code and visuals for row number two are shown below.

Codes for the fluidRow 2

Boxes in fluidRow 2

Combining all the rows makes the body for the dashboard using dashboardBody function:

Combining all the rows in a body

Creating server functions:

The server function tells the shiny app to build the object. Server function create output/s containing all the codes needed to update the objects the the app. Each input to output must contain a render functions. Render functions tell what kind of output is required. Some of the render functions are below:

renderDataTable → DataTable renderImage → images (saved as a link to a source file) renderPlot → plots renderPrint → any printed output renderTable → data frame, matrix, other table like structures renderText → character strings renderUI → a Shiny tag object or HTML

Code example of renderPlot

Once you are done with writing codes within server functions the last step is to run the shinyApp.

Code to launch the app

The final output of my dashboard is below:

Once This dashboard was created I deployed it online. You can open it on your systems as well as on your smartphones.

You can find the codes and the data file for this dashboard on my Github.

Sources used: