<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/building-a-daily-bitcoin-price-tracker-with-coindeskr-and-shiny-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/building-a-daily-bitcoin-price-tracker-with-coindeskr-and-shiny-in-r/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Let’s admit it. The whole world has been going crazy with Bitcoin. Bitcoin (BTC), the first cryptocurrency (in fact, the first digital currency to solve the double-spend problem) introduced by Satoshi Nakamoto has become bigger than well-established firms (even a few countries). So, a lot of Bitcoin Enthusiasts and Investors are looking to keep a track of its daily price to better read the market and make moves accordingly. </p>
<p>This tutorial is to help an R user build his/her own Daily Bitcoin Price Tracker using three packages, Coindeskr, Shiny and Dygraphs. </p>
<p><code><a rel="nofollow" href="https://cran.r-project.org/package=coindeskr" target="_blank">Coindeskr</a></code> helps us access coindesk API to extract Bitcoin Price Index, including historic Bitcoin Prices, powered by <a rel="nofollow" href="https://www.coindesk.com/api/" target="_blank">Coindesk</a>. </p>
<h2> Shiny Structure and Script Naming </h2>
<p>Every <a rel="nofollow" href="https://datascienceplus.com/building-a-simple-sales-revenue-dashboard-with-r-shiny-shinydashboard/" target="_blank">Shiny app </a>contains two parts – the UI part and the Server part. This post follows single file layout of designing the Shiny app where the shiny app contains one single file <code>app.R</code> that contains two functions <code>ui</code> and <code>server</code>in the same code. </p>
<p>Start a new R Script <code>app.R</code> in a new folder (of desired name) and proceed further with the following code. </p>
<h2>Installation and Loading </h2>
<pre>
#install.packages('shiny')
#install.packages('coindeskr')
#install.packages('dygraphs')

