【发布时间】:2021-01-12 13:24:38
【问题描述】:
我的目标是实现一个闪亮的应用程序,它向用户显示几个“问题”并列出可能的答案,然后让用户选择一个答案并存储它。每个问题都有一个用户可以覆盖的先前存储的答案。
我无法找到一种方法来使用反应性对象同时满足这两个约束:
- 让用户选择下一个问题,并根据之前存储的答案重新初始化答案
- 在用户选择新答案时存储当前问题的答案(仅在这种情况下)
下面是一个简化的代码(没有数据,没有加载/写入),它显示了我当前的尝试。 In this version the issue is that when a new problem is selected, the selected answer from the previous problem is immediately written.
library(shiny)
maxProblem=10
ui <- fluidPage(
titlePanel("Debugging test"),
fluidRow(
column(12,
verbatimTextOutput("nbProblems"),
uiOutput("ProblemSelection"),
uiOutput("answerSelection")
)
)
)
server <- function(input, output) {
output$ProblemSelection <- renderUI({
numericInput("ProblemSelectionNo",
"Select Problem no",
value = 1, min=1, max=maxProblem)
})
currentProblemData <- reactive({
print("calling loadCurrentProblemData")
if (!is.null(input$ProblemSelectionNo)) {
print("pretending to load data and previously stored answer for problem", input$ProblemSelectionNo)
list( choices=c(1,2,3), answer=1)
}
})
output$answerSelection <- renderUI({
l<-currentProblemData()
choicesList <- l$choices
names(choicesList) <- l$choices
radioButtons("answerInput", label = "Select answer",
choices = choicesList,
selected = l$answer)
})
writeChanges <- observe({
print('calling writeChanges')
l<-currentProblemData()
newAnswer <- input$answerInput
prevAnswer <- l$answer
if (!is.null(prevAnswer) && !is.null(newAnswer) && (newAnswer != prevAnswer)) {
print(paste('Pretending to write new answer :',newAnswer,'for problem', input$ProblemSelectionNo))
l$answer <- newAnswer
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】:
-
在自己构建一切之前,也许
learnr适合您? -
@starja 感谢您的精彩提示,我不认识学习者,确实,这种任务看起来更方便。