【发布时间】:2019-11-13 12:03:49
【问题描述】:
我正在编写一个闪亮的应用程序来模拟各种草稿,用户将从模拟选择之间的列表中进行选择(随机选择)。为了更好地解释这个前提,我们从 12 个选项的列表开始。当他们的“槽”出现时,用户将选择这些选项之一。因此,如果他们选择“4”作为他们的位置,前 3 个选项将被随机模拟。然后用户将做出他们的选择,最后的 8 个选择将被随机模拟……无论何时做出选择,该选择都会从列表中删除。所以每个选项只能选择一次。
所以我需要遍历数字 1-12,然后随机模拟该选择,或者(如果轮到用户)允许通过用户输入进行选择。
我还没有弄清楚如何在用户做出选择之前暂停循环,然后在单击按钮确认后继续。我的最小、可重现示例的代码如下。照原样,用户基本上被跳过了,因为循环没有暂停。
new_list<-c()
choice_list<-c("Choice 1","Choice 2","Choice 3","Choice 4",
"Choice 5","Choice 6","Choice 7","Choice 8",
"Choice 9","Choice 10","Choice 11","Choice 12")
ui<-navbarPage(title="Title", id="navbar",
tabPanel("Main",
selectInput(inputId="slot", label="Select your Slot", choices=1:12),
actionButton(inputId="start", label="Start Process"),
fluidRow(hr()),
selectInput(inputId="user_choice", label="Choose an Option", choices=choice_list),
actionButton(inputId="selection", label="Make Selection"))
)
server<-function(input, output, session) {
observeEvent(input$start, {
user_slot<-isolate(input$slot)
print(paste0("User slot: ",user_slot))
print("----------")
print("----------")
for (i in 1:12) {
if (i==user_slot) {
print("TIME FOR USER SELECTION")
observeEvent(input$selection, {
user_choice<-isolate(input$user_choice)
print(paste0("User choice: ",user_choice))
new_list<-c(new_list,user_choice)
choice_list<-choice_list[choice_list!=user_choice]
})
} else {
random_number<-sample(seq(1:length(choice_list)),1)
print(paste0("Random number: ",random_number))
random_choice<-choice_list[random_number]
print(paste0("Random choice: ",random_choice))
new_list<-c(new_list,random_choice)
choice_list<-choice_list[choice_list!=random_choice]
}
print(paste0("# of choices left: ",length(choice_list)))
print("----------")
}
})
}
shinyApp(ui=ui, server=server)
有没有办法暂停循环,但在触发“observeEvent()”时让它继续?是否有可以解决此问题的“observeEvent()”替代方法?还是我必须重新设计具有不同循环过程的解决方案?
提前致谢!
【问题讨论】: