By Abdul Majed Raja.
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.
This tutorial is to help an R user build his/her own Daily Bitcoin Price Tracker using three packages, Coindeskr, Shiny and Dygraphs.
Coindeskr helps us access coindesk API to extract Bitcoin Price Index, including historic Bitcoin Prices, powered by Coindesk.
Shiny Structure and Script Naming
Every Shiny app 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
app.R that contains two functions
serverin the same code.
Start a new R Script
app.R in a new folder (of desired name) and proceed further with the following code.
Installation and Loading
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.
Extract Last 31 days Bitcoin Price
Let us use coindeskr’s handy function
get_last31days_price() to extract Bitcoin’s USD Price for the last 31 days and store the output in a dataframe (last31).
UI Elements to be displayed in the app
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.
UI should contain the following:
* Title of the App
* Minimum Bitcoin Price and its equivalent Date
* Maximum Bitcoin Price and its equivalent Date
* Interactive Time-Series Graph to see the trend of Bitcoin Price for the last 31 days
While the first three elements can be coded just with basic shiny functions, the last – Interactive Time-Series Graph – requires
dygraphOutput() function to display a dygraph object.
Server code of the Shiny App
This is where we have to extract information from the dataframe – last31 (created initially) and feed into the respective
output id – 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
renderDygraph() function to display the interactive (html) time-series graph created using
Running the Bitcoin Price Tracker Shiny App:
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).
Screenshot of the Bitcoin Price Tracker Shiny App:
Thus, Your Daily Bitcoin Price Tracker is ready! The code used above is available on my github and if you are interested in Shiny, you can learn more from Datacamp’s Building Web Applications in R with Shiny Course.
Bio: Abdul Majed Raja is an Analytics consultant, Helping organizations make some sense out of the massive - often not knowing what to do - data. Married to R (but dating Python).
Original. Reposted with permission.
- Natural Language Processing Library for Apache Spark – free to use
- Blockchain Key Terms, Explained
- Data Science vs Addiction: Estimating Opioid Abuse by Location