Repository navigation
Expand file tree
/
Copy pathserver.R
More file actions
272 lines (228 loc) · 7.96 KB
/
Copy pathserver.R
File metadata and controls
272 lines (228 loc) · 7.96 KB
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
# Run scoring in Shiny -- server
#
# written by Martin Monkman
# last update: 2019-02-18
#
# Copyright 2019 Martin Monkman
# license: MIT https://opensource.org/licenses/MIT
#
#
#
# ####################
#
# package load
library(shiny)
# tidyverse
library(dplyr)
library(tidyr)
library(ggplot2)
# and beyond
library(feather)
#
# turn off error messages
options(show.error.messages=F)
#
Teams.merge <- read_feather("Teams_merge.feather")
MLB_RPG <- read_feather("MLB_RPG.feather")
LG_RPG <- read_feather("LG_RPG.feather")
# create a list of the team names
TeamNames <- Teams.merge %>%
filter(yearID > 1900, lgID != "FL") %>%
distinct(franchID) %>%
select(franchID) %>%
arrange(franchID)
TeamNameList <- TeamNames$franchID
#
# define the year limits of the data set
firstyear <- MLB_RPG$yearID[1]
mostrecentyear <- tail(MLB_RPG$yearID, 1)
#
# ####################################################
#
# Define server logic
shinyServer(function(input, output) {
# turn off error messages
options(show.error.messages=F)
#
# LEAGUE PAGE
# -----------
# UI reactive elements for league ui page
# trendline select (TRUE / FALSE)
output$trendlineselectvalue <- renderPrint({ input$trendlineselect })
# trendline sensitivity (slider)
output$trendline_sen <- renderPrint({ input$trendline_sen_sel })
# trendline confidence interval (slider)
output$trendline_conf <- renderPrint({ input$trendline_conf_sel })
# define yearrange for UI
output$lg_yearrange <- renderUI({
sliderInput("lg_yearrange_input", label = h4("Select year range to plot"),
min = firstyear, max = mostrecentyear, value = c(firstyear, mostrecentyear),
step = 1, round = TRUE, sep = "",
animate = TRUE)
})
#
#
#
# +++++ plot: runs scored per game by league
output$plot_MLBtrend <- renderPlot({
# plot the data
MLBRPG <- ggplot(MLB_RPG, aes(x=yearID, y=leagueRPG)) +
geom_point() +
xlim(input$lg_yearrange_input[1], input$lg_yearrange_input[2]) +
ylim(3, 6) +
ggtitle(paste("Major League Baseball: runs per team per game",
input$lg_yearrange_input[1], "-", input$lg_yearrange_input[2])) +
xlab("year") + ylab("runs per team per game")
# plot each league separately?
if (input$leaguesplitselect == TRUE) {
MLBRPG <- ggplot(LG_RPG, aes(x=yearID, y=leagueRPG)) +
geom_point() +
xlim(input$lg_yearrange_input[1], input$lg_yearrange_input[2]) +
ylim(3, 6) +
ggtitle(paste("Major League Baseball: runs per team per game",
input$lg_yearrange_input[1], "-", input$lg_yearrange_input[2])) +
xlab("year") + ylab("runs per game") +
facet_grid(lgID ~ .)
}
# add trend line to plot?
if (input$trendlineselect == TRUE) {
MLBRPG <- MLBRPG +
stat_smooth(method='loess',
span=as.numeric(input$trendline_sen_sel),
level=as.numeric(input$trendline_conf_sel))
}
# final plot
MLBRPG
})
# ----------- end MLBtrend plot
#
# MLB table output
# (if leagues are plotted separately, then show the data split by league)
output$MLBtable <- renderDataTable({
if (input$leaguesplitselect == TRUE) {
RPG_data <- as.data.frame(LG_RPG)
} else {
RPG_data <- as.data.frame(MLB_RPG)
}
})
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
# TEAM PAGE
#
# UI reactive elements for team ui page
#
# list of team names for drop down box
output$TeamNameListUI <- renderUI ({
selectInput("TeamName", label=h4("Choose team to plot:"),
choices=as.character(TeamNameList),
selected=TeamNameList[1])
})
# trendline select (TRUE / FALSE)
output$team_trendlineselectvalue <- renderPrint({ input$team_trendlineselect })
# trendline sensitivity (slider)
output$team_trendline_sen <- renderPrint({ input$team_trendline_sen_sel })
# trendline confidence interval (slider)
output$team_trendline_conf <- renderPrint({ input$team_trendline_conf_sel })
# filter by team using franchID variable in the Teams.merge data frame
# note the use of double "=" i.e. ==
#output$team_yearrange <- renderUI({
# sliderInput("team_yearrange_input", label = h4("Select year range to plot"),
# min = firstteamyear, max = lastteamyear, value = c(firstteamyear, lastteamyear),
# step = 1, round = TRUE, format = "####",
# animate = TRUE)
#})
#
# +++++ PLOTS: TEAM RUNS SCORED / ALLOWED PER GAME
output$plot_teamTrend <- renderPlot({
#
req(input$TeamName)
#
Team1 <- filter(Teams.merge, franchID == input$TeamName)
#
# then pull first and last years of the analysis and team name fields
firstteamyear <- Team1$yearID[1]
lastteamyear <- tail(Team1$yearID, 1)
#
Team2 <- Team1 %>%
select(yearID, teamID, R_index, RA_index) %>%
tidyr::gather(c("R_index", "RA_index"), key = "variable", value = "value")
#
output$team_yearrange <- renderUI({
sliderInput("team_yearrange_input", label = h4("Select year range to plot"),
min = firstteamyear, max = lastteamyear, value = c(firstteamyear, lastteamyear),
step = 1, round = TRUE, sep = "",
animate = TRUE)
})
# pull the team name
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
## NOTE: THIS IS THE ULTIMATE CODE BUT DUE TO AN ERROR IN THE SOURCE LAHMAN DATABASE
## (THAT GOT TRANSFERED INTO THE LAHMAN PACKAGE)
## THE ALTERNATE CODE TO DEFINE THE TEAM NAME IS USED TEMPORARILY
## AND SHOULD BE CORRECT WITH LAHMAN 4.0
#team.name <- tail(Team1$name, 1)
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
team.name <- tail(Team1$name, 1)
# now put the team name, years, and the rest of the title text into a single string
titletext <- paste(team.name, firstteamyear, "-", lastteamyear,
"\nRuns scored relative to the league average")
#
# plot the data
teamTrend <- ggplot(Team2, aes(x=yearID, y=value, colour=variable)) +
geom_point() +
# xlim(input$team_yearrange_input[1], input$team_yearrange_input[2]) +
# ylim(50, 150) +
ggtitle(titletext) +
xlab("year") + ylab("Index (league average = 100)") +
annotate("segment", x=firstteamyear, xend=lastteamyear, y=100, yend=100, colour="black", size=1)
# add trend line to plot?
if (input$team_trendlineselect == TRUE) {
teamTrend <- teamTrend +
stat_smooth(method='loess',
size=1.5,
span=input$team_trendline_sen_sel,
se=FALSE)
}
# final plot
teamTrend
})
# ----------- end team trend plot
output$Team1_data_table <- renderDataTable({
#
#
Team1 <- filter(Teams.merge, franchID == input$TeamName)
# the raw data table
# Team1_data_table <- as.data.frame(Team1)
#
# the data table, with only the interesting fields (columns)
#
Team1_data_table <- Team1 %>%
select(yearID, lgID, franchID, name, W, L, WLpct, R.x, RA.x, teamRPG, leagueRPG, R_index,
teamRAPG, leagueRAPG, RA_index)
})
# -- TEAM DATA TABLE
output$team_data_table <- renderDataTable({
#
# the raw data table
# team_data_table <- as.data.frame(Teams.merge)
#
# NOTE: moved to pre-processing
# the data table, with only the interesting fields (columns)
# select(yearID, lgID, franchID, name, W, L, WLpct, R.x, RA.x, teamRPG, leagueRPG, R_index,
# teamRAPG, leagueRAPG, RA_index)
team_data_table <- Teams.merge
if (input$tableyearselect != "All"){
team_data_table <- team_data_table[team_data_table$yearID == input$tableyearselect,]
}
if (input$tableleagueselect != "All"){
team_data_table <- team_data_table[team_data_table$lgID == input$tableleagueselect,]
}
if (input$tableteamselect != "All"){
team_data_table <- team_data_table[team_data_table$franchID == input$tableteamselect,]
}
team_data_table
})
# ----------- end team data table
#
})
# ----------- end shinyServer function