我正在试着做一个非常简单的测验。我有一个包含10个问题的data.frame,用户单击以回答0或1。
直到我试图实现
计时器/
倒计时
使下一个问题在5秒后自动出现,使用
invalidateLater()
打电话。
当前问题号存储在reactiveValues()对象中,变量
i
当我添加观察功能时,应用程序停止工作,没有显示问题。我的代码在这里:
init = data.frame(question=paste("question", 1:10), correct=c(1,1,1,1,1,0,0,0,0,0), answer=NA, stringsAsFactors = FALSE)
library(shiny); library(shinydashboard)
ui=dashboardPage(dashboardHeader(title = "questionnaire"),dashboardSidebar(),dashboardBody(
fluidRow(box(width = 6, title="how it works..", p("balblabla"))),
fluidRow(
box(width = 12, background = "orange",
fluidRow(
box(width = 12,
verbatimTextOutput("question"),
tags$head(tags$style("#question{font-size: 20px; text-align: left; font-weight: bold;}"))
),
box(width = 12,
actionButton("negative", "answer 0", width = '30%'),
actionButton("positive", "answer 1", width = '30%')
))))))
server <- function(input, output, session) {
vals = reactiveValues(df = init, i=1)
output$question <- renderText({vals$df[vals$i, "question"]})
observe({
invalidateLater(5000)
vals$i <- vals$i + 1
})
observeEvent(input$negative, ignoreInit=TRUE, {
vals$df[vals$i, "answer"] = 1
if (vals$df[vals$i, "correct"]==1){
showNotification("WRONG!", type="error", duration=1, closeButton = FALSE)
} else {
showNotification("CORRECT!", type="message", duration=1, closeButton = FALSE)
}})
observeEvent(input$positive, ignoreInit=TRUE, {
vals$df[vals$i, "answer"] = 1
if (vals$df[vals$i, "correct"]==0){
showNotification("WRONG!", type="error", duration=1, closeButton = FALSE)
} else {
showNotification("CORRECT!", type="message", duration=1, closeButton = FALSE)
}})
}
shinyApp(ui, server)
你能帮我修一下这个观察报告吗?我不知道这里出了什么问题。