【发布时间】:2021-01-02 17:41:41
【问题描述】:
我将继续尝试了解闪亮的基础知识。我现在尝试通过文本框 (input$answer) 获取用户输入,基于该输入 (input$answer == "xyz") 进行条件测试,并根据此条件生成输出 (if { <do stuff for correct answer> } else { <do stuff for incorrect answer> })。
我想我已经掌握了很多基础知识。我可以获得用户输入,我已将其转换为反应值,因此我可以在 if 语句中生成查询而不会出错。我可以在提交答案后使用该用户输入生成输出。
不过有两个问题:
- 查询
(isolate(input$answer) == "Hello")永远不会为TRUE,因为isolare(input$answer)的值始终保持它最初分配的值。在当前情况下是“输入文本...”,但如果我将其留空,则行为不会改变(它只是假设“”作为值)。如果我将语句更改为if (isolate(input$answer) == "Enter text..."),则评估将始终为 TRUE。为什么即使在随后的paste0("Your answer of ",input$answer, " is incorrect!") })中该值已正确更新,该值也不会改变? - 有没有办法在服务器启动时防止正确/不正确的评估,只有在第一次点击提交按钮时才踢它?
library(shiny)
#// Define UI for game ----
ui <- fluidPage(
#// for query
fluidRow (
#// column width and title
column(6, h3("Question"),
h4("Type the word `Hello`"),
#// Input: Text Box
textInput("answer", h3("Text input"),
value = "Enter text..."),
#// submit button to terminate the text input
submitButton("Submit")
), #// end column
#// column width and title
column(6, h3("Evaluation"),
#// Output: Text ----
textOutput(outputId = "evaluation")
) #// end column
) #// end fluidrow
) # end fluidpage
# Define server logic ----
server <- function(input, output) {
#// set up variables
answer <- reactiveValues()
#// logic for correct vs. incorrect
if (isolate(input$answer) == "Hello") {
#// correct counter up by one
correct <- correct + 1
counter <- counter + 1
#// answer is correct
output$evaluation <- renderText({
paste0("Your answer of `",input$answer, "` is correct!") })
} #// end if
else {
#// answer is not correct
output$evaluation <- renderText({
paste0("Your answer of `",input$answer, "` is incorrect!") })
counter <- counter + 1
} #// end else
#// stop app if count reaches number of games
if (counter == num) stopApp()
} #// end server
# Run the app ----
counter <- 0
num <- 10
shinyApp(ui = ui, server = server)
【问题讨论】: