我一直在尝试用Shiny创建一个骰子/赌博模拟器,但我遇到了两个问题(尽管这两个问题都可能与一个错误有关,我不确定)。我基本上创建了一个reactiveValue,最初在服务器中设置为10000 ($),以及一个sample( )函数,它只从1-6 (对于六面骰子)中抽取,并允许用户选择骰子/掷骰子的数量。然后,我设置了一个observeEvent,其中的if循环确定,如果sample( )函数中生成的随机数等于或大于用户下注的骰子数量,并且只要reactiveValue保持在1左右,那么用户就可以将他们的“钱”加倍。如果他们的赌注小于样本产生的赌注,那么他们就输了一半。如果他们输得太多以至于低于1,那么他们就不能再下注了,或者至少被告知他们破产了,但这对代码来说并不是那么重要的部分。
现在,我似乎无法让if循环工作,因为它抛出了error: Warning: Error in if: missing value where TRUE/FALSE
library(shiny)
ui <- fluidPage(
pageWithSidebar(
titlePanel("Feelin' Lucky?"),
sidebarPanel(
p("In this simulation, you can select the number of dice you would like to roll,
and then make a 'bet' on the numbers that will show up with every roll. If there are more faces
than the number you guessed, then you still win. If there are less faces, however,
you will lose 'money,' displayed at the top of the main panel. Give it a try and
good luck!"),
numericInput("dicenum", "Enter the number of dice to roll:", 1),
numericInput("face", "What face do you think will be rolled?", 1),
numericInput("bet", "How many of this face do you think will be rolled?", 1),
actionButton("go1", "GO!")
),
mainPanel(
htmlOutput("the.dough"),
htmlOutput("the.bet"),
textOutput("results"),
htmlOutput("results2"),
htmlOutput("warning")
)
)
)
server <- function(input, output, session) {
money <- 10000
output$the.dough <- renderText(
paste(money)
)
buttonValue <- reactiveValues(go1=FALSE)
rv <- reactiveValues(money.tot = 10000)
observeEvent(input$go1,{
isolate({buttonValue$go1=TRUE})
the.roll <- sample(1:6, size = input$dicenum, replace=TRUE)
the.face <- as.numeric(input$face) #Dice Face Bet
amount <- as.numeric(input$bet)
output$warning <- renderText({
goop <- sum(the.roll==the.face)
if ((goop >= amount) & (rv$money.tot>1)) {
rv$money.tot <- sum(rv$money.tot, rv$money.tot*2-rv$money.tot)
paste("Well-done! You won the bid! You now have: $", rv$money.tot)
} else if ((goop < amount) & (rv$money.tot>1)) {
rv$money.tot <- rv$money.tot-(rv$money.tot/2)
paste("Darn! It looks like you lost the bid! You now have: $", rv$money.tot)
} else if (rv$money.tot<1) {
paste("You are broke! You cannot bid anymore money!")
} else {
NULL }
})
})
}
shinyApp(ui, server)我已经查找了这意味着什么,但我找不到一个案例来准确地解释为什么它会在我的脚本中这样做,特别是因为if循环在编写为基本R if循环时可以工作,即没有反应性或闪亮涉及。工作循环示例:
rvv <- 10000
face.guess <- 4
throw <- sample(1:6, size = 10, replace = TRUE)
guess.howmany <- 2
if (sum(throw==face.guess)>=guess.howmany & rvv>1) {
rvv <- sum(rvv, rvv*2-rvv)
print("Woohoo!")
print(rvv)
} else if (sum(throw==face.guess)<guess.howmany & rvv>1){
print("Darn")
rvv <- rvv-(rvv/2)
print(rvv)
} else if (rvv<1) {
print("You are broke! You can't bid anymore money!") }其他时候,if循环似乎确实部分起作用,但只是跳到最后一个选项,以某种方式将起始reactiveValue降低到1以下,并在第一次竞标时声明它们被打破,这应该是不可能的。
我的电脑现在也是摇摇晃晃的,所以如果上面有语法问题,我很抱歉--部分原因可能是目前无法正确复制/粘贴。
任何帮助都是非常感谢的!
编辑:已修复代码。我还计算出,当用户获得正确的出价时,会有延迟,然后它会抛出上面的错误。当他们猜测的值太高/错误时,它会显示最后一个选项(print("You‘are!“)),这似乎表明我调用另一个条件("goop")的方式存在问题。我只是不明白是什么。
发布于 2019-08-10 08:53:54
我想出了一个解决方法:我基本上必须将if循环放在renderText( )的外部,而不是在if循环的每个步骤中嵌入多个呈现。我复制了下面更正后的服务器部分。可能不是最优雅的修复,但它的工作方式如我所愿:
server <- function(input, output, session) {
money <- 10000
output$the.dough <- renderText(
paste(money)
)
buttonValue <- reactiveValues(go1=FALSE)
rv <- reactiveValues(money.tot = 10000)
observeEvent(input$go1,{
isolate({buttonValue$go1=TRUE})
the.roll <- sample(1:6, size = input$dicenum, replace=TRUE)
the.face <- as.numeric(input$face) #Dice Face Bet
amount <- as.numeric(input$bet)
goop <- sum(the.roll==the.face)
if (isTRUE(goop >= amount) & isTRUE(rv$money.tot>1) & buttonValue$go1) {
rv$money.tot <- sum(rv$money.tot, rv$money.tot*2-rv$money.tot)
output$warning <- renderText({
paste("Well-done! You won the bid! You now have: $", rv$money.tot)
})
} else if (isTRUE(goop < amount) & isTRUE(rv$money.tot>1) & buttonValue$go1) {
rv$money.tot <- rv$money.tot-(rv$money.tot/2)
output$warning <- renderText({
paste("Darn! It looks like you lost the bid! You now have: $", rv$money.tot)
})
} else if (isTRUE(rv$money.tot<1) & buttonValue$go1) {
output$warning <- renderText({
paste("You are broke! You cannot bid anymore money!")
})
} else {
NULL }
output$results <- renderText(
paste("You bid that there would be:", amount, "of the", the.face, "face.")
)
output$results2 <- renderText(
paste("Actual Result:", as.numeric(sum(the.roll==the.face)), "of the", the.face)
)
})
}希望这篇文章能帮助任何想做类似事情的人!
https://stackoverflow.com/questions/57435885
复制相似问题