Interactive applications with Shiny {#Shiny}

Shiny is an R framework in which you can set up browser-based interactive applications and use them to interact with the data. This approach results in a better understanding of models you may build in R. Full documentation and details are available at http://shiny.rstudio.com/

Preparing an application in Shiny requires creating the back end processing code, which has to be stored in a file named server.R and a front end graphical user interface (GUI), placed in a file named ui.R. Both these file names are mandated, as the shiny package will look for these files. One may also create a file called app.R in which both a server function and a ui function are embedded. To illustrate, we will create an interactive application to price options using the well-known Black-Scholes-Merton (1973) model.

The Black-Scholes-Merton (1973) model

The price of a call option in this model is given by the following formula $$ C = S e^{-qT} \cdot N(d_1) - K e^{-rT} \cdot N(d_2) $$ where $$ d_1 = \frac{\ln(S/K)+(r-q+v^2/2)T}{v \sqrt{T}} $$ and $d_2 = d_1 - v \sqrt{T}$. Here $S$ is the stock price, $K$ is the strike price, $T$ is option maturity, $v$ is the annualized volatility of the stock, and $r$ is the continuous risk free rate of interest for maturity $T$. Finally, $q$ is the annual dividend rate, assuming it is paid continuously.

Likewise, the formula for a put option is $$ P = K e^{-rT} \cdot N(-d_2) - S e^{-qT} \cdot N(-d_1) $$ and $d_1$ and $d_2$ are the same as for the call option.

The application program

Here is the code and it is stored in a file called app.R.

This may not run in a Jupyter notebook and you will then have to use RStudio. Cut and paste this code into an R script file app.R and then run it.

Running the App

To run the app, open the file app.R in RStudio and then execute RunApp from the menu. This app will generate the following screen.

  1. Note the sidebar panel, that allows numeric input for the stock price and the strike price.
  2. Note also the slider input for the other variables of the model.
  3. Changing the inputs results in automatic interactive updates in the output panel, both to call and put prices, as well as the plots.
  4. Look at the panel with the plots, it has two tabs, and one can click to switch between the plot for calls and the one for puts.

Server section of the App

The server section has the following features (examine the code above).

  1. The packages used may be invoked at the top of the file, as they may be used by both the server and ui functions.
  2. Each external output is created by a separate function. The text output is carried out by a shiny function called renderText and the plots are generated by a function called renderPlot.
  3. One may also create subsidiary functions that do not generate external output, but are called by other functions inside the program. For example, the function BS in the code above implements the option pricing formula but does not return anything to the UI.

UI section of the App

The ui section has the following features (examine the code above).

  1. There are three panels: title, sidebar, main. This allows for a nice layout of inputs and outputs. In the example here, we use the sidebar panel to input values to the app, and the main panel to present outputs.
  2. All inputs are taken in to an object called input which is then accessed by the server section of the program. Different formats for the inputs are allowed and here we show numeric and slider inputs as examples.
  3. The output can be tabbed as is done for the plots.

Using the reactive mode in the app

The ui portion of the program takes input values and makes them available to the server section. We see that each function in the server section has to collect all the inputs for itself, and as a result the initialization of variables in this section occurs inside each function in a repetitive manner. In order to avoid this, and thereby shorten and speed up the code, we may use the inputs in reactive mode. What this means is that inputs are live and available globally to all functions in the server segment of the program.

Here is the ui.R file from the reactive version. We see that is much the same as before.

However, the server.R file is quite different, and it needed the Black-Scholes pricing funtion BS to be refactored to take reactive input, and we were then able to shorten the code considerably, see here.

You can copy this code and create two files in your directory and then run the app to see it execute in exactly the same way as before when reactive inputs were not used.

Market Liquidity in Real Time using Shiny

In this segment we combine web scraping with shiny to create a real time liquidity model. The app is based on the paper by George Chacko, Sanjiv Das, and Rong Fan titled "An Index-Based Measure of Liquidity", published in the Journal of Banking and Finance, 2016, v68, 162-178. It is available at: http://srdas.github.io/Papers/etfliq.pdf

The main idea of the paper's algorithm is as follows. Since the ETF is usually more liquid than the underlying bonds it represents, any difference in the price of the ETF and the NAV (net asset value) of the underlying bonds must be on account of liquidity, because market risk is otherwise the same for the ETF and its underlying. The paper uses an option pricing based derivation of the illiquidity of the market sector represented by the ETF. This illiquidity is represented in a basis points spread given by the following equation:

$$ BILLIQ = -10000 \ln \left(\frac{NAV}{NAV + |ETF-NAV|}\right) $$

Program files

For this application here are the ui.R and server.R files. You can cut and paste them into separate files in RStudio, and then run the app.

When the app is launched the following interactive screen comes up so one may enter the ETF market for which the liquidity is being computed.

As one can see, several statistics are provided, after being scraped from the web. The code in server.R shows how the information is sourced from the web. The code below runs the app from Jupyter.

Using Shiny with Data Table

In this section we will redisplay the data set for finance firms that we looked at earlier in the previous chapter using shiny. What we will do is add to the shiny app a feature that lets you select which columns of the data set to display. The resulting Shiny App should look as follows:

We create the data that we need to apply to the shiny app. Here are the few lines of code needed if we do not use an app. Following this, we will look at the app code.

Next, here is the server.R code.

And then, as needed, the ui.R script.

Cut and paste this code into the server.R and ui.R scripts in your directory and then execute the app.