Advertisement
Guest User

server.R

a guest
Dec 27th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. function(input, output){
  2.  
  3. playerData <- reactive({
  4. #below is probably wrong but it works. maybe create column on load and then playerDisplay is when it equals it?
  5. allData$playerDisplay <- paste(allData$player, " (" , allData$playerHand, ")", sep="")
  6. allData$opponentDisplay <- paste(allData$opponent, " (" , allData$opponentHand, ")", sep="")
  7. filteredData <- subset(allData, allData$player == input$playerSelect & allData$eventDate >= input$dateRange[1] & allData$eventDate <= input$dateRange[2])
  8.  
  9. return(filteredData)
  10.  
  11. })
  12.  
  13. playerCareerData <- reactive({
  14. #has to be an easier way to do this
  15. data <- playerData()
  16. totalWins <- sum(data$winLose == 'Win')
  17. totalLoss <- sum(data$winLose == 'Loss')
  18. totalFaceoffs <- totalWins+totalLoss
  19. winPercentage <- round(totalWins/(totalFaceoffs)*100,digits=2)
  20. totalWinsHome <- sum(data$winLose == 'Win' & data$playerHomeAway == 'Home')
  21. totalLossHome <- sum(data$winLose == 'Loss' & data$playerHomeAway == 'Home')
  22. totalFaceoffsHome <- totalWinsHome+totalLossHome
  23. winPercentageHome <- round(totalWinsHome/(totalFaceoffsHome)*100,digits=2)
  24. totalWinsAway <- sum(data$winLose == 'Win' & data$playerHomeAway == 'Away')
  25. totalLossAway <- sum(data$winLose == 'Loss' & data$playerHomeAway == 'Away')
  26. totalFaceoffsAway <- totalWinsAway+totalLossAway
  27. winPercentageAway <- round(totalWinsAway/(totalFaceoffsAway)*100,digits=2)
  28. totalWinsVsL <- sum(data$winLose == 'Win' & data$opponentHand == 'L')
  29. totalLossVsL <- sum(data$winLose == 'Loss' & data$opponentHand == 'L')
  30. totalFaceoffsVsL <- totalWinsVsL+totalLossVsL
  31. winPercentageVsL <- round(totalWinsVsL/(totalFaceoffsVsL)*100,digits=2)
  32. totalWinsVsR <- sum(data$winLose == 'Win' & data$opponentHand == 'R')
  33. totalLossVsR <- sum(data$winLose == 'Loss' & data$opponentHand == 'R')
  34. totalFaceoffsVsR <- totalWinsVsR+totalLossVsR
  35. dataPlayer <- c(unique(data$playerDisplay), "", "", "", "")
  36. dataSub <- c("All", "Home", "Away", "vs. L", "vs. R")
  37. winPercentageVsR <- round(totalWinsVsR/(totalFaceoffsVsR)*100,digits=2)
  38. winsV <- c(totalWins, totalWinsHome, totalWinsAway, totalWinsVsL, totalWinsVsR)
  39. lossV <- c(totalLoss, totalLossHome, totalLossAway, totalLossVsL, totalLossVsR)
  40. totalFaceoffsV <- c(totalFaceoffs, totalFaceoffsHome, totalFaceoffsAway, totalFaceoffsVsL, totalFaceoffsVsR)
  41. winPercentageV <- c(winPercentage, winPercentageHome, winPercentageAway, winPercentageVsL, winPercentageVsR)
  42. careerData.df <- data.frame(dataPlayer,dataSub, totalFaceoffsV, winsV, lossV, winPercentageV)
  43. careerData <- as.data.table(careerData.df)
  44. setnames(careerData, "dataPlayer", "Player")
  45. setnames(careerData, "dataSub", "Subset")
  46. setnames(careerData, "totalFaceoffsV", "Total Faceoffs")
  47. setnames(careerData, "winsV", "Wins")
  48. setnames(careerData, "lossV", "Losses")
  49. setnames(careerData, "winPercentageV", "Winning Percentage")
  50. return(careerData)
  51.  
  52. })
  53.  
  54. playerH2HData <- reactive({
  55. #has to be an easier way to do this
  56. data <- playerData()
  57. opponentChoices <- subset(data, data$opponent == input$opponentSelect)
  58. data <- opponentChoices
  59. totalWins <- sum(data$winLose == 'Win')
  60. totalLoss <- sum(data$winLose == 'Loss')
  61. totalFaceoffs <- totalWins+totalLoss
  62. winPercentage <- round(totalWins/(totalFaceoffs)*100,digits=2)
  63. totalWinsHome <- sum(data$winLose == 'Win' & data$playerHomeAway == 'Home')
  64. totalLossHome <- sum(data$winLose == 'Loss' & data$playerHomeAway == 'Home')
  65. totalFaceoffsHome <- totalWinsHome+totalLossHome
  66. winPercentageHome <- round(totalWinsHome/(totalFaceoffsHome)*100,digits=2)
  67. totalWinsAway <- sum(data$winLose == 'Win' & data$playerHomeAway == 'Away')
  68. totalLossAway <- sum(data$winLose == 'Loss' & data$playerHomeAway == 'Away')
  69. totalFaceoffsAway <- totalWinsAway+totalLossAway
  70. winPercentageAway <- round(totalWinsAway/(totalFaceoffsAway)*100,digits=2)
  71.  
  72. dataPlayer <- c(unique(data$playerDisplay), " vs.", unique(data$opponentDisplay))
  73. dataSub <- c("All", "Home", "Away")
  74.  
  75. winsV <- c(totalWins, totalWinsHome, totalWinsAway)
  76. lossV <- c(totalLoss, totalLossHome, totalLossAway)
  77. totalFaceoffsV <- c(totalFaceoffs, totalFaceoffsHome, totalFaceoffsAway)
  78. winPercentageV <- c(winPercentage, winPercentageHome, winPercentageAway)
  79.  
  80. h2h.df <- data.frame(dataPlayer,dataSub, totalFaceoffsV, winsV, lossV, winPercentageV)
  81. h2hData <- as.data.table(h2h.df)
  82. setnames(h2hData, "dataPlayer", "Player")
  83. setnames(h2hData, "dataSub", "Subset")
  84. setnames(h2hData, "totalFaceoffsV", "Total Faceoffs")
  85. setnames(h2hData, "winsV", "Wins")
  86. setnames(h2hData, "lossV", "Losses")
  87. setnames(h2hData, "winPercentageV", "Winning Percentage")
  88. return(h2hData)
  89.  
  90. })
  91.  
  92. teamIndividualData <- reactive({
  93.  
  94. filteredData <- subset(allData, playerTeam == input$teamSelect & allData$eventDate >= input$dateRange[1] & allData$eventDate <= input$dateRange[2])
  95.  
  96. return(filteredData)
  97.  
  98. })
  99.  
  100. teamFilteredData <- reactive({
  101.  
  102. filteredData <- subset(allData, allData$eventDate >= input$dateRange[1] & allData$eventDate <= input$dateRange[2])
  103.  
  104. return(filteredData)
  105.  
  106. })
  107.  
  108. output$careerDataPlayer <- renderDT({
  109.  
  110. data <- playerCareerData()
  111.  
  112. datatable(data, rownames = FALSE, options = list(pageLength = nrow(data)))
  113.  
  114. })
  115.  
  116. output$yearlyDataPlayer <- renderDT({
  117.  
  118. data <- playerData()
  119.  
  120. datatable(data, rownames = FALSE, options = list(pageLength = nrow(data), columnDefs = list(list(visible=FALSE, targets=c(0,1,7,17)))))
  121.  
  122. })
  123.  
  124. output$h2hData <- renderDT({
  125.  
  126. data <- playerH2HData()
  127.  
  128. datatable(data, rownames = FALSE, options = list(pageLength = nrow(data)))
  129.  
  130. })
  131.  
  132. output$runningAveragePlot <- renderPlot({
  133.  
  134. dataRoll <- playerData()
  135. runningAverageValue <- input$runningAverageValue
  136. #add something so if the textbox is empty don't display graph
  137. faceoffsMean <- rollmean(dataRoll$winLoseNumber, k = runningAverageValue, fill = NA)
  138. plot <- ggplot(dataRoll, aes_string(x = dataRoll$eventDate, y = faceoffsMean))
  139. plot <- plot + geom_line()
  140. plot <- plot + ggtitle(paste(input$playerSelect, " ", runningAverageValue," Faceoff Rolling Average for ", input$dateRange[1], " to ", input$dateRange[2], sep=""))
  141. plot <- plot + labs(y = "Winning Percentage", x = "Date")
  142. plot
  143. })
  144.  
  145. output$opportunityBreakdownTable <- renderDT({
  146.  
  147. data <- teamIndividualData()
  148. a <- as.data.frame(table(data$player))
  149. datatable(a, rownames = FALSE, options = list(pageLength = nrow(data), autoWidth = TRUE))
  150. setnames(tmp.dt, "Var1", "Player")
  151. setnames(tmp.dt, "Freq", "Faceoff Opportunities")
  152.  
  153. })
  154.  
  155. output$opportunityBreakdown <- renderDT({
  156.  
  157. data <- teamIndividualData()
  158. datatable(data, rownames = FALSE, options = list(pageLength = 50, columnDefs = list(list(visible=FALSE, targets=c(0,1,7,17)))))
  159.  
  160.  
  161. })
  162.  
  163. output$opportunityBreakdownBoxPlot <- renderPlot({
  164.  
  165. data <- teamIndividualData()
  166. a <- as.data.frame(table(data$player))
  167. # ggplot won't work for some reason. gives me an error about unable to write to disk but it works above for running plots
  168. # and it works in the console
  169. ggplot(a, aes(x = Var1, y = Freq))
  170. plot <- plot + geom_col(fill = "blue")
  171. plot <- plot + ggtitle(paste0("Total Faceoffs For Each Player"))
  172. plot
  173.  
  174. })
  175.  
  176.  
  177. output$teamComparisonTBL <- renderDT({
  178.  
  179. data <- teamFilteredData()
  180. win <- subset(data, winLose == "Win")
  181. loss <- subset(data, winLose == "Loss")
  182. #learn dplyr for R, different than python
  183. totalWin <- win %>% count(playerTeam)
  184. totalLoss <- loss %>% count(playerTeam)
  185. totalFaceoffs <- data %>% count(playerTeam)
  186. totalWinPercentage <- round(totalWin$n/totalFaceoffs$n*100, digits=2)
  187. tmp.df <- data.frame(totalWin$playerTeam, totalWin$n, totalLoss$n, totalFaceoffs$n, totalWinPercentage)
  188. tmp.dt <- as.data.table(tmp.df)
  189. setnames(tmp.dt, "totalWin.playerTeam", "Team")
  190. setnames(tmp.dt, "totalWin.n", "Faceoffs Won")
  191. setnames(tmp.dt, "totalLoss.n", "Faceoffs Lost")
  192. setnames(tmp.dt, "totalFaceoffs.n", "Faceoffs Total")
  193. setnames(tmp.dt, "totalWinPercentage", "Win Percentage")
  194. datatable(tmp.dt, rownames = FALSE, options = list(pageLength = nrow(tmp.dt)))
  195.  
  196. })
  197.  
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement