Reproducible Finance with R: A Shiny ETF Map

December 16, 2016
By

(This article was first published on RStudio, and kindly contributed to R-bloggers)

by Jonathan Regenstein

In a previous post, we built an R Notebook that laid the groundwork for a Shiny app that allows users to graph country ETFs by clicking on a world map. In today’s Fun Friday post, we’ll charge forth to build that app, again using a flexdashboard so that we can stay in the Rmarkdown schema. Devoted readers of this blog know that I have a predilection for the Notebook-to-flexdashboard workflow, for reasons of efficiency and reproducibility.

The final app is here, with the code available in the upper right-hand corner. Let’s step through this script.

In the first code chunk, we load the ‘etfData.RDat’ file, the same file that we created in the dataGrab Notebook to save our shapefile of the world. After loading that file, we use the exact same code from the Notebook to construct our leaflet map.

Next, we want to display that map in the first row of our flexdashboard. We will use ‘renderLeaflet’. Nothing fancy here.

Now, we want to add the ability for the user to click a country and see the price history of that country’s ETF. This will require the following steps: (1) click a country, (2) pass the ETF ticker symbol to getSymbols, (3) import the price data time series, and (4) display the time series using dygraphs. All of this is going to be easy for us because of an important decision that we made when building our leaflet map object.

Recall that we set layerID = ticker in the view. This is part of the magic of the shapefile. It allows us to capture the ticker symbol associated with a country shape when a user clicks. Our ticker column is associated with our country longitudes and latitudes. In the code chunk below, we use an observeEvent function to capture the ID of whatever shape a user clicks, and we set that ID to be the ticker.

Now, we have the ticker in the ‘clickedCountry’ reactive, and just need to pass it to getSymbols. One important note: if a user clicks on a country with no ETF, that reactive object will be ‘NA’ or null. The first step in the code chunk below is to validate the entry and, if it is null, let the user know that a country with no ETF has been clicked.

To graph the resulting xts object, which we have named ‘etf’, we pass it to dygraph.

Remember, there were two key components: setting layerID = ticker and then capturing that layerID using observeEvent when a user clicks

That’s it for building this app, but keep in mind: this interface need not be limited to country ETFs. We could wire this up to display currency prices, interest rates, labor force participation rates, or, say, the 10 best-performing stocks in a country. We could combine this map with the Sharpe Ratio app, so that a user could build his portfolio by clicking on different countries, selecting weightings to those countries, and then calculating the Sharpe Ratio. Instead of simply displaying a time series of prices, we could display the results of a forecasting model for each country. The nice thing is that once we have our template to use a map as an interface to data, the world is our playground.

To leave a comment for the author, please follow the link and comment on their blog: RStudio.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)