-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.R
3693 lines (2575 loc) · 170 KB
/
app.R
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##########################################################################################
#NBA Game Density APP
#Jose Fernandez
#March-June 2020 (Updated March 2022)
#APP File
###########################################################################################
#loading all required libraries with functions used for different aspects in the app####
library(maps)
library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(shinydashboardPlus)
library(tidyverse)
library(DT)
library(formattable)
library(dashboardthemes)
library(readxl)
library(RcppRoll)
library(plotly)
library(shinyBS)
library(shinyalert)
library(shinycustomloader)
library(geosphere)
library(waiter)
library(shinyjs)
library(ballr)
library(grid)
library(jpeg)
library(png)
library(feather)
library(sever)
###################################################
#import feathers objects. This loads the initial datasets that will be used throughout the app####
articles <- read_feather("article.feather")
highlights <- read_feather("highlights.feather")
highlights2 <- read_feather("highlights2.feather")
pro_file <- read_feather("pro_file.feather")
shotchart <- read_feather("shotchart.feather")
game_logs <- read_feather("gamelogs.feather")
sche <- read_feather("sche.feather")
acities <- read_feather("acities.feather")
Logos <- read_feather("logos.feather")
toronto <- c("Toronto", 43.65, -79.38) #for map
##################################################
#Import NBA team logos and create a table to use later in the app####
df <- data.frame(val = c("Atlanta Hawks","Boston Celtics", "Brooklyn Nets", "Charlotte Hornets", "Chicago Bulls", "Cleveland Cavaliers", "Dallas Mavericks",
"Denver Nuggets", "Detroit Pistons", "Golden State Warriors", "Houston Rockets", "Indiana Pacers", "Los Angeles Clippers", "Los Angeles Lakers",
"Memphis Grizzlies", "Miami Heat", "Milwaukee Bucks", "Minnesota Timberwolves", "New Orleans Pelicans", "New York Knicks", "Oklahoma City Thunder",
"Orlando Magic", "Philadelphia 76ers", "Phoenix Suns", "Portland Trail Blazers", "Sacramento Kings", "San Antonio Spurs", "Toronto Raptors",
"Utah Jazz", "Washington Wizards"))
df$img2 = c(
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/atlanta-hawks-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[1]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/boston-celtics-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[2]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/brooklyn-nets-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[3]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/charlotte-hornets-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[4]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/chicago-bulls-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[5]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/cleveland-cavaliers-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[6]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/dallas-mavericks-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[7]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/denver-nuggets-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[8]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/detroit-pistons-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[9]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/golden-state-warriors-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[10]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/houston-rockets-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[11]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/indiana-pacers-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[12]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/los-angeles-clippers-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[13]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/los-angeles-lakers-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[14]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/memphis-grizzlies-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[15]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/miami-heat-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[16]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/milwaukee-bucks-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[17]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/minnesota-timberwolves-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[18]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/new-orleans-pelicans-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[19]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/new-york-knicks-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[20]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/oklahoma-city-thunder-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[21]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/orlando-magic-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[22]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/philadelphia-76ers-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[23]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/phoenix-suns-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[24]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/portland-trail-blazers-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[25]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/sacramento-kings-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[26]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/san-antonio-spurs-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[27]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/toronto-raptors-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[28]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/utah-jazz-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[29]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/washington-wizards-logo.png' width=45px><div class='jhr'>%s</div></img>", df$val[30]))
df$img = c(
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/atlanta-hawks-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[1]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/boston-celtics-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[2]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/brooklyn-nets-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[3]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/charlotte-hornets-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[4]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/chicago-bulls-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[5]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/cleveland-cavaliers-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[6]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/dallas-mavericks-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[7]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/denver-nuggets-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[8]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/detroit-pistons-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[9]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/golden-state-warriors-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[10]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/houston-rockets-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[11]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/indiana-pacers-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[12]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/los-angeles-clippers-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[13]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/los-angeles-lakers-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[14]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/memphis-grizzlies-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[15]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/miami-heat-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[16]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/milwaukee-bucks-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[17]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/minnesota-timberwolves-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[18]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/new-orleans-pelicans-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[19]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/new-york-knicks-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[20]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/oklahoma-city-thunder-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[21]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/orlando-magic-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[22]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/philadelphia-76ers-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[23]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/phoenix-suns-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[24]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/portland-trail-blazers-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[25]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/sacramento-kings-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[26]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/san-antonio-spurs-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[27]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/toronto-raptors-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[28]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/utah-jazz-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[29]),
sprintf("<img src='https://cdn.freebiesupply.com/images/thumbs/2x/washington-wizards-logo.png' width=30px><div class='jhr'>%s</div></img>", df$val[30]))
##################################################
#links social media buttons. URL address for the social media links in the app####
url <- "https://twitter.com/intent/tweet?text=Check out this app to explore the density of the schedule in the NBA. @NBAGameDensity&url=https://josedv.shinyapps.io/NBASchedule/"
url2 <- "https://josedv.shinyapps.io/NBASchedule/"
##################################################
#code for sever reloading screen####
disconnected <- tagList(
h1("NBA Game Density APP"),
p("Disconnection due to inactivity"),
reload_button("REFRESH", class = "warning")
)
##################################################
#User Interface: This is the part that users can see and interact with. It defines what the app looks like
############################################################################################
ui <- dashboardPage(
#Header: The code below defines aspect related to the header of the app############
header = dashboardHeader(
leftUi = tagList(
#Code for the NBAtwitter feed
dropdownBlock(
id = "twitterdropdown",
title = "NBA Feed",
icon = icon("twitter-square"),
badgeStatus = NULL,
HTML('<a class="twitter-timeline" data-height="600" href="https://twitter.com/NBAGameDensity/lists/nba-game-density-app?ref_src=twsrc%5Etfw">A Twitter List by NBAGameDensity</a> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>')
)),
#github code
tags$li(class = "dropdown", actionLink("welcome1", label = 'Code', icon = icon("github"), style = 'color:white',
onclick ="window.open('https://github.com/josedv82/NBA-Game-Density-App', '_blank')"),
bsTooltip("welcome1", HTML("Link to app repository."), placement = "bottom", trigger = "hover", options = NULL)),
#popup modal (window) link. This code opens a window with basic explanation of the app
tags$li(class = "dropdown", actionLink("welcome", label = 'About', icon = icon("info-circle"), style = 'color:white'),
bsTooltip("welcome", HTML("Click to know more about this app."), placement = "bottom", trigger = "hover", options = NULL)),
#This code enables the use of a right side bar on the app, which we will use to add user inputs
controlbarIcon = icon("cogs"),
#this code shows an icon (logo) on the top left of the screen the the left side bar is collapsed
title = tagList(
shiny::span(class = "logo-lg", "NBA Schedule Density", style = "color:white"),
img(src = "hexlogo.png", width = "35px", height = "33px")),
titleWidth = 330),
##################################################
#sidebar######
#the following code crates funtionality on the left side bar (menu). Users will use this functionality to interact and access
#different parts of the app
sidebar = dashboardSidebar(
width = 330, #width of the left side bar
sidebarMenu(
#fluidRow(width = "100%", align = "center", #style = "padding-right:45px",
#tags$br(),
#column(width = 5),
#column(width = 2, style = "padding-right:45px",
# Create a twitter button for users to share the app on twitter
# tags$a(href = url, "Tweet", class="twitter-share-button", `data-show-count` = "true"),
# includeScript("http://platform.twitter.com/widgets.js")),
#column(width = 1),
# Create 'linkedin-share-button' for users to share the app on linkedin
# column(width = 2, style = "padding-right:45px",
# tags$script(src = "https://platform.linkedin.com/in.js", type = "text/javascript", "lang: en_US"),
# tags$script(type = "IN/Share",`data-url` = url2)),
#column(width = 2)
#),
#Add a season filter. Users can filter the last 3 NBAseasons (17-18, 18-19, 19-20)
fluidRow(width = "100%", align = "center",
pickerInput(
inputId = "season_filter",
label = p("Select Season:", style = "padding-right:160px"),
choices = sche$Season %>% unique() %>% sort(),
selected = "2021-22",
choicesOpt = list(style = rep(("color: black; background: white"),30)),
multiple = F)
),
#Team by Team tab. This tab contains individual reports for each team
menuItem("Team by Team", tabName = "teambyteam", icon = icon("project-diagram"), startExpanded = T,
#team filter: users can select which team they can to look at
menuSubItem(icon = NULL, tabName = "teambyteam",
pickerInput(
inputId = "team_filter",
label = "Select Team",
choices = df$val,
choicesOpt = list(style = rep(("color: black; background: white"),30), content = df$img),
multiple = F)),
tags$br(),
menuSubItem(icon = NULL, tabName = "teambyteam", fluidRow(withLoader(DT::dataTableOutput("daily_games"), type ="html", loader = "loader1"), style="padding-right: 17px;")),
tags$br()
),
#All Teams Tab: This tab contains reports for all teams. Enables users to compare data across teams
menuItem("All Teams", tabName = "allteams", icon = icon("th"), startExpanded = F),
#Recommended Reading Tab. This tab displays an interactive table with related research and articles that users can explore
menuItem("Research / Media Articles", tabName = "reading", icon = icon("book-reader"), startExpanded = F),
tags$br(),
tags$br(),
tags$br(),
tags$hr(),
userBox(
title = userDescription(
title = "airball",
#subtitle = h6(HTML("Check out my R package")),
type = 2,
image = "https://raw.githubusercontent.com/josedv82/airball/master/man/images/airballlogo.PNG",
),
status = "primary",
background = "light-blue",
width = 12,
collapsible = F,
headerBorder = T,
h5(HTML("After creating this app, I published {airball}. <br>
An R package to quickly extract NBA <br> schedule metrics and more since the 1947 <br> season.")),
footer = tags$a(href = "https://github.com/josedv82/airball", "Go to airball", style = "color:dodgerblue; text-align: left")
)
)#sidebar menu
),
##################################################
#Dashboard body. The part of the app that contains the main reports##########
body = dashboardBody(
use_waiter(), #this function is needed to add the loader page displayed while the app loads
#bring in sever dependencies
useSever(),
#this code supresses potential error messages that may confuse the user
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
#this sets the width of dropdownBlock where twitter feed is embedded on the header part
tags$head(tags$style(HTML('.navbar-custom-menu>.navbar-nav>li>.dropdown-menu {width:400px;}'))),
#this code helps align the team logos in the picker input displayed on the left side bar
tags$head(tags$style(".jhr{ display: inline;vertical-align: middle;padding-left: 10px;}")),
#this code helps prevent Data Tables from altering default table font colors automatically
tags$style(HTML("
.dataTables_wrapper .dataTables_length, .dataTables_wrapper .dataTables_filter, .dataTables_wrapper .dataTables_info, .dataTables_wrapper .dataTables_processing, .dataTables_wrapper .dataTables_paginate {
color:lightblue;
}
thead {
color:white;
}
tbody {
color:white;
}
")),
#this sets the customised look for slider inputs on the right side bar
chooseSliderSkin("Flat"),
#this sets the custom theme for the app, In our case we are using a dark grey theme
shinyDashboardThemes(theme = "grey_dark"),
#this function is needed to initialize shiny alert for modal and popups windows
useShinyalert(),
#this function is needed for shinyJS, which allows to customise some functionalities on the background
useShinyjs(),
# here we are going to start designing what the dashboard body is going to look like
tabItems(
#the code below relates to the "Team by Team tab.
tabItem(
tabName = "teambyteam",
tabBox(title = "", id = "tab1", height = "100%", width = "100%",
#user interface for the Game Card tab
tabPanel("Game Card", icon = icon("map-marked-alt"),
fluidRow(column(width = 6, uiOutput("date")),
column(width = 6, align = "right",
actionButton("gdash", label = '', icon = icon("info-circle"), style = 'color:#e74c3c; background-color:#343E48; border-color:#343E48'),
bsTooltip("gdash", HTML("About This Dashboard"), placement = "left", trigger = "hover", options = NULL)
)),
tags$hr(),
fluidRow(
#Selected team data
column(width = 4, align="center",
htmlOutput("team_logo"),
uiOutput("team.pct"),
uiOutput("team"),
htmlOutput("location_team"),
uiOutput("team_points"),
tags$strong(htmlOutput("team_label")),
tags$br(),
htmlOutput("density_team"),
tags$br(),
htmlOutput("text_index1"),
htmlOutput("team_index"),
tags$br(),
htmlOutput("text_movindex1"),
htmlOutput("team_movindex")
),
#game details, buttons for stats, shotchart and youtube highlights
column(width = 4, align = "center",
tags$strong(htmlOutput("city")),
htmlOutput("arena"),
tags$br(),
tags$strong(htmlOutput("route")),
htmlOutput("distance"),
tags$br(),
actionButton("gamelogs", label = '', icon = icon("table"), style = 'color:white; background-color:seagreen'),
bsTooltip("gamelogs", HTML("Game Stats"), placement = "left", trigger = "hover", options = NULL),
tags$head(tags$style("#gamelogmodal .modal-dialog{ width:1700px}")),
actionButton("shotchart", label = '', icon = icon("basketball-ball"), style = 'color:white; background-color:darkcyan'),
bsTooltip("shotchart", HTML("Shot Chart"), placement = "bottom", trigger = "hover", options = NULL),
actionButton("video_yes", label = '', icon = icon("youtube"), style = 'color:white; background-color:#c4302b'),
bsTooltip("video_yes", HTML("Video Highlights"), placement = "right", trigger = "hover", options = NULL),
withLoader(plotOutput("map_plot", width = "100%"), type = "html", loader = "loader1")
),
#Opponent's Data
column(width = 4, align="center",
htmlOutput("opponent_logo"),
uiOutput("opponent.pct"),
uiOutput("opponent"),
htmlOutput("location_opponent"),
uiOutput("opponent_points"),
tags$strong(htmlOutput("opponent_label")),
tags$br(),
htmlOutput("density_opponent"),
tags$br(),
htmlOutput("text_index2"),
htmlOutput("opponent_index"),
tags$br(),
htmlOutput("text_movindex2"),
htmlOutput("opponent_movindex")
)
) #fluidrow
),#tabpanel
# the code below is for the "Schedule Table" tab user interface
tabPanel("Schedule Table", icon = icon("calendar-alt"),
fluidRow(column(12, align="right",
actionButton("pIndex", label = '', icon = icon("info-circle"), style = 'color:#e74c3c; background-color:#343E48; border-color:#343E48'),
bsTooltip("pIndex", HTML("About Game Index"), placement = "left", trigger = "hover", options = NULL)
)),
fluidRow(column(width = 12, withLoader(DT::dataTableOutput("team_table", width = "100%"), type = "html", loader = "loader1")))),
# The code below is the the "Rolling Density" tab user interface
tabPanel("Rolling Density", icon = icon("chart-line"),
fluidRow(column(width = 12, align = "right",
actionButton("denplot", label = '', icon = icon("info-circle"), style = 'color:#e74c3c; background-color:#343E48; border-color:#343E48'),
bsTooltip("denplot", HTML("About This Dashboard"), placement = "left", trigger = "hover", options = NULL))),
fluidRow(column(width = 12, withLoader(plotlyOutput("team_plot", height = "auto", width = "100%"), type = "html", loader = "loader1")))),
# The code below is the the "Outcome" tab user interface
tabPanel("Outcome", icon = icon("tasks"),
fluidRow(column(width = 12, align = "right",
actionButton("counts", label = '', icon = icon("info-circle"), style = 'color:#e74c3c; background-color:#343E48; border-color:#343E48'),
bsTooltip("counts", HTML("About This Dashboard"), placement = "left", trigger = "hover", options = NULL))),
fluidRow(style = "padding:15px", column(width = 8, DT::dataTableOutput("w_l_table")), column(width = 4,
fluidRow(DT::dataTableOutput("H_A_table")),
tags$br(),
tags$br(),
fluidRow(DT::dataTableOutput("team_table_density"))))),
# The code below is for the "Player Load" tab user interface
tabPanel("Player Load", icon = icon("charging-station"),
fluidRow(column(12, align="right",
actionButton("pLoad", label = '', icon = icon("info-circle"), style = 'color:#e74c3c; background-color:#343E48; border-color:#343E48'),
bsTooltip("pLoad", HTML("About Player Load"), placement = "left", trigger = "hover", options = NULL)
)),
tags$br(),
fluidRow(column(width = 12,
box(
title = "Full Roster",
closable = F,
enable_label = F,
status = "info",
collapsible = TRUE,
solidHeader = F,
enable_sidebar = F,
collapsed = TRUE,
width = NULL,
p(
fluidRow(column(width = 12, withLoader(plotOutput("player_load", width = "100%", height = '750px'),type = "html", loader = "loader1")))
)))),
fluidRow(column(width = 12,
box(
title = "Individual Player Load",
closable = F,
width = NULL,
enable_label = F,
status = "info",
solidHeader = FALSE,
collapsible = TRUE,
collapsed = FALSE,
enable_sidebar = FALSE,
p(
column(width = 2,
fluidRow(uiOutput("player_image")),
fluidRow(uiOutput("playerLoad.filter")),
tags$br(),
tags$br(),
fluidRow(uiOutput("last.n.filter")),
fluidRow(DT::dataTableOutput("playerLoad.table"))
),
column(width = 10,
plotlyOutput("player.load.trend", width = "100%", height = '750px')
)
)
)
)))
)#tabbox
),#tabitem
# The code below relates to the "ALL TEAMS" tab
tabItem(
tabName = "allteams",
tabBox(title = "", id = "tab2", height = "100%", width = "100%",
#this sets the user interface for the "Density Counts" tab
tabPanel("Density Counts", icon = icon("list-alt"),
fluidRow(column(width = 4,
pickerInput(
inputId = "location_filter",
label = "Select Game Location(s)",
choices = c("Home", "Away"),
selected = c("Home", "Away"),
choicesOpt = list(style = rep(("color: black; background: white"),2)),
multiple = T)
)),
fluidRow(column(width = 12, withLoader(DT::dataTableOutput("all_teams", width = "100%"), type = "html", loader = "loader1")))),
#this sets the user interface for the "heat map" tab
tabPanel("Heatmap", icon = icon("solar-panel"),
fluidRow(column(width = 4, uiOutput("by.team")),
column(width = 8, align = "right",
actionButton("hmap", label = '', icon = icon("info-circle"), style = 'color:#e74c3c; background-color:#343E48; border-color:#343E48'),
bsTooltip("hmap", HTML("More About Heatmaps"), placement = "left", trigger = "hover", options = NULL)
)),
fluidRow(column(width = 12, withLoader(plotOutput("heatmap", width = "100%", height = '750px'),type = "html", loader = "loader1")))),
#this sets the user interface for the "Flight Routes" tab
tabPanel("Flight Routes", icon = icon("plane-departure"),
fluidRow(column(width = 12, align = "right",
actionButton("fmap", label = '', icon = icon("info-circle"), style = 'color:#e74c3c; background-color:#343E48; border-color:#343E48'),
bsTooltip("fmap", HTML("More About Flight Routes"), placement = "left", trigger = "hover", options = NULL)
)),
fluidRow(style = "height:700px;",
column(width = 12,
withLoader(
plotOutput("mapsf", width = "100%"),type = "html", loader = "loader1")))),
#This sets the user experience for the "player Loads" tab
tabPanel("Player Loads", icon = icon("charging-station"),
fluidRow(
column(width = 2, uiOutput("team.filter2")),
column(width = 2, uiOutput("month.filter")),
column(width = 2, uiOutput("week.filter")),
column(width = 2, uiOutput("metric.filter")),
#column(width = 2, uiOutput("top.rows.filter"))
column(width = 4, align = "right",
actionButton("player_loads", label = '', icon = icon("info-circle"), style = 'color:#e74c3c; background-color:#343E48; border-color:#343E48'),
bsTooltip("player_loads", HTML("About This Table"), placement = "left", trigger = "hover", options = NULL))
),
fluidRow(
column(width = 12, DT::dataTableOutput("all.player.load"))
)
)
)#tabbox
),#tabitem
# The code below relates to the "Research / Articles tab"
tabItem(
tabName = "reading",
fluidRow(style = "padding-bottom: 8px", column(width = 2,
dropdown(
#creates an input for users to filter articles by type. i.e scientific or media articles
pickerInput(
inputId = "type.reads",
label = shiny::HTML("<p><span style='color: black'>Article Type</span></p>"),
choices = articles$Type %>% unique(),
selected = c("Research", "Media / Others"),
multiple = TRUE),
tags$br(),
tags$hr(),
tags$br(),
#creates an input for users articles by year
sliderInput(
inputId = "year.reads",
label = shiny::HTML("<p><span style='color: black'>Select Year(s)</span></p>"),
sep = "",
min = min(articles$Year),
max = max(articles$Year),
step = 1,
value = c(min(articles$Year), max(articles$Year))
),
tags$br(),
tags$hr(),
tags$br(),
blockQuote(uiOutput('item'), side = "right"),
inputId = "dropdown1",
status = "info",
icon = icon("filter"), width = "300px",
tooltip = tooltipOptions(title = "Filtering Options")
)
)),#fluidrow
fluidRow(column(width = 12, withLoader(DT::dataTableOutput("reads", width = "100%"), type = "html", loader = "loader1")))
)#tabitem
),#tabitems
##################################################
#code for page loader. this is the landing page displayed while the app loads####
waiter_show_on_load(
color = "white",
div(style = "color:white;",
tags$h2("Loading...", style = "color:grey", align = "center"),
tags$img(src="waiter.gif", width="auto")
)
)
),#dashboardbody ends here
##################################################
#right side bar. This code sets user interface for the right side bar#####
#right side bar provides several inputs for users to customise different metrics related to schedule loads and density
controlbar = dashboardControlbar(
width = 300, #sets the width of the rigth side bar
skin = "dark", #sets the background color of the bar
controlbarMenu(
id = "rightmenu",
#first tab within the right side bar
controlbarItem(
id = 1,
icon = icon("plane-departure"),
title = "",
active = TRUE,
tags$h4("Travel Load Factor", style = "color:white"),
tags$hr(),
#inputs for game location load factors
tags$h4("Game Location", style = "color:#e48b38"),
sliderTextInput("home","Home Games", choices = seq(from = 1, to = 5, by = 0.5), selected = 1, grid = T),
sliderTextInput("away","Away Games", choices = seq(from = 1, to = 5, by = 0.5), selected = 2.5, grid = T),
tags$hr(),
#input for travel load factors
tags$h4("Travel", style = "color:#e48b38"),
sliderTextInput("travelyes","Game with travel", choices = seq(from = 1, to = 5, by = 0.5), selected = 2.5, grid = T),
sliderTextInput("travelno","Game without travel", choices = seq(from = 1, to = 5, by = 0.5), selected = 1, grid = T),
tags$hr(),
#inputs for direction of travel load factors
tags$h4("Direction", style = "color:#e48b38"),
sliderTextInput("east","Direction Eastbound", choices = seq(from = 1, to = 5, by = 0.5), selected = 2, grid = T),
sliderTextInput("west","Direction Westbound", choices = seq(from = 1, to = 5, by = 0.5), selected = 3, grid = T),
sliderTextInput("neutral","Within same zone", choices = seq(from = 1, to = 5, by = 0.5), selected = 1, grid = T)
),
#second tab within the right side bar
controlbarItem(
id = 2,
icon = icon("globe-americas"),
title = "",
active = FALSE,
tags$h4("Time Shift Factor", style = "color:white"),
tags$hr(),
#input for time zones corossed load factors
tags$h4("Time Zones Crossed", style = "color:#e48b38"),
sliderTextInput("0neutral","No zone change", choices = seq(from = 1, to = 5, by = 0.5), selected = 1, grid = T),
sliderTextInput("1east","1 Zone East", choices = seq(from = 1, to = 5, by = 0.5), selected = 2, grid = T),
sliderTextInput("2east","2 Zones East", choices = seq(from = 1, to = 5, by = 0.5), selected = 3, grid = T),
sliderTextInput("3east","3 Zones East", choices = seq(from = 1, to = 5, by = 0.5), selected = 4, grid = T),
sliderTextInput("1west","1 Zone West", choices = seq(from = 1, to = 5, by = 0.5), selected = 2.5, grid = T),
sliderTextInput("2west","2 Zone West", choices = seq(from = 1, to = 5, by = 0.5), selected = 3.5, grid = T),
sliderTextInput("3west","3 Zone West", choices = seq(from = 1, to = 5, by = 0.5), selected = 4.5, grid = T)
),
#third tab within the right side bar
controlbarItem(
id = 3,
icon = icon("clock"),
title = "",
active = FALSE,
tags$h4("Elapsed Days Factor", style = "color:white"),
tags$hr(),
#input for days elapsed between games
tags$h4("Days since last game.", style = "color:#e48b38"),
sliderTextInput("1day","1 Day ", choices = seq(from = 1, to = 5, by = 0.5), selected = 5, grid = T),
sliderTextInput("2day","2 Days", choices = seq(from = 1, to = 5, by = 0.5), selected = 3.5, grid = T),
sliderTextInput("3day","3 Days", choices = seq(from = 1, to = 5, by = 0.5), selected = 2, grid = T),
sliderTextInput("4day","4 Days", choices = seq(from = 1, to = 5, by = 0.5), selected = 1.5, grid = T),
sliderTextInput("5day","5+ Days", choices = seq(from = 1, to = 5, by = 0.5), selected = 1, grid = T)
),
#fourth tab within the right side bar
controlbarItem(
id = 4,
icon = icon("suitcase-rolling"),
title = "",
active = FALSE,
tags$h4("Game Density Factor", style = "color:white"),
tags$hr(),
#input for game density profiles and related load factors
tags$h4("Acummulated Games.", style = "color:#e48b38"),
#sliderTextInput("G4in5b2b","4 Games in 5 Days + B2B", choices = seq(from = 1, to = 5, by = 0.5), selected = 5, grid = T),
sliderTextInput("G3in4b2b","3 Games in 4 Days + B2B", choices = seq(from = 1, to = 5, by = 0.5), selected = 5, grid = T),
sliderTextInput("G3in4","3 Games in 4 Days", choices = seq(from = 1, to = 5, by = 0.5), selected = 4.5, grid = T),
sliderTextInput("Gb2b","Back to Back", choices = seq(from = 1, to = 5, by = 0.5), selected = 4, grid = T),
sliderTextInput("G1d","1 Rest Day", choices = seq(from = 1, to = 5, by = 0.5), selected = 3.5, grid = T),
sliderTextInput("G2d","2 Rest Days", choices = seq(from = 1, to = 5, by = 0.5), selected = 3, grid = T),
sliderTextInput("G3d","3+ Rest Days", choices = seq(from = 1, to = 5, by = 0.5), selected = 2, grid = T)
),
#fith tab within the right side bar
controlbarItem(
id = 5,
icon = icon("circle-notch"),
title = "",
active = FALSE,
tags$h4("Density Moving Average", style = "color:white"),
tags$hr(),
tags$h5("A simple moving (or rolling) average is an unweighted mean of the last n games. Use the knob below so determine the n of games to roll over.", style = "color:lightgray"),
tags$hr(),
#knob for users to select length of rolling average
fluidRow(width = "100%", align = "center",
knobInput(
inputId = "rolling",
label = "",
value = 7,
min = 1,
max = 31,
displayPrevious = T,
lineCap = "round",
fgColor = "#e48b38",
inputColor = "#e48b38",
bgColor = "black",
immediate = FALSE
)
)#fluidRow
)#right sidebar tab content
)#controlbarmenu
),#right sidebar
##################################################
#Footer. The footer bar displayed a the bottom of the dashboard###########
#this codes adds a footer to the dashboard
footer = dashboardFooter(
#footer details on the right side of the footer
right = HTML(paste(img(src = "hexlogo.png", width = "35px", height = "33px"),
tags$span("NBA Game Density APP", style = "font-family: Arial; color: grey; font-size: 16px"))),
#footer details on the left side of the footer
left = HTML(paste(icon = icon("copyright"), tags$span("2022. Created by Jose Fernandez", style = "font-family: Arial; color: grey; font-size: 16px"),
tags$span(tags$a(href= "https://twitter.com/jfernandez__", icon("twitter"))),
sep = " "))
)
)
##################################################
#Server Logic.
#This part of the app is not visible to the user, but it adds the required functionality for the
#user to be able to interact with the app
##################################################
server <- function(input, output, session) {
#this code creates an alert that is displayed when the user changes the length of the rolling average####
observeEvent(input$rolling, ignoreInit = TRUE, {
# Show a popup window when the rolling average button is changes
shinyalert(paste("Moving Index Set to last", input$rolling, "Games.", sep = " "), type = "success")
})
###################################################################################################
#Intro pop up window. This code creates a window with basic explanation about what the app does.####
observeEvent(input$welcome, {
showModal(
modalDialog(
HTML(paste(img(src = "hexlogo.png", width = "80px", height = "75px"), tags$br(), tags$span("NBA Game Density App", style = "font-family: Arial; color: white; font-size:40px"))),
tags$hr(),
tags$br(),
tags$h4("Game Density Simulator", style = "padding-left:2.3em; font-family: Arial; color: white"),
tags$img(src = "https://cdn-icons.flaticon.com/png/512/4482/premium/4482449.png?token=exp=1646901392~hmac=92c7dddf3f48fab19868e858450e2d05", width = "60px", height = "60px", align = "left"),
p("The density of the schedule is different for each team and it may be influenced by different factors, such as travel, travel distance, rest days, etc.", style = "padding-left:3em; font-family: Arial; color: white"),
tags$br(),
tags$h4("Accumulated Games", style = "padding-left:2.3em; font-family: Arial; color: white"),
tags$img(src = "https://cdn-icons.flaticon.com/png/512/2511/premium/2511651.png?token=exp=1646901566~hmac=75b5178a2b673d65674945ee8f350137", width = "60px", height = "60px", align = "left"),
p("NBA teams play various games within a week. For example, 3 games in 4 days including a Back to Back (3IN4-B2B). 3 games in 4 days (3IN4). Back to Back (B2B). 1 day rest (1). Two days rest (2). 3 or more rest days (3+)", style = "padding-left:3em; font-family: Arial; color: white"),
HTML(paste(tags$span("--------", style = "color:transparent"), tags$span("For more information on those categories visit", style = "font-family: Arial; color: white"), tags$span(tags$a(href= "https://www.nbastuffer.com/2019-2020-nba-schedule-rest-days-analysis/", "NBAstuffer.")))),
tags$br(),
tags$br(),
tags$h4("Density Index", style = "padding-left:2.3em; font-family: Arial; color: white"),
tags$img(src = "https://cdn-icons.flaticon.com/png/512/4258/premium/4258436.png?token=exp=1646901634~hmac=21d24ea7fec9d90259994b96b008e5d4", width = "60px", height = "60px", align = "left"),
p("Whe using this app, you can adjust the various factors involved in calculated density index such as game location, travel, time shift, direction of travel, density index, days since last game, etc. You can adjust how much weight you give to each of these factors on the right side menu.", style = "padding-left:3em; font-family: Arial; color: white"),
tags$br(),
tags$h4("Teams Comparison", style = "padding-left:2.3em; font-family: Arial; color: white"),
tags$img(src = "https://cdn-icons.flaticon.com/png/512/3822/premium/3822520.png?token=exp=1646901684~hmac=6353cd7d5776d3ab9d9d8c65abbe42e3", width = "60px", height = "60px", align = "left"),
p("There are various options throughout the app to compare a team density index vs the rivals.", style = "padding-left:3em; font-family: Arial; color: white"),
tags$br(),
tags$h4("Player Load", style = "padding-left:2.3em; font-family: Arial; color: white"),
tags$img(src = "https://cdn-icons.flaticon.com/png/512/3598/premium/3598270.png?token=exp=1646901750~hmac=9e957bcd824f76412d416d72b70f4ad6", width = "60px", height = "60px", align = "left"),
p("Information about individual player loads accounting for factors including rest days and minutes playes.", style = "padding-left:3em; font-family: Arial; color: white"),
tags$br(),
tags$hr(),
HTML(paste(tags$span("If you have any feedback please get in touch via", style = "font-family: Arial; color: white"), tags$span(tags$a(href= "https://twitter.com/NBAGameDensity", "Twitter.")))),
size = "l",
easyClose = TRUE)
)#showmodal
})#observeEvent
###################################################################################################
#Reactive Dataset that reponds to users interactions####
#This part of the code creates the final data set with reactive filters to allow users to decide how
#much weight to give to each type of game and the rolling window for the average
#let users filter by season
sche1 <- reactive({
sche %>%
#correction for bubble game
mutate(City = ifelse(Arena == "Disney Resort", "Orlando", City)) %>%
mutate(Location = ifelse(Arena == "Disney Resort", "Home", Location)) %>%
filter(Season == input$season_filter) %>%
mutate(Season = as.factor(Season)) %>%
group_by(Team) %>%
#converts months from numbers to actual names
mutate(Month = ifelse(Month == 1, 'January',
ifelse(Month == 2, 'February',
ifelse(Month == 3, 'March',
ifelse(Month == 4, 'April',
ifelse(Month == 5, 'May',
ifelse(Month == 6, 'June',
ifelse(Month == 7, 'July',
ifelse(Month == 8, 'August',
ifelse(Month == 9, 'September',
ifelse(Month == 10, 'October',
ifelse(Month == 11, 'November', 'December')))))))))))) %>%
select(-Time) %>%
#calculates time elapsed between last game and the previous one
mutate(elapsed = Date - lag(Date)) %>%
#simple functionality to determine if there was a travel previous to a game
mutate(Travel = ifelse(City != lag(City), "y", "n")) %>%
#identifies which which time zone (within the us) a game is held
mutate(Zone = ifelse(City == "Toronto", "Eastern",
ifelse( City == "New Orleans", "Central",
ifelse( City == "Houston", "Central",
ifelse( City == "Oklahoma", "Central",
ifelse( City == "New York", "Eastern",
ifelse( City == "Charlotte", "Eastern",
ifelse( City == "Miami", "Eastern",
ifelse( City == "Phoenix", "Mountain",
ifelse( City == "Utah", "Mountain",
ifelse( City == "Los Angeles", "Pacific",
ifelse( City == "Dallas", "Central",
ifelse( City == "Milwaukee", "Central",
ifelse( City == "Philadelphia", "Eastern",
ifelse( City == "Minnesota", "Central",
ifelse( City == "San Francisco", "Pacific",
ifelse( City == "Portland", "Pacific",
ifelse( City == "Denver", "Mountain",
ifelse( City == "Sacramento", "Pacific",
ifelse( City == "Boston", "Eastern",
ifelse( City == "Detroit", "Eastern",
ifelse( City == "Memphis", "Central",
ifelse( City == "Cleveland", "Eastern",
ifelse( City == "Chicago", "Central",
ifelse( City == "Orlando", "Eastern",
ifelse( City == "Atlanta", "Eastern",
ifelse( City == "Washington D.C.", "Eastern",
ifelse( City == "Indiana", "Eastern",
ifelse( City == "Tampa", "Eastern",
ifelse( City == "San Antonio", "Central", "other")))))))))))))))))))))))))))))) %>%
#some code to determine time changes between zones
mutate(zone.n = ifelse(Zone == "Pacific", 1,
ifelse(Zone == "Mountain", 2,
ifelse(Zone == "Central", 3,
ifelse(Zone == "Eastern", 4, 0))))) %>%
mutate(shift = zone.n - lag(zone.n)) %>%
mutate(Direction = ifelse(shift < 0, "westbound",
ifelse(shift > 0, "eastbound",
ifelse(shift == 0, "-", 100)))) %>%
#the blow code takes all the user inputs on the right side bar and calculates a game density index