Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function(input, output){
- playerData <- reactive({
- #below is probably wrong but it works. maybe create column on load and then playerDisplay is when it equals it?
- allData$playerDisplay <- paste(allData$player, " (" , allData$playerHand, ")", sep="")
- allData$opponentDisplay <- paste(allData$opponent, " (" , allData$opponentHand, ")", sep="")
- filteredData <- subset(allData, allData$player == input$playerSelect & allData$eventDate >= input$dateRange[1] & allData$eventDate <= input$dateRange[2])
- return(filteredData)
- })
- playerCareerData <- reactive({
- #has to be an easier way to do this
- data <- playerData()
- totalWins <- sum(data$winLose == 'Win')
- totalLoss <- sum(data$winLose == 'Loss')
- totalFaceoffs <- totalWins+totalLoss
- winPercentage <- round(totalWins/(totalFaceoffs)*100,digits=2)
- totalWinsHome <- sum(data$winLose == 'Win' & data$playerHomeAway == 'Home')
- totalLossHome <- sum(data$winLose == 'Loss' & data$playerHomeAway == 'Home')
- totalFaceoffsHome <- totalWinsHome+totalLossHome
- winPercentageHome <- round(totalWinsHome/(totalFaceoffsHome)*100,digits=2)
- totalWinsAway <- sum(data$winLose == 'Win' & data$playerHomeAway == 'Away')
- totalLossAway <- sum(data$winLose == 'Loss' & data$playerHomeAway == 'Away')
- totalFaceoffsAway <- totalWinsAway+totalLossAway
- winPercentageAway <- round(totalWinsAway/(totalFaceoffsAway)*100,digits=2)
- totalWinsVsL <- sum(data$winLose == 'Win' & data$opponentHand == 'L')
- totalLossVsL <- sum(data$winLose == 'Loss' & data$opponentHand == 'L')
- totalFaceoffsVsL <- totalWinsVsL+totalLossVsL
- winPercentageVsL <- round(totalWinsVsL/(totalFaceoffsVsL)*100,digits=2)
- totalWinsVsR <- sum(data$winLose == 'Win' & data$opponentHand == 'R')
- totalLossVsR <- sum(data$winLose == 'Loss' & data$opponentHand == 'R')
- totalFaceoffsVsR <- totalWinsVsR+totalLossVsR
- dataPlayer <- c(unique(data$playerDisplay), "", "", "", "")
- dataSub <- c("All", "Home", "Away", "vs. L", "vs. R")
- winPercentageVsR <- round(totalWinsVsR/(totalFaceoffsVsR)*100,digits=2)
- winsV <- c(totalWins, totalWinsHome, totalWinsAway, totalWinsVsL, totalWinsVsR)
- lossV <- c(totalLoss, totalLossHome, totalLossAway, totalLossVsL, totalLossVsR)
- totalFaceoffsV <- c(totalFaceoffs, totalFaceoffsHome, totalFaceoffsAway, totalFaceoffsVsL, totalFaceoffsVsR)
- winPercentageV <- c(winPercentage, winPercentageHome, winPercentageAway, winPercentageVsL, winPercentageVsR)
- careerData.df <- data.frame(dataPlayer,dataSub, totalFaceoffsV, winsV, lossV, winPercentageV)
- careerData <- as.data.table(careerData.df)
- setnames(careerData, "dataPlayer", "Player")
- setnames(careerData, "dataSub", "Subset")
- setnames(careerData, "totalFaceoffsV", "Total Faceoffs")
- setnames(careerData, "winsV", "Wins")
- setnames(careerData, "lossV", "Losses")
- setnames(careerData, "winPercentageV", "Winning Percentage")
- return(careerData)
- })
- playerH2HData <- reactive({
- #has to be an easier way to do this
- data <- playerData()
- opponentChoices <- subset(data, data$opponent == input$opponentSelect)
- data <- opponentChoices
- totalWins <- sum(data$winLose == 'Win')
- totalLoss <- sum(data$winLose == 'Loss')
- totalFaceoffs <- totalWins+totalLoss
- winPercentage <- round(totalWins/(totalFaceoffs)*100,digits=2)
- totalWinsHome <- sum(data$winLose == 'Win' & data$playerHomeAway == 'Home')
- totalLossHome <- sum(data$winLose == 'Loss' & data$playerHomeAway == 'Home')
- totalFaceoffsHome <- totalWinsHome+totalLossHome
- winPercentageHome <- round(totalWinsHome/(totalFaceoffsHome)*100,digits=2)
- totalWinsAway <- sum(data$winLose == 'Win' & data$playerHomeAway == 'Away')
- totalLossAway <- sum(data$winLose == 'Loss' & data$playerHomeAway == 'Away')
- totalFaceoffsAway <- totalWinsAway+totalLossAway
- winPercentageAway <- round(totalWinsAway/(totalFaceoffsAway)*100,digits=2)
- dataPlayer <- c(unique(data$playerDisplay), " vs.", unique(data$opponentDisplay))
- dataSub <- c("All", "Home", "Away")
- winsV <- c(totalWins, totalWinsHome, totalWinsAway)
- lossV <- c(totalLoss, totalLossHome, totalLossAway)
- totalFaceoffsV <- c(totalFaceoffs, totalFaceoffsHome, totalFaceoffsAway)
- winPercentageV <- c(winPercentage, winPercentageHome, winPercentageAway)
- h2h.df <- data.frame(dataPlayer,dataSub, totalFaceoffsV, winsV, lossV, winPercentageV)
- h2hData <- as.data.table(h2h.df)
- setnames(h2hData, "dataPlayer", "Player")
- setnames(h2hData, "dataSub", "Subset")
- setnames(h2hData, "totalFaceoffsV", "Total Faceoffs")
- setnames(h2hData, "winsV", "Wins")
- setnames(h2hData, "lossV", "Losses")
- setnames(h2hData, "winPercentageV", "Winning Percentage")
- return(h2hData)
- })
- teamIndividualData <- reactive({
- filteredData <- subset(allData, playerTeam == input$teamSelect & allData$eventDate >= input$dateRange[1] & allData$eventDate <= input$dateRange[2])
- return(filteredData)
- })
- teamFilteredData <- reactive({
- filteredData <- subset(allData, allData$eventDate >= input$dateRange[1] & allData$eventDate <= input$dateRange[2])
- return(filteredData)
- })
- output$careerDataPlayer <- renderDT({
- data <- playerCareerData()
- datatable(data, rownames = FALSE, options = list(pageLength = nrow(data)))
- })
- output$yearlyDataPlayer <- renderDT({
- data <- playerData()
- datatable(data, rownames = FALSE, options = list(pageLength = nrow(data), columnDefs = list(list(visible=FALSE, targets=c(0,1,7,17)))))
- })
- output$h2hData <- renderDT({
- data <- playerH2HData()
- datatable(data, rownames = FALSE, options = list(pageLength = nrow(data)))
- })
- output$runningAveragePlot <- renderPlot({
- dataRoll <- playerData()
- runningAverageValue <- input$runningAverageValue
- #add something so if the textbox is empty don't display graph
- faceoffsMean <- rollmean(dataRoll$winLoseNumber, k = runningAverageValue, fill = NA)
- plot <- ggplot(dataRoll, aes_string(x = dataRoll$eventDate, y = faceoffsMean))
- plot <- plot + geom_line()
- plot <- plot + ggtitle(paste(input$playerSelect, " ", runningAverageValue," Faceoff Rolling Average for ", input$dateRange[1], " to ", input$dateRange[2], sep=""))
- plot <- plot + labs(y = "Winning Percentage", x = "Date")
- plot
- })
- output$opportunityBreakdownTable <- renderDT({
- data <- teamIndividualData()
- a <- as.data.frame(table(data$player))
- datatable(a, rownames = FALSE, options = list(pageLength = nrow(data), autoWidth = TRUE))
- setnames(tmp.dt, "Var1", "Player")
- setnames(tmp.dt, "Freq", "Faceoff Opportunities")
- })
- output$opportunityBreakdown <- renderDT({
- data <- teamIndividualData()
- datatable(data, rownames = FALSE, options = list(pageLength = 50, columnDefs = list(list(visible=FALSE, targets=c(0,1,7,17)))))
- })
- output$opportunityBreakdownBoxPlot <- renderPlot({
- data <- teamIndividualData()
- a <- as.data.frame(table(data$player))
- # ggplot won't work for some reason. gives me an error about unable to write to disk but it works above for running plots
- # and it works in the console
- ggplot(a, aes(x = Var1, y = Freq))
- plot <- plot + geom_col(fill = "blue")
- plot <- plot + ggtitle(paste0("Total Faceoffs For Each Player"))
- plot
- })
- output$teamComparisonTBL <- renderDT({
- data <- teamFilteredData()
- win <- subset(data, winLose == "Win")
- loss <- subset(data, winLose == "Loss")
- #learn dplyr for R, different than python
- totalWin <- win %>% count(playerTeam)
- totalLoss <- loss %>% count(playerTeam)
- totalFaceoffs <- data %>% count(playerTeam)
- totalWinPercentage <- round(totalWin$n/totalFaceoffs$n*100, digits=2)
- tmp.df <- data.frame(totalWin$playerTeam, totalWin$n, totalLoss$n, totalFaceoffs$n, totalWinPercentage)
- tmp.dt <- as.data.table(tmp.df)
- setnames(tmp.dt, "totalWin.playerTeam", "Team")
- setnames(tmp.dt, "totalWin.n", "Faceoffs Won")
- setnames(tmp.dt, "totalLoss.n", "Faceoffs Lost")
- setnames(tmp.dt, "totalFaceoffs.n", "Faceoffs Total")
- setnames(tmp.dt, "totalWinPercentage", "Win Percentage")
- datatable(tmp.dt, rownames = FALSE, options = list(pageLength = nrow(tmp.dt)))
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement