【问题标题】:Basic Shiny Question - Making Program Variables Reactive?基本的闪亮问题 - 使程序变量反应?
【发布时间】:2018-09-08 23:50:35
【问题描述】:

我有一个简单的 Shiny 应用程序,它在主面板中显示一个问题。问题只是 10、20 或 30。

“正确”或“错误”的结果显示在 R 控制台中。

然后您可以单击一个动作按钮,“下一步”转到下一个问题,该问题应显示在主面板中。目前,问题未显示,但索引正在工作。

我不清楚如何显示第二个问题。我相信我需要将“问题”或“问题索引”设为反应变量??不确定。

另外,我对 question_index 使用了一个全局变量,我认为这是错误的形式。同样,不确定替代方案是什么。

js <- '
$(document).on("keyup", function(e) {
if(e.keyCode == 13){
Shiny.onInputChange("keyPressed", Math.random());
}
});
'

shinyApp(
  ui = fluidPage(
    tags$script(js),

    sidebarLayout(
      sidebarPanel(
        textInput("answer", 
          width = "50px",
          label = "Answer"),

      actionButton(
        inputId = "next_question",
        label = "Next"
      )
    ),

      mainPanel(
        textOutput("equation")
      )
    )
  ),

  server = function(input, output, session){

    questions <- c("10", "20", "30")
    question_index <<- 1

    Answer <- reactiveVal()

    observeEvent(input$keyPressed, {
      Answer(input$answer)
      if (input$answer == questions[question_index]) {
        print("Right")
      } else {
        print("Wrong")
      }
    })

    output$equation <- renderText({
     # Answer()
      questions[question_index]
    })

    observeEvent(input$next_question, {
      question_index <<- question_index + 1
      print(question_index)
    })

  }
)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    一种方法确实是让问题索引像这样反应

    values <- reactiveValues(question_index=1)
    

    然后将其称为

    values$question_index
    

    【讨论】:

    • 非常有帮助。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-05-20
    • 2021-12-18
    • 2015-09-23
    • 2021-01-12
    • 2018-01-23
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多