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.
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
#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
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
server Date :', rownames(last31)[which.min(last31$Price)] ) ) output$maxprice Date :', rownames(last31)[which.max(last31$Price)] ) ) output$btcprice % dyHighlight(highlightCircleSize = 5, highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = FALSE, highlightSeriesOpts = list(strokeWidth = 3)) %>% dyRangeSelector() ) }
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.
- Getting started with dplyr in R using Titanic Dataset
- How to apply Monte Carlo simulation to forecast Stock prices using Python
- Analysing iOS App Store iTunes Reviews in R
- Handling ‘Happy’ vs ‘Not Happy’: Better sentiment analysis with sentimentr in R
- Creating Reporting Template with Glue in R
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…
Source:: R News