Step By Step: How to Create an ‘R’ Shiny App
In a previous Step By Step by my fellow analyst James Midkiff, readers were introduced to the statistical software ‘R’, and how ESI increasingly uses this resource for analyses. In this week’s post I build on what James introduced and show readers how to implement a Shiny App tool with ‘R’. As Present Value readers know, ‘R’ is a programming oriented statistical analysis software. It is very powerful and allows users to create all sorts of customized analysis and tailored datasets to suit specific needs. However, the trade off is that it can require a high level of programming skills.
A Shiny App consists of two major components: a user interface that defines the input and display output, and a server which processes data and performs analyses. For this post, I illustrate how this works with a simple example – a Property Transaction Search. The Shiny App has as the back end a list of about 300,000 property transactions in Philadelphia since 2000, and is able to search and display the transactions within a user-defined radius from an address.
The first step in creating a Shiny App tool is to set the work directory, and load your packages and datasets. If other data processing is desired, it may need to come after your initial loading, but before creating the user interface and server. The “shiny” library is required for running a Shiny App, while other libraries depend on the needs of the analyses. Here, it also loaded the transaction dataset. The dataset is processed before creating the App by removing unwanted columns, altering variable names, filtering invalid transactions, etc. Then, it is saved as an .Rda file, an ‘R’ data storage file that is easy to load and reserve ‘R’ features.
The next two figures show the user interface and the code for it. For illustration purposes, I added a title, a map, two input values, a button to initiate the analysis, and an output table which is invisible before launching the analysis.
The Shiny package in ‘R’ has many functions to set up inputs and buttons. They are very straightforward and user-friendly. In the sample code shown below, for example, if I want to set up a text input, I simply use the function “textInput”. Then, I give it a name “address”, which is how I call it in the later server part, a label to show in the user interface, and a starting value, which is a blank here. You can just let Shiny arrange them if the layout is simple.
The Server is where all analyses happen. Within the server, we first defined a map, and centered it in Philadephia. The latter part is for the search. The function “eventReactive” is a function that allows a trigger (the “Find” button) to initiate the analysis. After clicking the “Find” button, the App will use google to find the location of the address entered, calculate the distance from this point to all transactions, and filter them based on the radius entered earlier. The filtered list is then returned as the output named “results”, and sent to the user interface. After the user interface UI and the server are ready, just clicking on the “Run App” button will launch the App.
The example below tried to find transactions within a quarter mile of ESI’s office using the Shiny App tool. The output table shows the location, sales price, and a few characters of the properties.
In real world projects, use of the Shiny App can be far more complicated. It may contain several inputs and outputs, along with several tabs to perform different tasks. But if you have all your analyses ready, building a Shiny App tool to house it is quite easy. In the user interface part, you simply set all variable to be customized as inputs and then define the outputs. In the server, put the code into an appropriate “event Reactive” function and make sure each of the names of the values matches the inputs and outputs. After the Shiny App tool is set up, it can be deployed online and shared with other people through a shinyapp.io account.
You can access the App illustrated in this post here. This version will allow you to search 2016 transactions. Feel free to try it out!
As you may notice, we have a map in the user interface, but never used it. In the next blog of this “how to” series, we will talk about the application of a leaflet map in the Shiny App, a spatial tool that allows users to pick a location on the map and visualize outputs on the map.
Peiyong Yu is a Research Analyst with Econsult Solutions. He has extensive experience with spatial and statistical analysis, transportation planning and economic analysis, and proficient in ArcGIS, R-Studio, and Excel.
Tags: How To, Philadelphia, Present Value, R, statistical computing, Step by Step
Categorised in: ESI Blog