-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.R
839 lines (685 loc) · 34.2 KB
/
server.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
function(input, output, session) {
# Source functions ---------------------------------------------------------
# Load data ---------------------------------------------------------
# hinkdata is my personal dataset; not included on github
data_fldr <- ifelse(dir.exists("hinkdata"), "hinkdata", "data")
# using read.csv instead of read_csv b/c there was an issue with DT (maybe related to hiding columns; or issue was with other package? can't remember)
# track IDs separately to avoid duplicating IDs
teamIDs <- read.csv(file.path(data_fldr, "TeamIDs.csv"), stringsAsFactors = FALSE)
teams <- read.csv(file.path(data_fldr, "Teams.csv"), stringsAsFactors = FALSE)
# track IDs separately to avoid duplicating IDs
playerIDs <- read.csv(file.path(data_fldr, "PlayerIDs.csv"), stringsAsFactors = FALSE)
players <- read.csv(file.path(data_fldr, "Players.csv"), stringsAsFactors = FALSE, colClasses = c("integer", "character", "character")) # specifying class important b/c an empty column will be read as logical; caused problems with paste
# rosters are the players on each team (players can be on more than one team)
rosters <- read.csv(file.path(data_fldr, "Rosters.csv"), stringsAsFactors = FALSE)
# track IDs separately to avoid duplicating IDs
gameIDs <- read.csv(file.path(data_fldr, "GameIDs.csv"), stringsAsFactors = FALSE)
games <- read.csv(file.path(data_fldr, "Games.csv"), stringsAsFactors = FALSE)
game_stats <- read.csv(file.path(data_fldr, "GameStats.csv"), stringsAsFactors = FALSE)
# Reactive values ---------------------------------------------------------
# rv <- reactiveValues(teams = teams, players = players,
# rosters = rosters, roster = NULL,
# game_id = NULL, games = games,
# game_stats = game_stats, game_stats_raw = NULL, game_stats_calc = NULL)
rv <- reactiveValues(teams = arrange(teams, desc(Season)), players = players,
rosters = rosters, roster = NULL,
game_id = NULL, games = arrange(games, desc(Date)),
game_stats = game_stats, game_stats_raw = NULL, game_stats_calc = NULL,
game_log = NULL)
# Teams ---------------------------------------------------------
## table ---------------------------------------------------------
output$teamsTable <- renderDT(
rv[["teams"]], selection = "single", style = "bootstrap", rownames = FALSE,
editable = list(target = "cell", disable = list(columns = 0)), # disable TeamID
options = list(searching = FALSE, bPaginate = FALSE, info = FALSE))
proxyTeams <- dataTableProxy("teamsTable")
## edit cell ---------------------------------------------------------
observeEvent(input$teamsTable_cell_edit, {
info <- input$teamsTable_cell_edit
i <- info$row
j <- info$col + 1L # column index offset by 1
v <- info$value
rv[["teams"]][i, j] <- coerceValue(v, rv[["teams"]][i, j])
replaceData(proxyTeams, rv[["teams"]], resetPaging = FALSE, rownames = FALSE) # important
})
## delete row ---------------------------------------------------------
observe({
toggle("delete_teams_row", condition = nrow(rv[["teams"]]) > 0 & !is.null(input$teamsTable_rows_selected))
})
observeEvent(input$delete_teams_row,{
req(input$teamsTable_rows_selected)
i <- input$teamsTable_rows_selected
rv[["rosters"]] <- rv[["rosters"]] %>%
filter(TeamID != rv[["teams"]]$TeamID[i]) # drop old roster
rv[["players"]] <- rv[["players"]] %>%
filter(PlayerID %in% rv[["rosters"]][["PlayerID"]]) # drop players not on any rosters
rv[["teams"]] <- rv[["teams"]][-i,] # needs to come last b/c rv$rosters uses rv$teams
replaceData(proxyTeams, rv[["teams"]], resetPaging = FALSE, rownames = FALSE) # important
})
## add row ---------------------------------------------------------
observeEvent(input$add_teams_row,{
#addRow() only works when server = FALSE
req(rv[["teams"]])
# update master list of team IDs
tid <- nrow(teamIDs) + 1L # ID and row number are the same
teamIDs[tid,] <<- tid
write.csv(teamIDs, file.path(data_fldr, "TeamIDs.csv"), row.names = FALSE)
# update master list of player IDs
pid <- nrow(playerIDs) + 1L # ID and row number are the same
playerIDs[pid,] <<- pid
write.csv(playerIDs, file.path(data_fldr, "PlayerIDs.csv"), row.names = FALSE)
# update all of the relevant tables
ti <- nrow(rv[["teams"]]) + 1L
rv[["teams"]][ti,] <- list(tid, "", "", "")
ri <- nrow(rv[["rosters"]]) + 1L
rv[["rosters"]][ri,] <- list(tid, pid, NA_integer_)
pi <- nrow(rv[["players"]]) + 1L
rv[["players"]][pi,] <- list(pid, "", "")
replaceData(proxyTeams, rv[["teams"]], resetPaging = FALSE, rownames = FALSE) # important
})
# Roster ---------------------------------------------------------
## table ---------------------------------------------------------
output$rosterTable <- renderDT(
rv[["roster"]], selection = "single", style = "bootstrap", rownames = FALSE,
editable = list(target = "cell", disable = list(columns = 1)), # disable PlayerID column
options = list(searching = FALSE, bPaginate = FALSE, info = FALSE,
columnDefs = list(list(visible = FALSE, targets = 0)))) # hide TeamID column
proxyRoster <- dataTableProxy("rosterTable")
observe({
toggle("select_row_msg", condition = is.null(input$teamsTable_rows_selected))
})
# rv[["roster"]] is first assigned (and updated) when a row is selected in teamsTable
observeEvent(input$teamsTable_rows_selected,{
req(input$teamsTable_rows_selected)
ti <- rv[["teams"]]$TeamID[input$teamsTable_rows_selected]
rv[["roster"]] <- filter(rv[["rosters"]], TeamID == ti) %>%
left_join(rv[["players"]], by = "PlayerID")
replaceData(proxyRoster, rv[["roster"]], resetPaging = FALSE, rownames = FALSE) # important
})
## dynamic UI ---------------------------------------------------------
observe({
req(rv[["roster"]])
toggle("rosterTable", condition = !is.null(input$teamsTable_rows_selected))
toggle("add_roster_row", condition = !is.null(input$teamsTable_rows_selected))
toggle("delete_roster_row", condition = nrow(rv[["roster"]]) > 0 & !is.null(input$rosterTable_rows_selected))
})
## edit cell ---------------------------------------------------------
observeEvent(input$rosterTable_cell_edit, {
info <- input$rosterTable_cell_edit
i <- info$row
j <- info$col + 1L # column index offset by 1
v <- info$value
# get IDs for row where change was made
tid <- rv[["roster"]][["TeamID"]][i]
pid <- rv[["roster"]][["PlayerID"]][i]
# find indices
ri <- which(rv[["rosters"]][["TeamID"]] == tid & rv[["rosters"]][["PlayerID"]] == pid)
pi <- which(rv[["players"]][["PlayerID"]] == pid)
# find colunm name
cn <- names(rv[["roster"]])[j]
# update values
if (cn == "Number"){
rv[["rosters"]][ri, cn] <- coerceValue(v, rv[["rosters"]][ri, cn])
}else{
rv[["players"]][pi, cn] <- coerceValue(v, rv[["players"]][pi, cn])
}
rv[["roster"]] <- rv[["rosters"]] %>% # rebuild rv$roster
filter(TeamID == tid) %>%
left_join(rv[["players"]], by = "PlayerID")
replaceData(proxyRoster, rv[["roster"]], resetPaging = FALSE, rownames = FALSE) # important
})
## add row ---------------------------------------------------------
observeEvent(input$add_roster_row,{
#addRow() only works when server = FALSE
req(rv[["roster"]])
# update master list of player IDs
pid <- nrow(playerIDs) + 1L # ID and row number are the same
playerIDs[pid,] <<- pid
write.csv(playerIDs, file.path(data_fldr, "PlayerIDs.csv"), row.names = FALSE)
tid <- rv[["roster"]]$TeamID[1] # all rows in rv[["roster"]] have same TeamID
ri <- nrow(rv[["rosters"]]) + 1L
rv[["rosters"]][ri,] <- list(tid, pid, NA_integer_)
pi <- nrow(rv[["players"]]) + 1L
rv[["players"]][pi,] <- list(pid, "", "")
rv[["roster"]] <- rv[["rosters"]] %>% # rebuild rv$roster; another heavy handed approach
filter(TeamID == tid) %>%
left_join(rv[["players"]], by = "PlayerID")
replaceData(proxyRoster, rv[["roster"]], resetPaging = FALSE, rownames = FALSE) # important
})
## delete row ---------------------------------------------------------
observeEvent(input$delete_roster_row,{
req(input$rosterTable_rows_selected)
i <- input$rosterTable_rows_selected
rv[["roster"]] <- rv[["roster"]][-i,]
rv[["rosters"]] <- rv[["rosters"]] %>%
filter(TeamID != rv[["roster"]]$TeamID[1]) %>% # drop old roster from table of all rosters (heavy handed approach)
bind_rows(select(rv[["roster"]], TeamID, PlayerID, Number)) # replace it with appropriate columns from new roster
rv[["players"]] <- rv[["players"]] %>%
filter(PlayerID %in% rv[["rosters"]][["PlayerID"]]) # drop players not on any rosters
replaceData(proxyRoster, rv[["roster"]], resetPaging = FALSE, rownames = FALSE) # important
})
## select from previous entered players ---------------------------------------------------------
output$previousPlayers <- renderUI({
req(rv[["roster"]], rv[["players"]])
sel_ids <- rv[["roster"]][["PlayerID"]]
all_ids <- rv[["players"]][["PlayerID"]]
ids <- all_ids[!(all_ids %in% sel_ids)] # find PlayerIDs that haven't been added to roster
req(length(ids) > 0) # at least one player that could be selected
d <- rv[["players"]] %>%
filter(PlayerID %in% ids) %>%
mutate(PlayerName = first_last(FirstName, LastName)) %>%
arrange(FirstName)
picker.ids <- d[["PlayerID"]]
names(picker.ids) <- d[["PlayerName"]]
pickerInput("selected_players", "Select previous players",
choices = picker.ids, multiple = TRUE,
options = list(`live-search` = TRUE))
})
observe({
toggle("add_selected_players", condition = !is.null(input$selected_players))
})
observeEvent(input$add_selected_players,{
req(rv[["roster"]], rv[["players"]]) # probably not necessary b/c handled upstream
tid <- rv[["roster"]]$TeamID[1] # all rows in rv[["roster"]] have same TeamID
rv[["rosters"]] <- bind_rows(rv[["rosters"]],
data.frame(TeamID = tid,
PlayerID = as.integer(input$selected_players),
Number = NA_integer_,
stringsAsFactors = FALSE))
rv[["roster"]] <- rv[["rosters"]] %>% # rebuild rv$roster; another heavy handed approach
filter(TeamID == tid) %>%
left_join(rv[["players"]], by = "PlayerID")
replaceData(proxyRoster, rv[["roster"]], resetPaging = FALSE, rownames = FALSE) # important
})
## show/hide save button ---------------------------------------------------------
observe({
input$save_teams_roster_changes # take dependency on save button to hide button after saving
cond <- (isTRUE(all.equal(players, rv[["players"]])) | isTRUE(all.equal(rosters, rv[["rosters"]])) | isTRUE(all.equal(teams, rv[["teams"]])))
toggle("set_roster", condition = cond & !is.null(input$teamsTable_rows_selected))
# toggle("exportRosters", condition = cond)
toggle("save_teams_roster_changes", condition = !isTRUE(all.equal(players, rv[["players"]])) | !isTRUE(all.equal(rosters, rv[["rosters"]])) | !isTRUE(all.equal(teams, rv[["teams"]])))
})
observeEvent(input$save_teams_roster_changes,{
# write teams, rosters, & players from memory to disk
write.csv(rv[["teams"]], file.path(data_fldr, "Teams.csv"), row.names = FALSE)
write.csv(rv[["rosters"]], file.path(data_fldr, "Rosters.csv"), row.names = FALSE)
write.csv(rv[["players"]], file.path(data_fldr, "Players.csv"), row.names = FALSE)
# update non-reactive versions to keep track of changes
teams <<- rv[["teams"]]
rosters <<- rv[["rosters"]]
players <<- rv[["players"]]
})
## export rosters ---------------------------------------------------------
# rosterFile <- reactive({
# left_join(rv[["teams"]], rv[["rosters"]], by = "TeamID")
# })
#
# output$exportRosters <- downloadHandler(
# filename = function() { "Rosters.csv"},
# content = function(file) {
# write.csv(rosterFile(), file, row.names = FALSE)
# }
# )
# Scorekeeper ---------------------------------------------------------
## set roster ---------------------------------------------------------
observeEvent(input$tabs, {
if (input$tabs == "scorekeeper" & input$set_roster == 0){
sendSweetAlert(
session = session,
title = "Warning: Need to set roster before scoring a game",
text = NULL,
type = "warning"
)
}
})
observeEvent(input$set_roster,{
# update master list of game IDs; always starting a new game when setting a roster (unless testing app; other times?)
gid <- nrow(gameIDs) + 1L # ID and row number are the same
gameIDs[gid,] <<- gid
rv[["game_id"]] <- gid
write.csv(gameIDs, file.path(data_fldr, "GameIDs.csv"), row.names = FALSE)
# setting roster initializes a new game
rv[["game_stats_raw"]] <- rv[["roster"]] %>%
mutate(GameID = gid,
FirstNum = first_num(FirstName, Number),
FTM = 0L,
FTA = 0L,
FGM2 = 0L,
FGA2 = 0L,
FGM3 = 0L,
FGA3 = 0L,
TOV = 0L,
STL = 0L,
DREB = 0L,
OREB = 0L,
BLK = 0L,
AST = 0L,
PF = 0L)
updateTabItems(session, "tabs", "scorekeeper") # move to scorekeeper tab after setting roster
})
## game info ---------------------------------------------------------
gameInfo <- reactive({
data.frame(GameID = rv[["game_id"]],
TeamID = rv[["roster"]]$TeamID[1], # same TeamID for all rows in roster
Date = as.character(input$game_date),
Opponent = input$opponent_name,
TeamScore = as.numeric(input$team_score),
OpponentScore = as.numeric(input$opp_score),
stringsAsFactors = FALSE)
})
gameInfoText <- reactive({
c(paste0("GameID: ", rv[["game_id"]], "; TeamID: ", rv[["roster"]]$TeamID[1],
"; Date: ", as.character(input$game_date), "; Opponent: ", input$opponent_name),
"---------------------------------------------------------------------")
})
observe({
req(rv[["game_id"]])
input$save_game_info # take dependency on save button
toggle("save_game_info", condition = !isTRUE(all.equal(filter(rv[["games"]], GameID == rv[["game_id"]]),
gameInfo())))
})
observeEvent(input$save_game_info,{
rv[["games"]] <- filter(rv[["games"]], GameID != rv[["game_id"]]) # drop old game info
tid <- rv[["roster"]]$TeamID[1] # same TeamID for all rows in roster
gi <- nrow(rv[["games"]]) + 1L
rv[["games"]][gi,] <- gameInfo()
write.csv(rv[["games"]], file.path(data_fldr, "Games.csv"), row.names = FALSE) # overwrites previous file
# if game info changes, then top part of game log also changed (so rewriting whole file)
cat(c(gameInfoText(), rv[["game_log"]]), file = file.path("gamelogs", paste0("GameID_", rv[["game_id"]], ".txt")), sep = "\n")
})
## player info ---------------------------------------------------------
firstNum <- reactive({
req(rv[["roster"]], input$set_roster > 0)
d <- rv[["roster"]] %>%
mutate(FirstNum = first_num(FirstName, Number ))
unique(d$FirstNum)
})
output$selectedPlayer <- renderUI({
radioGroupButtons(inputId = "selected_player", choices = firstNum(), size = "normal",
justified = TRUE, direction = "vertical")
})
output$DNP <- renderUI({
# did not play
pickerInput(inputId = "dnp", label = "Did Not Play (DNP)", choices = firstNum(), multiple = TRUE)
})
## save game stats ---------------------------------------------------------
observe({
toggle("undo", condition = !is.null(rv[["game_stats_raw"]]))
})
gameStatsCalcSelected <- reactive({
# this was how I initially solved this problem (i.e., after deciding not to save calculated columns)
# wouldn't have to drop as many columns from rv[["game_stats_raw"]] but that dataframe doesn't include DNP column
select(rv[["game_stats_calc"]], -FirstName, -LastName, -Number, -FirstNum, -`FT%`, -`FG%`, -`3PT%`, -`TS%`, -EFF) # drop columns contained in other tables (e.g., names and numbers) and non-counting stats, i.e., stats that can't be summarized with sum
})
observe({
req(rv[["game_stats_calc"]])
toggle("save_game_stats", condition = !isTRUE(all.equal(gameStatsCalcSelected(), filter(rv[["game_stats"]], GameID == rv[["game_id"]]))))
})
observeEvent(input$save_game_stats,{
rv[["game_stats"]] <- filter(rv[["game_stats"]], GameID != rv[["game_id"]]) # drop old game stats
rv[["game_stats"]] <- bind_rows(rv[["game_stats"]], gameStatsCalcSelected()) # add new game stats
write.csv(rv[["game_stats"]], file.path(data_fldr, "GameStats.csv"), row.names = FALSE)
# if game stats change, then game log also changed (not accounting for situation where game log is changing separately from the stats)
cat(c(gameInfoText(), rv[["game_log"]]), file = file.path("gamelogs", paste0("GameID_", rv[["game_id"]], ".txt")), sep = "\n")
})
## update stats ---------------------------------------------------------
# values are incremented or decremented based on whether undo is switched on
change <- reactive({
ifelse(input$undo, -1L, 1L)
})
rowIndex <- reactive({
which(rv[["game_stats_raw"]]$FirstNum == input$selected_player)
})
output$gameLogLastTitle <- renderText({
out = ""
if (!is.null(rv[["game_log"]])) out = "Last game log entry:"
out
})
output$gameLogLastText <- renderText({
req(rv[["game_log"]])
rv[["game_log"]][length(rv[["game_log"]])]
})
## tried this approach but yielded "Error in : promise already under evaluation: recursive default argument reference or earlier problems?"
## leads to redundant code below
# updateGameStats <- function(data = rv[["game_stats_raw"]], ri = rowIndex(), change = change(), columns){
# for (i in columns){
# ci = which(names(data) == i)
# data[ri,ci] = data[ri,ci] + change
# }
# return(data)
# }
### free throws ---------------------------------------------------------
observeEvent(input$miss_1,{
# need the redundancy of calling log_action in every observeEvent so that the logged action is not too reactive (e.g., changing with undo or player)
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Missed free throw by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "FTA")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
observeEvent(input$make_1,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Made free throw by ", input$selected_player))
columns <- c("FTM", "FTA")
ri <- rowIndex()
for (i in columns){
ci <- which(names(rv[["game_stats_raw"]]) == i)
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
}
})
### field goals ---------------------------------------------------------
observeEvent(input$miss_2,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Missed field goal by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "FGA2")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
observeEvent(input$make_2,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Made field goal by ", input$selected_player))
columns <- c("FGM2", "FGA2")
ri <- rowIndex()
for (i in columns){
ci <- which(names(rv[["game_stats_raw"]]) == i)
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
}
})
### three pointers ---------------------------------------------------------
observeEvent(input$miss_3,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Missed 3-pt field goal by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "FGA3")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
observeEvent(input$make_3,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Made 3-pt field goal by ", input$selected_player))
columns <- c("FGM3", "FGA3")
ri <- rowIndex()
for (i in columns){
ci <- which(names(rv[["game_stats_raw"]]) == i)
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
}
})
### steals and turnovers ---------------------------------------------------------
observeEvent(input$stl,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Steal by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "STL")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
observeEvent(input$tov,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Turnover by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "TOV")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
### rebounds ---------------------------------------------------------
observeEvent(input$dreb,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Defensive rebound by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "DREB")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
observeEvent(input$oreb,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Offensive rebound by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "OREB")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
### blocks and assists ---------------------------------------------------------
observeEvent(input$blk,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Block by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "BLK")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
observeEvent(input$ast,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Assist by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "AST")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
### fouls ---------------------------------------------------------
observeEvent(input$pf,{
rv[["game_log"]] <- c(rv[["game_log"]], log_action(input$undo, "Foul by ", input$selected_player))
ri <- rowIndex()
ci <- which(names(rv[["game_stats_raw"]]) == "PF")
rv[["game_stats_raw"]][ri,ci] <- rv[["game_stats_raw"]][ri,ci] + change()
})
## calculate stats ---------------------------------------------------------
# watch for changes to rv[["game_stats_raw"]] (and input$dnp) to update calculated columns
observe({
req(rv[["game_stats_raw"]])
temp <- rv[["game_stats_raw"]] %>%
mutate(PTS = FTM + FGM2 * 2 + FGM3 * 3,
REB = OREB + DREB,
`FT%` = round(FTM/FTA*100),
FGM = FGM2 + FGM3,
FGA = FGA2 + FGA3,
`FG%` = round(FGM/FGA*100),
`3PT%` = round(FGM3/FGA3*100),
`TS%` = round(true_shooting(PTS, FTA, FGA)),
EFF = efficiency(PTS, REB, AST, STL, BLK, FGA, FGM, FTA, FTM, TOV))
if (is.null(input$dnp)){ # if dnp is null, then no players selected
rv[["game_stats_calc"]] <- temp %>% mutate(DNP = 0)
}else{
rv[["game_stats_calc"]] <- temp %>%
mutate(DNP = ifelse(FirstNum %in% input$dnp, 1, 0))
}
})
## display stats ---------------------------------------------------------
rowIndexGSC <- reactive({
which(rv[["game_stats_calc"]][["FirstNum"]] == input$selected_player)
})
output$pts <- renderValueBox({
req(rv[["game_stats_calc"]])
ri <- rowIndexGSC()
valueBox(value = rv[["game_stats_calc"]][["PTS"]][ri], subtitle = "Points", color = "light-blue")
})
output$ft <- renderValueBox({
req(rv[["game_stats_calc"]])
out <- display_shooting(data = rv[["game_stats_calc"]], ri = rowIndexGSC(), stat = "FT%", ma = c("FTM", "FTA"))
valueBox(value = out[1], subtitle = out[2], color = "light-blue")
})
output$fg <- renderValueBox({
req(rv[["game_stats_calc"]])
out <- display_shooting(data = rv[["game_stats_calc"]], ri = rowIndexGSC(), stat = "FG%", ma = c("FGM", "FGA"))
valueBox(value = out[1], subtitle = out[2], color = "light-blue")
})
output$fg3 <- renderValueBox({
req(rv[["game_stats_calc"]])
out <- display_shooting(data = rv[["game_stats_calc"]], ri = rowIndexGSC(), stat = "3PT%", ma = c("FGM3", "FGA3"))
valueBox(value = out[1], subtitle = out[2], color = "light-blue")
})
output$ts <- renderValueBox({
req(rv[["game_stats_calc"]])
out <- display_shooting(data = rv[["game_stats_calc"]], ri = rowIndexGSC(), stat = "TS%")
valueBox(value = out[1], subtitle = out[2], color = "light-blue")
})
output$eff <- renderValueBox({
req(rv[["game_stats_calc"]])
ri <- rowIndexGSC()
valueBox(value = rv[["game_stats_calc"]][["EFF"]][ri], subtitle = "Efficiency", color = "light-blue")
})
output$reb <- renderValueBox({
req(rv[["game_stats_calc"]])
ri <- rowIndexGSC()
rvs <- rv[["game_stats_calc"]][ri,]
valueBox(value = rvs[["REB"]], subtitle = paste0("Rebounds (", rvs[["DREB"]], "+", rvs[["OREB"]], ")"), color = "light-blue")
})
output$blk <- renderValueBox({
req(rv[["game_stats_calc"]])
ri <- rowIndexGSC()
valueBox(value = rv[["game_stats_calc"]][["BLK"]][ri], subtitle = "Blocks", color = "light-blue")
})
output$stl <- renderValueBox({
req(rv[["game_stats_calc"]])
ri <- rowIndexGSC()
valueBox(value = rv[["game_stats_calc"]][["STL"]][ri], subtitle = "Steals", color = "light-blue")
})
output$ast <- renderValueBox({
req(rv[["game_stats_calc"]])
ri <- rowIndexGSC()
valueBox(value = rv[["game_stats_calc"]][["AST"]][ri], subtitle = "Assists", color = "light-blue")
})
output$tov <- renderValueBox({
req(rv[["game_stats_calc"]])
ri <- rowIndexGSC()
valueBox(value = rv[["game_stats_calc"]][["TOV"]][ri], subtitle = "Turnovers", color = "light-blue")
})
output$pf <- renderValueBox({
req(rv[["game_stats_calc"]])
ri <- rowIndexGSC()
valueBox(value = rv[["game_stats_calc"]][["PF"]][ri], subtitle = "Fouls", color = "light-blue")
})
# Stats Viewer ---------------------------------------------------------
observe({
cond <- is.null(input$teamsTableStatsViewer_rows_selected)
toggle("select_teams_row_msg", condition = cond)
toggle("games_selectall", condition = !cond)
toggle("games_deselectall", condition = !cond)
# toggle("select_games_row_msg", condition = is.null(input$teamsTableStatsViewer_rows_selected) | is.null(input$gamesTable_rows_selected))
})
observe({
cond <- !is.null(input$teamsTableStatsViewer_rows_selected) & !is.null(input$gamesTable_rows_selected)
toggle("stats_type", condition = cond)
toggle("stats_group_by", condition = cond)
})
output$statisticsMessage <- renderText({
out <- ""
if (is.null(input$teamsTableStatsViewer_rows_selected) & is.null(input$gamesTable_rows_selected)) out <- "Select row(s) in Teams and Games tables above to view statistics"
if (is.null(input$gamesTable_rows_selected)) out <- "Select row(s) in Games table above to view statistics"
out
})
output$teamsTableStatsViewer <- renderDT(
rv[["teams"]], selection = "multiple",
style = "bootstrap", rownames = FALSE,
options = list(pageLength = 5, bLengthChange = FALSE, bPaginate = TRUE, searching = FALSE, scrollX = TRUE,
columnDefs = list(list(visible = FALSE, targets = 0)))) # hide TeamID column
proxyTeamsTableStatsViewer <- dataTableProxy("teamsTableStatsViewer")
observeEvent(input$teams_selectall, {
proxyTeamsTableStatsViewer %>% selectRows(1:nrow(rv[["teams"]]))
})
observeEvent(input$teams_deselectall, {
proxyTeamsTableStatsViewer %>% selectRows(NULL)
})
gamesDisplay <- reactive({
req(input$teamsTableStatsViewer_rows_selected)
ti <- rv[["teams"]]$TeamID[input$teamsTableStatsViewer_rows_selected]
filter(rv[["games"]], TeamID %in% ti) %>%
mutate(Margin = TeamScore - OpponentScore)
})
output$gamesTable <- renderDT(
gamesDisplay(), selection = "multiple",
style = "bootstrap", rownames = FALSE,
options = list(pageLength = 5, bLengthChange = FALSE, bPaginate = TRUE, searching = FALSE, scrollX = TRUE,
columnDefs = list(list(visible = FALSE, targets = c(0,1))))) # hide GameID and TeamID columns
proxyGamesTable <- dataTableProxy("gamesTable")
observeEvent(input$games_selectall, {
proxyGamesTable %>% selectRows(1:nrow(gamesDisplay()))
})
observeEvent(input$games_deselectall, {
proxyGamesTable %>% selectRows(NULL)
})
playersDisplay <- reactive({
req(input$teamsTableStatsViewer_rows_selected)
ti <- rv[["teams"]]$TeamID[input$teamsTableStatsViewer_rows_selected]
left_join(rv[["rosters"]], rv[["players"]], by = "PlayerID") %>% filter(TeamID %in% ti)
})
output$selectedPlayers <- renderUI({
req(!is.null(input$gamesTable_rows_selected) & nrow(playersDisplay()) > 0 & "PlayerID" %in% input$stats_group_by)
d <- playersDisplay() %>%
select(PlayerID, FirstName, LastName) %>%
unique() %>% # same player could be on more than one team
mutate(PlayerName = first_last(FirstName, LastName)) %>%
filter(FirstName != "Opponent") %>%
arrange(FirstName)
picker.ids <- d[["PlayerID"]]
names(picker.ids) <- d[["PlayerName"]]
pickerInput("selected_players_stats", "Select players", choices = picker.ids, multiple = TRUE, selected = picker.ids,
options = list(`actions-box` = TRUE))
})
gameStats <- reactive({
req(input$teamsTableStatsViewer_rows_selected, input$gamesTable_rows_selected)
rv[["game_stats"]] %>%
left_join(select(rv[["teams"]], TeamID, Season, Team)) %>%
left_join(select(rv[["games"]], GameID, Opponent)) %>%
left_join(select(rv[["players"]], PlayerID, FirstName)) %>%
# Create Team to represent Opponent (which was recorded as the FirstName on a roster)
mutate(Team = ifelse(FirstName == "Opponent", Opponent, Team),
Team = paste0(Team, " (", Season, ")"))
})
gameStatsSub <- reactive({
# req(input$teamsTableStatsViewer_rows_selected, input$gamesTable_rows_selected)
ti <- gamesDisplay()$TeamID[input$gamesTable_rows_selected]
gi <- gamesDisplay()$GameID[input$gamesTable_rows_selected]
out = filter(gameStats(), TeamID %in% ti & GameID %in% gi)
if ("PlayerID" %in% input$stats_group_by) out = filter(out, PlayerID %in% input$selected_players_stats)
out
})
gameStatsSumm <- reactive({
gs = gameStatsSub()
gms <- gs %>%
group_by_at(input$stats_group_by) %>%
summarise(Games = length(unique(GameID)))
d <- gs %>%
group_by_at(input$stats_group_by) %>%
summarise(across(all_of(stats_cols), ~sum(., na.rm = TRUE))) %>%
left_join(gms) %>%
mutate(GP = Games - DNP)
if (input$stats_type == "Per game"){
div = if("PlayerID" %in% input$stats_group_by) "GP" else "Games"
d = mutate(d, across(all_of(stats_cols), ~round(. / .data[[div]], 2)))
}
d
})
statisticsDisplay <- reactive({
req(input$stats_group_by)
# probably a fancier way to do this, but starting with brute force
grps <- input$stats_group_by
d <- gameStatsSumm() %>%
ungroup() %>%
mutate(FGM = FGM2 + FGM3,
FGA = FGA2 + FGA3,
`FT%` = round(FTM/FTA*100),
`FG%` = round(FGM/FGA*100),
`3P%` = round(FGM3/FGA3*100),
`TS%` = round(true_shooting(PTS, FTA, FGA)))
if ("GameID" %in% grps){
d <- left_join(d, rv[["games"]])
}
if ("PlayerID" %in% grps){
d <- d %>%
left_join(rv[["players"]]) %>%
mutate(Player = FirstName,
EFF = round(efficiency(PTS, REB, AST, STL, BLK, FGA, FGM, FTA, FTM, TOV)))
}
if (all(c("Team", "GameID", "PlayerID") %in% grps)){
d <- select(d, c("Team", "Date", "Opponent", "Player", "GP", stats_display_cols, "EFF"))
}
if (!("Team" %in% grps) && "GameID" %in% grps && "PlayerID" %in% grps){
d <- select(d, c("Date", "Opponent", "Player", "GP", stats_display_cols, "EFF"))
}
if ("Team" %in% grps && "GameID" %in% grps && !("PlayerID" %in% grps)){
d <- select(d, c("Team", "Date", "Opponent", stats_display_cols))
}
if (!("Team" %in% grps) && "GameID" %in% grps && !("PlayerID" %in% grps)){
d <- select(d, c("Date", "Opponent", stats_display_cols))
}
if ("Team" %in% grps && !("GameID" %in% grps) && !("PlayerID" %in% grps)){
d <- select(d, c("Team", "Games", stats_display_cols))
if (input$stats_type == "Per game") d <- select(d, -Games)
}
if (!("Team" %in% grps) && !("GameID" %in% grps) && "PlayerID" %in% grps){
d <- select(d, c("Player", "GP", stats_display_cols, "EFF"))
if (input$stats_type == "Per game") d <- select(d, -GP)
}
if ("Team" %in% grps && !("GameID" %in% grps) && "PlayerID" %in% grps){
d <- select(d, c("Team", "Player", "GP", stats_display_cols, "EFF"))
if (input$stats_type == "Per game") d <- select(d, -GP)
}
d
})
output$statisticsTable <- renderDT(
statisticsDisplay(), selection = "single",
style = "bootstrap", rownames = FALSE,
options = list(searching = FALSE, bPaginate = FALSE, info = FALSE, scrollX = TRUE,
columnDefs = list(list(orderSequence = c('desc', 'asc'), targets = "_all")))) # change table to sort descending on first click on column heading
}