r - 每 12 个输入和初始输入重用相同输入值的代码
问题描述
每当我第一次启动这个应用程序时,备用现金(s)的输出值会根据之前的掷骰更新(如果最后一掷是赢,它会增加值,如果最后一个值是损失,它会减少值),如果我重新启动应用程序,它会在两次运行总共输入 12 次后再次执行此操作。之后重新启动,由于某种原因一切都很好。此外,由于 s 落后,Game Over 输出也落后。为什么是这样?
我尝试完全删除 e 和 func1 以查看我的问题是否与界面的其他部分有关,但这并没有解决我的问题。我希望我能理解为什么它会以如此具体和可预测的方式导致问题,因为我不知道它必须重新运行并且必须输入 12 次值有什么特别之处。我希望我能提供更多的背景,但我很难过。
编辑:我现在将 betting1 定义为服务器内的反应函数(并相应地将 betting1(input$amount) 更改为 betting1() )并且同样的问题仍然存在。
library(shiny)
e <- 0
betting1 <- function(p){
if(is.numeric(p)==T){
x <<- sample(1:6, size = 1)
y <<- sample(1:6, size = 1)
z <<- sum(x,y)
print(z)
if (z == 6| z == 7| z == 8| z == 12) {
e <<- 1
t <- p }
else {
e <<- 0
t <- -p }
return(t)
}
}
func1 <- function(b){
if (e==1){print(paste0("Won: $", b))}
else if(e==0) {print(paste0("Lost: $", b))}}
# Define UI ----
ui <- fluidPage(titlePanel("Game pays you back the amount that you bet. Win on a dice sum of: 6, 7, 8, or 12. Start with $100."),
fluidRow(
sidebarPanel("Game Info: ",br(),"Game #1",
textOutput("numeric_amount"),
textOutput("roll"),
textOutput("outcome")
),
column(4,numericInput("amount", h3("$ Betting Amount"), value = NA)),
column(8,submitButton("Confirm Bet"))
),
mainPanel(
textOutput("game_info"),
textOutput("avg"), br(),
textOutput("plays"), br(),
textOutput("Game_Over"), br(), br(),
textOutput("saves")
)
)
# Define server logic ----
server <- function(input, output){
v <- NULL
s <- 100
i <- 0
output$game_info <- renderText({
l<-betting1(input$amount)
if(input$amount>0 & is.numeric(input$amount)==T){ s<<-(s+l) }
paste("Spare Cash: $", s)})
output$numeric_amount <- renderText({paste("Your last bet: $", input$amount)})
output$avg <- renderText({
if(input$amount>0 & is.numeric(input$amount)==T){
v<<-c(v, input$amount)
paste("Average bet: $", mean(v))}
})
output$outcome <- renderText({paste(func1(input$amount)) })
output$plays <- renderText({
if(input$amount>0 & is.numeric(input$amount)==T){
i<<-i+1
paste("Plays: ", i)}
else{ paste("Plays: ", i)}
})
output$roll <- renderText({
if(input$amount>0 & is.numeric(input$amount)==T){
paste("First Die: ", x, " Second Die: ", y, " Sum: ", z)}
})
output$Game_Over <- renderText({
if(input$amount>0 & is.numeric(input$amount)==T){
if (s<=0){paste("Game Over. Tally your results (Spare Cash, Avg. Bet, Plays) and send them to - with your game number.")}
else {paste("You can choose to stop at any point. When you do, tally your results (Spare Cash, Avg. Bet, Plays) and send them to - with your game number.") }
}
else { paste("Please input a number larger than 0.")}
})
}
# Run the app ----
shinyApp(ui = ui, server = server)
解决方案
如果您将函数定义betting1()
为在服务器部分内部的反应函数,而不是外部标准函数,它似乎可以按预期工作(没有延迟,即使是游戏结束)。
server <- function(input, output){
v <- NULL
s <- 100
i <- 0
betting1 <- reactive({
p <- input$amount
if(is.numeric(p)==T){
x <<- sample(1:6, size = 1)
y <<- sample(1:6, size = 1)
z <<- sum(x,y)
print(z)
if (z == 6| z == 7| z == 8| z == 12) {
e <<- 1
t <- p }
else {
e <<- 0
t <- -p }
return(t)
}
})
[...]
反应式函数在闪亮的应用程序中非常有用,当一个函数被多次调用时,你的环境中的一些元素正在改变,但不是全部。反应式只会更新必要的内容。您可以在此处获得更多信息。
推荐阅读
- nservicebus - NServiceBus6 延迟恢复不延迟
- sublimetext3 - 将包控制与离线存储库一起使用
- excel - Excel,要根据其值查找一个单元格,然后从先前找到的单元格位置开始搜索另一个单元格
- spacy - 在训练模型上使用“Spacy 包”:错误“无法定位模型数据”
- svg - svg 填充颜色 覆盖文本颜色
- r - 基于多列条件 (R) 创建条件列
- python - 为什么 a**2 != a * a 对于某些浮点数?
- css - Shopify - 定位列表项
- javascript - 使用 webpack MiniCssExtractPlugin 和 OptimizeCSSAssetsPlugin 复制 css 文件
- javascript - 使用 Regex 从段落中提取多个 {tag}