<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/reproducible-finance-with-r-a-shiny-etf-map/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.rstudio.com/rviews/2016/12/16/reproducible-finance-with-r-a-shiny-etf-map/"> RStudio</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>

<p>by Jonathan Regenstein</p>
<p>In a <a target="_blank" rel="nofollow" href="https://www.rstudio.com/rviews/2016/12/14/reproducible-finance-with-r-pulling-and-displaying-etf-data/">previous post</a>, 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.</p>
<p>The final app is <a target="_blank" rel="nofollow" href="http://colorado.rstudio.com:3939/Global-ETF-Map/">here</a>, with the code available in the upper right-hand corner. Let’s step through this script.</p>
<p>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.</p>
<p><iframe height="370px" width="1000px" src="https://beta.rstudioconnect.com/content/2252/reproducible_finance3_block1.html"></iframe></p>
<p>Next, we want to display that map in the first row of our flexdashboard. We will use ‘renderLeaflet’. Nothing fancy here. </p>
<p><iframe height="110px" width="1000px" src="https://beta.rstudioconnect.com/content/2253/reproducible_finance3_block2.html"></iframe></p>
<p>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 <code>getSymbols</code>, (3) import the price data time series, and (4) display the time series using <code>dygraphs</code>. All of this is going to be easy for us because of an important decision that we made when building our leaflet map object.</p>
<p>Recall that we set <code>layerID = ticker</code> 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 <code>observeEvent</code> function to capture the ID of whatever shape a user clicks, and we set that ID to be the ticker.</p>
<p><iframe height="90px" width="1000px" src="https://beta.rstudioconnect.com/content/2254/reproducible_finance3_block3.html"></iframe></p>
<p>Now, we have the ticker in the ‘clickedCountry’ reactive, and just need to pass it to <code>getSymbols</code>. 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.</p>
<p><iframe height="90px" width="1000px" src="https://beta.rstudioconnect.com/content/2255/reproducible_finance3_block4.html"></iframe></p>
<p>To graph the resulting xts object, which we have named ‘etf’, we pass it to <code>dygraph</code>. </p>
<p><iframe height="50px" width="1000px" src="https://beta.rstudioconnect.com/content/2256/reproducible_finance3_block5.html"></iframe></p>
<p>Remember, there were two key components: setting <code>layerID = ticker</code> and then capturing that <code>layerID</code> using <code>observeEvent</code> when a user clicks</p>
<p>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 <a target="_blank" rel="nofollow" href="https://www.rstudio.com/rviews/2016/11/18/reproducible-finance-with-r-a-sharpe-ratio-shiny-app/">Sharpe Ratio app</a>, 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.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.rstudio.com/rviews/2016/12/16/reproducible-finance-with-r-a-shiny-etf-map/"> RStudio</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/bLl0G8cZHDU"></a></div>