library(shiny) #To build the shiny App
library(coindeskr) #R-Package connecting to Coindesk API 
library(dygraphs) #For interactive Time-series graphs
</pre>
<p>To start with let us load the required packages – Shiny, Coindeskr and dygraphs – as mentioned in the above code. If the above packages are not already available on your machine, Please install them before loading. </p>
<h2>Extract Last 31 days Bitcoin Price</h2>
<p>Let us use coindeskr’s handy function <code>get_last31days_price()</code> to extract Bitcoin’s USD Price for the last 31 days and store the output in a dataframe (last31).</p>
<pre>
last31 &lt;- get_last31days_price() 
</pre>
<h2>UI Elements to be displayed in the app</h2>
<p>A Bitcoin Price Tracker should not only display the graph of the last one month price information but also should give us some highlights or summary, hence let us also display the minimum and maximum price of Bitcoin in the given time period and dates of when it was minimum and maximum.</p>
<p>UI should contain the following:</p>
<p>* Title of the App<br>
* Minimum Bitcoin Price and its equivalent Date<br>
* Maximum Bitcoin Price and its equivalent Date<br>
* Interactive Time-Series Graph to see the trend of Bitcoin Price for the last 31 days</p>
<p>While the first three elements can be coded just with basic shiny functions, the last – Interactive Time-Series Graph – requires <code>dygraphOutput()</code> function to display a dygraph object.</p>
<pre>
ui &lt;- shinyUI(
  fluidPage(
  titlePanel('Bitcoin USD Price for Last 31 days'),
  mainPanel(
    h3('Minimum'),
    h3(htmlOutput('minprice')),
    h3('Maximum'),
    h3(htmlOutput('maxprice')),
    dygraphOutput(&quot;btcprice&quot;)
  )
))
</pre>
<h2>Server code of the Shiny App</h2>
<p>This is where we have to extract information from the dataframe – last31 (created initially) and feed into the respective <code>output id</code> – minprice, maxprice and btcprice – that are defined in the ui part of the code. To extract minimum Bitcoin price and maximum Bitcoin price, min() and max() function are used respectively and to which.min() and which.max() are used, that return the rownames of minimum and maximum price values, where rownames contain equivalent dates. And as mentioned earlier, dygraphs package provides <code>renderDygraph()</code> function to display the interactive (html) time-series graph created using <code>dygraph()</code> function.</p>
<pre>
server &lt;- function(input,output){
  
  output$minprice &lt;- renderText(
    paste('Price : $', min(last31), '<br>Date :', rownames(last31)[which.min(last31$Price)] )<a rel="nofollow" href="https://cran.r-project.org/package=coindeskr" target="_blank"></a>
  )
  
  
  output$maxprice &lt;- renderText(
    paste('Price : $', max(last31), '<br>Date :', rownames(last31)[which.max(last31$Price)] )
  )
  output$btcprice &lt;- renderDygraph(
    dygraph(data = last31, main = &quot;Bitcoin USD Price for Last 31 days&quot;) %>% 
      dyHighlight(highlightCircleSize = 5, 
                  highlightSeriesBackgroundAlpha = 0.2,
                  hideOnMouseOut = FALSE, highlightSeriesOpts = list(strokeWidth = 3)) %>%
      dyRangeSelector()
  )
}
</pre>
<h2>Running the Bitcoin Price Tracker Shiny App:</h2>
<p>If your code is ready, The shiny app can be run (as usual) using the Run App button on the top right of your RStudio. If your code is not yet ready, you can use the following code the run the shiny app directly from Github (provided all the required packages – shiny, coindeskr, dygraphs – are already installed).</p>
<pre>
shiny::runGitHub('amrrs/Bitcoin_price_tracker_Daily')
</pre>
<h2>Screenshot of the Bitcoin Price Tracker Shiny App:</h2>
<p><a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/01/btc_tracker_shiny.png?ssl=1" target="_blank"><img sizes="(max-width: 929px) 100vw, 929px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/01/btc_tracker_shiny.png?w=450&ssl=1" alt="" class="wp-image-10382" data-recalc-dims="1"></a></p>
<p>Thus, Your Daily Bitcoin Price Tracker is ready! The code used above is available on <a rel="nofollow" href="https://github.com/amrrs/Bitcoin_price_tracker_Daily" target="_blank">my github</a> and if you are interested in Shiny, you can learn more from Datacamp’s <a rel="nofollow" href="https://www.datacamp.com/courses/building-web-applications-in-r-with-shiny?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">Building Web Applications in R with Shiny Course</a>.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/getting-started-with-dplyr-in-r-using-titanic-dataset/" title="Permanent Link toGetting started with dplyr in R using Titanic Dataset" target="_blank">Getting started with dplyr in R using Titanic Dataset</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/how-to-apply-monte-carlo-simulation-to-forecast-stock-prices-using-python/" title="Permanent Link toHow to apply Monte Carlo simulation to forecast Stock prices using Python" target="_blank">How to apply Monte Carlo simulation to forecast Stock prices using Python</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/analysing-ios-app-store-itunes-reviews-in-r/" title="Permanent Link toAnalysing iOS App Store iTunes Reviews in R" target="_blank">Analysing iOS App Store iTunes Reviews in R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/handling-happy-vs-not-happy-better-sentiment-analysis-with-sentimentr-in-r/" title="Permanent Link toHandling ‘Happy’ vs ‘Not Happy’: Better sentiment analysis with sentimentr in R" target="_blank">Handling ‘Happy’ vs ‘Not Happy’: Better sentiment analysis with sentimentr in R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/creating-reporting-template-with-glue-in-r/" title="Permanent Link toCreating Reporting Template with Glue in R" target="_blank">Creating Reporting Template with Glue in R</a></li>
</ol>
	<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://datascienceplus.com/building-a-daily-bitcoin-price-tracker-with-coindeskr-and-shiny-in-r/"> R Programming – DataScience+</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/t3t5wn7Kgs0" height="1"></a></div>