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

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

#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), '
Date :', rownames(last31)[which.min(last31$Price)] )https://cran.r-project.org/package=coindeskr ) 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).

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
R-bloggers责编内容来自:R-bloggers (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合技术 » Building a Daily Bitcoin Price Tracker with Coindeskr and Shiny in R

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录