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

综合技术 2018-01-10 阅读原文

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 server in 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


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(
  titlePanel('Bitcoin USD Price for Last 31 days'),

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), '
Date :', rownames(last31)[which.min(last31$Price)] ) ) output$maxprice <- renderText( paste('Price : $', max(last31), '
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 .

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



London police seize over $667,000 in bitcoin from ... London Metropolitan Police seized more than £500,000 ($667,000) in bitcoin from a hacker convicted of phishing attacks against 100 global com...
比特币下跌至6500美元,数字货币全球总市值2400亿美元... 自7月下旬以来,比特币(BTC)与其它加密货币的交易量大幅下降,降幅超过30%。鉴于此,分析家预测BTC的价格可能会跌至6000美元左右稳定,而目前报价6480美元。此外,全球数字货币市场总市值在8月8日早些时分创下今年新低,为2308.85亿美元,24h成交量则是150.28亿美元,交易量低使得数...
Top 6 Partnerships Capable of Taking Iota to the N... Partnerships play an integral role in the world of blockchain and cryptocurrency. Projects focusing on innovative concepts need to gain mainstream tra...
一个产品经理的区块链学习指南(三)... 一提起「区块链」,我们第一个想到的就是比特币,那么有了「比特币」为什么还需要「以太坊」呢? 「以太坊」是继「比特币」之后有一个「加密数字货币」。自2014年初Vitalik Buterin在北美比特币大会上首次谈及以太坊之后,这个新概念就得到了大众广泛的关注。另外,「以太坊」(Ethereum)...
区块链每日早报(9 月 12 日) 1、香港证监会正拟修订证券条例以监管 ICO 据《香港经济日报》最新的报道显示,香港证监会正拟修订证券条例以监测 ICO,该报道指出,当局留意到《证券及期货条例》(SFO)未必能有效及切合监管金融科技,或考虑与政府商讨需否修例,惟未有最终定案,也不会贸然更改监管方针以推动 ICO 发展。 2...