Site icon R-bloggers

Building a Daily Bitcoin Price Tracker with Coindeskr and Shiny in R

[This article was first published on R Programming – DataScience+, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 ui and 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).

last31 <- get_last31days_price() 

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.

ui <- shinyUI(
  fluidPage(
  titlePanel('Bitcoin USD Price for Last 31 days'),
  mainPanel(
    h3('Minimum'),
    h3(htmlOutput('minprice')),
    h3('Maximum'),
    h3(htmlOutput('maxprice')),
    dygraphOutput("btcprice")
  )
))

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 dygraph() function.

server <- function(input,output){
  
  output$minprice <- renderText(
    paste('Price : $', min(last31), '<br>Date :', rownames(last31)[which.min(last31$Price)] )
  )
  
  
  output$maxprice <- renderText(
    paste('Price : $', max(last31), '<br>Date :', rownames(last31)[which.max(last31$Price)] )
  )
  output$btcprice <- renderDygraph(
    dygraph(data = last31, main = "Bitcoin USD Price for Last 31 days") %>% 
      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).

shiny::runGitHub('amrrs/Bitcoin_price_tracker_Daily')

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.

Related Post

  1. Getting started with dplyr in R using Titanic Dataset
  2. How to apply Monte Carlo simulation to forecast Stock prices using Python
  3. Analysing iOS App Store iTunes Reviews in R
  4. Handling ‘Happy’ vs ‘Not Happy’: Better sentiment analysis with sentimentr in R
  5. Creating Reporting Template with Glue in R

To leave a comment for the author, please follow the link and comment on their blog: R Programming – DataScience+.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.