-
Notifications
You must be signed in to change notification settings - Fork 1
/
tables.qmd
740 lines (680 loc) · 19 KB
/
tables.qmd
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
---
title: "Tables for Personal Finance (Ebenezer)"
author: "Deepansh Khurana"
format:
html:
code-fold: true
theme:
- cosmo
- style/custom.scss
editor: visual
---
::: {style="text-align: justify;"}
When I made the [Hrafnagud](https://github.com/DeepanshKhurana/Hrafnagud-Dynamo "Hrafnagud-Dynamo") project I recently talked about at **ShinyConf** (and am about to talk about at **useR!** in July and **EARL** in September), I also made several apps for myself to consume each API. The app and API for tracking my finances is called **Ebenezer**, named after the miserly Scrooge. My goal was to take each kind of financial instrument and make a table that fits well for it. Bonds work differently than Equity stocks, for example, and therefore, should be represented differently. The result was the following tables, which are now a part and parcel of my application.
:::
#### Key Features
::: {style="text-align: justify;"}
One of the key features of these tables are the way the colors are assigned automatically. This is done using a *get_css_class()* function which basically assigns the different colors and highlights wherever required.
Another important bit is how the tables themselves are constructed. Processing functions are written in the *cell* parameter of the *reactable()* call. The function then takes up a concatenated string and builds up a *div* structure that is styled with a custom scss file.
:::
## Setup
Here we setup some libraries, functions and load the dataframes for these examples.
```{r "setup"}
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(glue))
suppressPackageStartupMessages(library(lubridate))
suppressPackageStartupMessages(library(reactable))
suppressPackageStartupMessages(library(reactablefmtr))
suppressPackageStartupMessages(library(shiny))
source("utils/data_formatting.R")
files <- list.files("data", "*.csv")
paths <- paste("data", files, sep = "/")
data <- lapply(
paths,
read.csv
)
names(data) <- gsub(".csv", "", files)
```
Now that the data is loaded, we can take each table at a time.
### Ticker
::: {style="text-align: justify;"}
The ticker is a simple table that needs a few key pieces of information. The most important out of which is the correct semantic colour for rise and fall of a stock.
**Note:** While I usually consume this data from a custom API, for the sake of illustration and reproducibility, I have included a static file here as a .csv.
Let's try to make a Ticker Table.
:::
```{r "ticker"}
ticker_data <- data$ticker_data
colnames(ticker_data) <- c(
"Code", "Stock", "Price", "Change", "Percent"
)
ticker_data$Stock <- paste(
ticker_data$Code,
ticker_data$Stock,
sep = "_"
)
ticker_data$Price <-
paste(ticker_data$Price,
ticker_data$Change,
ticker_data$Percent,
sep = "_"
)
ticker_data <- ticker_data[, c("Stock", "Price")] |>
na.omit()
reactable(
data = ticker_data,
pagination = FALSE,
sortable = FALSE,
searchable = TRUE,
columns = list(
Stock = colDef(
align = "left",
cell = function(value) {
value <- strsplit(value, "_")[[1]]
div(
class = "stock-name-row",
div(
class = "stock-code",
value[1]
),
div(
class = "stock-name",
value[2]
)
)
}
),
Price = colDef(
align = "right",
cell = function(value) {
value <- strsplit(value, "_")[[1]]
price <- as.numeric(value[1])
change <- as.numeric(value[2])
percent <- as.numeric(value[3])
indicator <- get_css_class(change, type = "all")
div(
class = "stock-price-row",
div(
class = "stock-price",
style = list(
fontSize = "1.5em",
fontWeight = 600
),
paste0(
"$",
sub(
"\\.0+$",
"",
format_price(price)
)
)
),
div(
class = "stock-change-block",
div(
class = paste("stock-percent",
indicator$text,
indicator$highlight,
sep = " "
),
paste0(sub("-", "", preserve_zero(percent)), "%")
),
div(
class = paste("stock-change",
indicator$text,
sep = " "
),
sub("-", "", format_price(change))
)
),
)
}
)
)
)
```
### Stocks
::: {style="text-align: justify;"}
Alright, now that we have data for the ticker, we can create a stocks table. The table below gives you the portfolio with different fields such as Invested Value, Quantity, Average Price on the left, and Unrealized P&L, Realized P&L, Current Price on the right. If a user hovers on the icons, they will find these labels as well.
:::
```{r "stocks"}
portfolio <- data$stocks_data |>
mutate(
unrealized_percent =
true_round(
unrealized * 100 / holding_value, 2
)
) |>
mutate(
left =
paste(
stock_symbol,
name,
holding_value,
quantity,
avg_price,
sep = "_"
),
right = paste(
unrealized,
unrealized_percent,
realized,
current_price,
change_percent,
sep = "_"
)
) |>
select(left, right)
reactable(
data = portfolio,
pagination = FALSE,
searchable = TRUE,
columns = list(
left = colDef(
align = "left",
cell = function(value) {
value <- strsplit(value, "_")[[1]]
div(
class = "stock-left-area",
div(
class = "stock-name-row",
div(
class = "stock-code",
value[1]
),
div(
class = "stock-name",
value[2]
)
),
div(
class = "stock-holding-row",
div(
class = "stock-market-value small-row-left",
title = "Invested Value",
icon(name = "vault", class = "label"),
p(class = "value", format_price(value[3], 2))
),
div(
class = "stock-quantity small-row-left",
title = "Quantity",
icon(name = "hashtag", class = "label"),
p(class = "value", value[4])
),
div(
class = "stock-avg-price small-row-left",
title = "Average Price",
icon(name = "barcode", class = "label"),
p(class = "value", paste0("₹", true_round(value[5], 2)))
)
)
)
}
),
right = colDef(
align = "right",
cell = function(value) {
value <- strsplit(value, "_")[[1]]
unrealized_class <- get_css_class(value[2], type = "text")
realized_class <- get_css_class(value[3], type = "text")
current_price_class <- get_css_class(value[5], type = "text")
div(
class = "stock-right-area",
div(
class = "stock-unrealized-row small-row-right",
title = "Unrealized P&L",
icon(
name = "money-bill-transfer",
class = paste("stock-current-price label",
unrealized_class,
sep = " "
)
),
p(
class = paste("stock-unrealized value",
unrealized_class,
sep = " "
),
format_price(value[1])
),
p(
class = paste("stock-unrealized-percent value",
unrealized_class,
sep = " "
),
paste0("(", value[2], "%)")
)
),
div(
class = "stock-realized small-row-right",
title = "Realized P&L",
icon(
name = "receipt",
class = paste("stock-current-price label",
realized_class,
sep = " "
)
),
p(
class = paste("value",
realized_class,
sep = " "
),
format_price(value[3])
)
),
div(
class = "stock-current-price-row small-row-right",
title = "Current Price",
icon(
name = "comment-dollar",
class = paste("stock-current-price label",
current_price_class,
sep = " "
)
),
div(
p(
class = paste(
"stock-current-price value",
current_price_class,
sep = " "
),
value[4]
)
),
p(
class = paste(
"stock-current-price-percent value",
current_price_class,
sep = " "
),
paste0("(", value[5], "%)")
)
)
)
}
)
)
)
```
### Bonds
::: {style="text-align: justify;"}
Between Stocks and Bonds, something changes. Most stock data is current. You need the most crucial information immediately. With bonds, the information needed is a bit different. You need to know the current amount, the amount it will total to when the time period passes and the progress. So, with the help of *reactablefmtr*, I added a progress bar to the tables.
Another thing to note is that the progress bar color changes according to a palette. This gives the user visual feedback for how far their bonds have progressed to maturity. Darker is "matures soon".
:::
```{r "bonds"}
bonds <- data$sgb_data
today <- Sys.Date()
bonds$info <- paste(
bonds$name,
bonds$invested_date,
bonds$maturity_date,
bonds$category,
bonds$quantity,
bonds$unit_price,
bonds$total,
sep = "_"
)
bonds$progress <-
1 - as.numeric(
difftime(
format_date(bonds$maturity_date),
as.Date(today),
units = "days"
)
) / (8 * 365.25)
bonds <- bonds |>
select(info, progress) |>
rowwise() |>
mutate(
color = get_progress_bar_colour(
progress,
c(
"#a3b18a",
"#588157",
"#3a5a40",
"#344e41"
)
)
) |>
ungroup()
reactable(
bonds,
class = "sgb-reactable",
columns = list(
info = colDef(
name = "Bond",
cell = function(string) {
string <- strsplit(string, "_")[[1]]
div(
class = "sgb-row",
div(
class = "sgb-name",
gsub(
"BOND NAME",
"/",
string[1]
)
),
div(
class = "sgb-timeline",
year(format_date(string[2])),
icon("arrow-right"),
year(format_date(string[3]))
),
div(
class = "sgb-metrics-container",
div(
class = "sgb-category metric",
title = "Category",
icon("receipt", class = "label"),
p(string[4], class = "value")
),
div(
class = "sgb-quantity metric",
title = "Quantity",
icon("hashtag", class = "label"),
p(string[5], class = "value")
),
div(
class = "sgb-price metric",
title = "Unit Price",
icon("barcode", class = "label"),
p(format_price(string[6]), class = "value")
),
div(
class = "sgb-total metric",
title = "Total Invested",
icon("vault", class = "label"),
p(format_price(string[7]), class = "value")
),
)
)
}
),
progress = colDef(
cell = data_bars(
bonds,
max_value = 1,
fill_color_ref = "color",
background = "#f1f3f4",
round_edges = TRUE,
bar_height = 20,
number_fmt = scales::label_percent()
)
),
color = colDef(
show = FALSE
)
)
)
```
### Funds
::: {style="text-align: justify;"}
Funds work similar to the bonds with slight variation. In some ways, they are a mixture between Stocks and Bonds. But with all the tables covered above, this one requires no explanation. This is how we can reuse most of our CSS here as well.
The key here is a column called *is_sip_ongoing,* which is used to split the tables into two, with slightly different structures.
:::
#### SIPs
::: {style="text-align: justify;"}
For SIPs, we have a simple reminder for each row which tells the date for the installment and the amount.
:::
```{r "funds_sip"}
sips <- data$funds_data |>
mutate(
left = paste(
provider,
name,
total_invested,
unit_balance,
sep = "_"
),
right = paste(
current_value,
profit,
change_percent,
sep = "_"
)
) |>
select(
c(
left,
right,
is_sip_ongoing,
sip_date,
sip_amount
)
) |>
filter(is_sip_ongoing == 1) |>
mutate(
left = paste(
left,
sip_date,
sip_amount,
sep = "_"
)
) |>
select(
-c(
is_sip_ongoing,
sip_date,
sip_amount
)
)
reactable(
sips,
searchable = TRUE,
columns = list(
left = colDef(
name = "Invested",
align = "left",
cell = function(string) {
string <- strsplit(string, "_")[[1]]
div(
class = "sip-table",
div(
class = "fund-holding-container stock-left-area",
div(
class = "stock-name-row",
div(
class = "stock-code",
string[1]
),
div(
class = "stock-name",
string[2]
)
),
div(
class = "stock-holding-row",
div(
class = "fund-invested small-row-left",
title = "Invested Value",
icon(name = "vault", class = "label"),
p(class = "value", format_price(string[3], 2))
),
div(
class = "fund-units small-row-left",
title = "Total Units",
icon(name = "hashtag", class = "label"),
p(class = "value", true_round(string[4], 2))
)
),
p(
class = "fund-sip-information",
glue("${format_price(string[6], 0)} on the {string[5]}th")
)
)
)
}
),
right = colDef(
name = "Current",
align = "right",
cell = function(string) {
string <- strsplit(string, "_")[[1]]
price_class <- get_css_class(string[2], type = "text")
div(
class = "sip-table stock-right-area",
div(
class = "fund-current small-row-right",
icon(
name = "money-bill-transfer",
title = "Current Value",
class = paste("stock-current-price label",
price_class,
sep = " "
)
),
p(
format_price(string[1]),
title = "Change %",
class = paste("stock-current-price value",
price_class,
sep = " "
)
)
),
div(
class = "fund-profit small-row-right",
icon(
name = "comment-dollar",
class = paste("stock-current-price label",
price_class,
sep = " "
)
),
p(
glue("{string[3]}%"),
class = paste("stock-current-price value",
price_class,
sep = " "
)
)
)
)
}
)
)
)
```
#### Lumpsum
::: {style="text-align: justify;"}
The Lumpsum funds look very, very similar to the stocks interface and this is by design.
:::
```{r "funds_lumpsum"}
funds <- data$funds_data |>
mutate(
left = paste(
provider,
name,
total_invested,
unit_balance,
sep = "_"
),
right = paste(
current_value,
profit,
change_percent,
sep = "_"
)
) |>
filter(is_sip_ongoing != 1) |>
select(
c(
left,
right
)
)
reactable(
funds,
searchable = TRUE,
columns = list(
left = colDef(
name = "Invested",
align = "left",
cell = function(string) {
string <- strsplit(string, "_")[[1]]
div(
class = "sip-table",
div(
class = "fund-holding-container stock-left-area",
div(
class = "stock-name-row",
div(
class = "stock-code",
string[1]
),
div(
class = "stock-name",
string[2]
)
),
div(
class = "stock-holding-row",
div(
class = "fund-invested small-row-left",
title = "Invested Value",
icon(name = "vault", class = "label"),
p(class = "value", format_price(string[3], 2))
),
div(
class = "fund-units small-row-left",
title = "Total Units",
icon(name = "hashtag", class = "label"),
p(class = "value", true_round(string[4], 2))
)
)
)
)
}
),
right = colDef(
name = "Current",
align = "right",
cell = function(string) {
string <- strsplit(string, "_")[[1]]
price_class <- get_css_class(string[2], type = "text")
div(
class = "sip-table stock-right-area",
div(
class = "fund-current small-row-right",
icon(
name = "money-bill-transfer",
title = "Current Value",
class = paste("stock-current-price label",
price_class,
sep = " "
)
),
p(
format_price(string[1]),
title = "Change %",
class = paste("stock-current-price value",
price_class,
sep = " "
)
)
),
div(
class = "fund-profit small-row-right",
icon(
name = "comment-dollar",
class = paste("stock-current-price label",
price_class,
sep = " "
)
),
p(
glue("{string[3]}%"),
class = paste("stock-current-price value",
price_class,
sep = " "
)
)
)
)
}
)
)
)
```