R shiny stock analysis

Introduction

This post we deploy our own R-shiny app.

  1. You can select a stock
  2. The app fetches the data, does some number crunching and plots the results

The source code is on github.

Bear in mind that I might not run the demo-server indefinitely, or that it might not be responsive, so first here’s a screenshot:

Here is the demo:

If you want to see this in a full page view here’s the link: https://arthought.shinyapps.io/15_stock_analysis/

Remark: This application might not be available as there is a fairly tight limit on the number of hours the free-plan shinyapps.io based apps will run. There is another server accessible using this link.

 

There are various options as to how you can use Shiny server:

  1. You can host your own server, instructions are here and then here
  2. You can sign up at https://www.shinyapps.io/

There are pros and cons to either, here’s a summary:

 Own serverhttps://www.shinyapps.io/
Complexity
Cost
Scalability

For this post we choose the free account option of https://www.shinyapps.io , which is limited to 5 Applications and 25 Active Hours (per month).

We then use an iframe to reference the shiny app in this post (as shown in the previous post).

Discussion of the code

In the typical shiny app there are two files, one representing the “frontend”/user interface called ui.R, and one representing the “backend” called server.R.  The ui.R defines the user interface widgets, the server.R defines e.g. calculations or plots.

So let’s briefly discuss these two components in our app.

The “frontend”/user interface called ui.R

In the ui.R file we have the following:

  • We download the current DAX components from a Wikipedia page, using the htmltab library : https://en.wikipedia.org/wiki/DAX
  • This list is fed into a selectInput box named DAX stock

    There is also a sliderInput
  • Once you, the user, select a stock this information is passed to server.R which defines that the data is downloaded from yahoo, analysed and shown in the plot; the server.R file is discussed below. However the way these plots are presented is defined in the ui.R. The definitions in the ui.R use a hierarchy of panels, in this case the hierarchy is mainPanel,tabsetPanel and tabPanel :

     

    The “backend” called server.R

In the server.R we have the following

  • We download the data for the DAX index, using the getSymbols function from the quantmod package.
  • The input “stock_id” from the selectInput box in the ui.R (=input$stock_id)  is fed into what’s called a reactive expression.
    The reactive expression is “informed” if and when any input has changed. When this happens the expression is recalculated, i.e. it reacts. In our case the reactive expression is the following function:

    When either input$stock_id or input$range are updated, the result of the datasetInput is updated wherever it is used, for instance in the plotting functions.
    What this “datasetInput” function actually returns is a merge of the DAX and the stock’s data, restricted to the range defined by the slider, where the range is specified in terms of two percentage values of the total number of rows of the data table.
  •  The server.R also has the definitions of the output plotting functions. These in turn make use of plotting functions from the package PerformanceAnalytics. The functions from PerformanceAnalytics that are used here are charts.PerformanceSummary, chart.Histogram and chart.Boxplot. As input they expect return type data. For charts.PerformanceSummary we use daily returns, for chart.Histogram and chart.Boxplot we use monthly returns.

    Please note that these plotting functions use the reactive function datasetInput(). Therefore the plots are automatically updated whenever the inputs input$stock_id or input$range, used in datasetInput(), have changed.

Success!