嘿伙计们,我正在制作我的第一个闪亮的应用程序并且正在玩它。但是我已经停止了。我为NBA创造了一个非常基本的游戏模拟。所以你选择了两支球队,他们一起比赛,并给你一个最终得分。我希望能够在游戏进行时输入一个显示得分的图表(因此每个团队的当前点数每1秒或2秒绘制一次)。
每次提交团队时我都无法更改图表,而我无法弄清楚如何解决这个问题。我想我必须让它反应或将图形函数放入函数中,但我不知道该怎么做。
即使输入变量没有改变,我也很好奇如何让我的提交按钮工作。这样你可以选择两支球队并模拟让我们连续比赛7场比赛。但是我每次都必须更换团队才能重新提交。
这是我的代码:
UI.R
##UI.R
shinyUI(fluidPage(
titlePanel("NBA Simulation","NBA Simulation"),
sidebarLayout(
sidebarPanel(
helpText("Choose two different NBA teams to simulate a game!"),
selectInput("team1",
label="Team 1:",
choices=list("Atlanta Hawks"=1, "Boston Celtics"=2, "Brooklyn Nets"=3, "Charlotte Hornets"=4, "Chicago Bulls"=5, "Cleveland Cavaliers"=6, "Dallas Mavericks"=7, "Denver Nuggets"=8, "Detroit Pistons"=9, "Golden State Warriors"=10, "Houston Rockets"=11, "Indiana Pacers"=12, "LA Clippers"=13, "LA Lakers"=14, "Memphis Grizzlies"=15, "Miami Heat"=16, "Milwaukee Bucks"=17, "Minnesota Timberwolves"=18, "New Orleans Pelicans"=19, "New York Knicks"=20, "OKC Thunder"=21, "Orlando Magic"=22, "Philadelphia 76ers"=23, "Phoenix Suns"=24, "Portland Trailblazers"=25, "Sacramento Kings"=26, "San Antonio Spurs"=27, "Toronto Raptors"=28, "Utah Jazz"=29, "Washington Wizards"=30),
selected=1),
selectInput("team2",
label="Team 2:",
choices=list("Atlanta Hawks"=1, "Boston Celtics"=2, "Brooklyn Nets"=3, "Charlotte Hornets"=4, "Chicago Bulls"=5, "Cleveland Cavaliers"=6, "Dallas Mavericks"=7, "Denver Nuggets"=8, "Detroit Pistons"=9, "Golden State Warriors"=10, "Houston Rockets"=11, "Indiana Pacers"=12, "LA Clippers"=13, "LA Lakers"=14, "Memphis Grizzlies"=15, "Miami Heat"=16, "Milwaukee Bucks"=17, "Minnesota Timberwolves"=18, "New Orleans Pelicans"=19, "New York Knicks"=20, "OKC Thunder"=21, "Orlando Magic"=22, "Philadelphia 76ers"=23, "Phoenix Suns"=24, "Portland Trailblazers"=25, "Sacramento Kings"=26, "San Antonio Spurs"=27, "Toronto Raptors"=28, "Utah Jazz"=29, "Washington Wizards"=30),
selected=2),
submitButton("Submit")
),
mainPanel(
h1("Final Result"),
htmlOutput("simLogo1"),
htmlOutput("simResult"),
htmlOutput("simLogo2"),
plotOutput("scrPlot")
)
)
)
)
server.R
##server.R
source("TwoTeamSimulation.R")
shinyServer(function(input, output) {
output$simResult <- renderText({
team1 <- input$team1
team2<- input$team2
simgame(team1,team2)
})
output$simLogo1 <- renderText({
team1 <- input$team1
sprintf('<img src="%s.jpg" height="150" width="350"/>', team1)
})
output$simLogo2 <- renderText({
team2 <- input$team2
sprintf('<img src="%s.jpg" height="150" width="350"/>', team2)
})
output$scrPlot <- renderPlot({
return(graphsp)
})
})
TwoTeamSimulation.R
##TwoTeamSimulation.R
Stats<-NBA.Stats
Stats
library(ggplot2)
simgame<-function(team1,team2){
#assign variables
team1name<<-Stats[team1,1]
team2name<<-Stats[team2,1]
#find total attempts
totalAtt1<-Stats[team1,3]+Stats[team1,5]+(Stats[team1,7]/2)+Stats[team1,8]+Stats[team1,9]
#Find the probability of attempting a 2 pt, 3 pt, ft, and turnover.
twoPtP1<-Stats[team1,3]/totalAtt1
threePtP1<-Stats[team1,5]/totalAtt1
FtP1<-(Stats[team1,7]/2)/totalAtt1
TOP1<-Stats[team1,8]/totalAtt1
ORB1<-Stats[team1,9]/totalAtt1
#set team1 points to 0
team1pt<-0
#set team1 possession counter
i1<-0
#create score progression
t1sp<-c()
#Now we start to run through the simulation
while(i1 <= 100){
#Randomly select what action is taken
action<-sample(1:4, 1, prob=c(twoPtP1, threePtP1, FtP1, TOP1))
#Action = 1? Two Point Attempt
if(action==1){
#1 = Make, 2= Miss
twoMake1<-sample(1:2,1,prob=c(Stats[team1,2],(1-Stats[team1,2])))
if(twoMake1==1){
team1pt<-team1pt+2
t1sp <- c(team1pt,t1sp)
i1<-i1+1
}
if(twoMake1==2){
oreb21<-sample(1:2,1,prob=c(ORB1,(1-ORB1)))
if(oreb21==1){
i1<-i1
}
if(oreb21==2){
t1sp <- c(team1pt,t1sp)
i1<-i1+1
}
}
}
#Action = 2? Three Point Attempt
if(action==2){
#1 = Make, 2= Miss
threeMake1<-sample(1:2,1,prob=c(Stats[team1,4],(1-Stats[team1,4])))
if(threeMake1==1){
team1pt<-team1pt+3
t1sp <- c(team1pt,t1sp)
i1<-i1+1
}
if(threeMake1==2){
oreb31<-sample(1:2,1,prob=c(ORB1,(1-ORB1)))
if(oreb31==1){
i1<-i1
}
if(oreb31==2){
t1sp <- c(team1pt,t1sp)
i1<-i1+1
}
}
}
#Action = 3? Free Throw Attempt
if(action==3){
#free throw 1 and 2
ft11<-sample(1:2,1,prob=c(Stats[team1,6],(1-Stats[team1,6])))
ft21<-sample(1:2,1,prob=c(Stats[team1,6],(1-Stats[team1,6])))
if(ft11==1 & ft21==1){ #Makes both
team1pt<-team1pt+2
t1sp <- c(team1pt,t1sp)
i1<-i1+1
}
if(ft11==1 & ft21==2){ #Makes first, misses second
team1pt<-team1pt+1
t1sp <- c(team1pt,t1sp)
i1<-i1+1
}
if(ft11==2 & ft21==1){ #Misses first, makes second
team1pt<-team1pt+1
t1sp <- c(team1pt,t1sp)
i1<-i1+1
}
if(ft11==2 & ft21==2){ #Misses both
t1sp <- c(team1pt,t1sp)
i1<-i1+1
}
}
if(action==4){ #Turnover so nothing happens besides them losing a possesion
t1sp <- c(team1pt,t1sp)
i1<-i1+1
}
}
#find total attempts
totalAtt2<-Stats[team2,3]+Stats[team2,5]+(Stats[team2,7]/2)+Stats[team2,8]+Stats[team2,9]
#Find the probability of attempting a 2 pt, 3 pt, ft, and turnover.
twoPtP2<-Stats[team2,3]/totalAtt2
threePtP2<-Stats[team2,5]/totalAtt2
FtP2<-(Stats[team2,7]/2)/totalAtt2
TOP2<-Stats[team2,8]/totalAtt2
ORB2<-Stats[team2,9]/totalAtt2
#set team1 points to 0
team2pt<-0
#set team1 possession counter
i2<-0
#set team2 score progression
t2sp<-c()
#Now we start to run through the simulation
while(i2 <= 100){
#Randomly select what action is taken
action<-sample(1:4, 1, prob=c(twoPtP2, threePtP2, FtP2, TOP2))
#Action = 1? Two Point Attempt
if(action==1){
#1 = Make, 2= Miss
twoMake2<-sample(1:2,1,prob=c(Stats[team2,2],(1-Stats[team2,2])))
if(twoMake2==1){
team2pt<-team2pt+2
t2sp <- c(team2pt,t2sp)
i2<-i2+1
}
if(twoMake2==2){
oreb22<-sample(1:2,1,prob=c(ORB2,(1-ORB2)))
if(oreb22==1){
i2<-i2
}
if(oreb22==2){
t2sp <- c(team2pt,t2sp)
i2<-i2+1
}
}
}
#Action = 2? Three Point Attempt
if(action==2){
#1 = Make, 2= Miss
threeMake2<-sample(1:2,1,prob=c(Stats[team2,4],(1-Stats[team2,4])))
if(threeMake2==1){
team2pt<-team2pt+3
t2sp <- c(team2pt,t2sp)
i2<-i2+1
}
if(threeMake2==2){
oreb32<-sample(1:2,1,prob=c(ORB2,(1-ORB2)))
if(oreb32==1){
i2<-i2
}
if(oreb32==2){
t2sp <- c(team2pt,t2sp)
i2<-i2+1
}
}
}
#Action = 3? Free Throw Attempt
if(action==3){
#free throw 1 and 2
ft12<-sample(1:2,1,prob=c(Stats[team2,6],(1-Stats[team2,6])))
ft22<-sample(1:2,1,prob=c(Stats[team2,6],(1-Stats[team2,6])))
if(ft12==1 & ft22==1){ #Makes both
team2pt<-team2pt+2
t2sp <- c(team2pt,t2sp)
i2<-i2+1
}
if(ft12==1 & ft22==2){ #Makes first, misses second
team2pt<-team2pt+1
t2sp <- c(team2pt,t2sp)
i2<-i2+1
}
if(ft12==2 & ft22==1){ #Misses first, makes second
team2pt<-team2pt+1
t2sp <- c(team2pt,t2sp)
i2<-i2+1
}
if(ft12==2 & ft22==2){ #Misses both
t2sp <- c(team2pt,t2sp)
i2<-i2+1
}
}
if(action==4){ #Turnover so nothing happens besides them losing a possesion
t2sp <- c(team2pt,t2sp)
i2<-i2+1
}
}
result1<-sprintf("<h4><strong> %s Final: </h4></strong> %s", Stats[team1,1],team1pt)
result2<-sprintf("<h4><strong> %s Final: </h4></strong> %s", Stats[team2,1],team2pt)
team1sp <<- rev(t1sp)
team2sp <<- rev(t2sp)
gamescore <<- cbind(team1sp,team2sp)
paste(result1, result2, sep="<br>")
}
simgame(2,7)
#GRAPHING
comsc<-c(team1sp,team2sp)
teamid <- c(rep("Team 1", length(team1sp)), rep("Team 2", length(team2sp)))
scoringprog <- data.frame(pos=rep(1:101, 2), score=comsc, team=teamid)
scoringprog
graphsp <- ggplot(data=scoringprog, aes(x=pos, y=comsc, color=team)) +
geom_line(aes(color=team))+
geom_point(aes(color=team))+
xlab("Possesion")+
ylab("Score")+
ggtitle("Scoring Progression")+
scale_color_discrete(name="Team Name",
breaks=c("Team 1", "Team 2"),
labels=c(as.character(team1name), as.character(team2name)))
graphsp