-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtracker.Rmd
160 lines (119 loc) · 4.81 KB
/
tracker.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: "crypto tracker"
output:
flexdashboard::flex_dashboard:
orientation: rows
css: www/styles.css
logo: www/favicon.ico
favicon: www/favicon.ico
social: [ "twitter", "linkedin", "menu"]
source_code: "https://github.com/PaulC91/crypto_tracker"
runtime: shiny
---
```{r global, include=FALSE}
library(flexdashboard)
library(googlesheets)
library(tidyverse)
library(dygraphs)
library(xts)
# to setup googlesheet auth (comment out once done) -----------------------------------
#shiny_token <- gs_auth() # authenticate w/ your desired Google identity here
#saveRDS(shiny_token, "shiny_app_token.rds")
googlesheets::gs_auth(token = "shiny_app_token.rds")
sheet_key <- "1T77AEDJxLtb7_sr4nrfyYR9Xkj6w0mMDFKhsMSK-Ykc"
coinz_raw <- googlesheets::gs_key(sheet_key) %>%
gs_read_csv(ws = "CoinDB") %>%
distinct()
```
Inputs {.sidebar}
-----------------------------------------------------------------------
Welcome to the [Culture of Insight's](https://cultureofinsight.com/) crypto tracking tool.
The tracker fetches real-time prices from the [coinmarketcap API](https://coinmarketcap.com/api/) every hour using a cron job Rscript on a cloud server then writes them to a googlesheets database which is then read into this app.
For personalised metrics, enter the amount of the selected coin you own and when you acquired it below:
---
```{r}
selectInput("coin", "What Crypto?", c("Bitcoin", "Ethereum"))
numericInput("n", "How many coins?", 1, min = 0, max = 100, step = 0.01)
dateInput("date", "Date Purchased:", value = "2018-01-01 00:00:00", min = NULL, max = Sys.time(),
format = "d MM yyyy", startview = "month", weekstart = 0,
language = "en", width = NULL)
radioButtons('currency', 'Currency', c('GBP', 'USD'), inline = TRUE)
```
---
Use the range slider at the bottom of the chart to see historical prices.
This dashboard was built using:
- [R](https://www.r-project.org/)
- [shiny](http://rmarkdown.rstudio.com/flexdashboard/shiny.html)
- [RCrypto](https://cran.r-project.org/web/packages/RCrypto/index.html)
- [googlesheets](https://github.com/jennybc/googlesheets)
- [cron](https://en.wikipedia.org/wiki/Cron)
- [dygraphs](https://rstudio.github.io/dygraphs/)
Contact [paul@cultureofinsight.com](mailto:paul@cultureofinsight.com) with any enquires.
Row
-----------------------------------------------------------------------
### Current Value
```{r}
valueBoxOutput("value")
```
### ROI
```{r}
valueBoxOutput("roi")
```
### ROI %
```{r}
valueBoxOutput("roi_pct")
```
Row
-----------------------------------------------------------------------
###
```{r}
dygraphOutput("chart")
```
```{r, server}
coinz <- reactive({
coinz_raw %>%
filter(name == input$coin) %>%
select(-name) %>%
mutate(USD = input$n * price_usd,
GBP = input$n * price_gbp)
})
stock <- reactive({
coinz() %>%
filter(date >= input$date) %>%
mutate(daily_pct = (price_usd - lag(price_usd)) / lag(price_usd)) %>%
mutate(total_pct = (price_usd - price_usd[1]) / price_usd[1])
})
column_select <- reactive({
switch(input$currency,
GBP = 5,
USD = 4)
})
output$value <- renderValueBox({
value <- dplyr::last(stock()[[column_select()]])
render_value <- paste0(ifelse(column_select() == 5, "£", "$"), round(value, digits = 0))
valueBox(render_value,
paste("Current Value Last Updated @ ", format(max(stock()$date), format = "%H:%M")),
icon = ifelse(column_select() == 5, "fa-gbp", "fa-usd"), color = "SteelBlue")
})
output$roi <- renderValueBox({
gainz <- dplyr::last(stock()[[column_select()]]) - dplyr::first(stock()[[column_select()]])
render_value <- paste0(ifelse(column_select() == 5, "£", "$"), round(gainz, digits = 0))
valueBox(render_value, "ROI", icon = "fa-line-chart", color = "DarkCyan")
})
output$roi_pct <-renderValueBox({
pct <- dplyr::last(stock()$total_pct)
render_value <- paste0(round(pct, digits = 3) * 100, "%")
valueBox(render_value, "ROI %", icon = "fa-percent", color = "LightCoral")
})
output$chart <- renderDygraph({
coinz_ts <- xts(coinz()[,column_select()], coinz()$date)
dygraph(coinz_ts, main = paste(input$n, input$coin, input$currency, "Value")) %>%
#dySeries("V1", label = input$currency) %>%
dyAxis("x", valueFormatter = 'function(ms) {return new Date(ms).toLocaleString();}') %>%
dyAxis("y", axisLabelFormatter = 'function(d){return d.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ",");}') %>%
dyEvent(input$date, "Date of Purchase", labelLoc = "bottom") %>%
dyRangeSelector(dateWindow = c(as.character(input$date - 7), as.character(Sys.time()))) %>%
dyLimit(as.numeric(coinz_ts[as.character(input$date)]), color = "red") %>%
dyOptions(fillGraph = TRUE, gridLineWidth = .1)
})
```