我的应用程序中有两个 selectizeInput(state1
和 state2
),它们具有相同的选择集。当用户单击 actionButton 时,我想比较 state1
和 state2
的值,如果它们相同,则向用户显示反馈消息。
两个 selectizeInputs 均使用占位符“按名称搜索”进行初始化。如果我第一次更改一个输入的值并将另一个输入的值保持其占位符值不变,则单击操作按钮会触发反馈消息:
这让我感到困惑,因为只有当 state1
和 state2
相同时才应显示该消息。
这是重现上述内容的代码:
library(shiny)
library(shinyFeedback)
ui <- fluidPage(shinyFeedback::useShinyFeedback(),
selectizeInput('state1', 'Select state 1', choices = state.name,
multiple = T,
options = list(placeholder = "Search by name", maxItems = 1)),
selectizeInput('state2', 'Select state 2', choices = state.name,
multiple = T,
options = list(placeholder = "Search by name", maxItems = 1)),
actionButton("click", "Click"),
textOutput("states")
)
server <- function(input, output, session) {
values = reactiveValues(states = NULL)
observeEvent(input$click, {
feedbackDanger("state1", input$state1 == input$state2, "")
feedbackDanger("state2", input$state2 == input$state1, "State 1 and state 2 cannot be the same.")
req(!is.null(input$state1), !is.null(input$state2), input$state1 != input$state2)
values$states <- paste(input$state1, input$state2)
})
output$states <- renderText({
values$states
})
}
shinyApp(ui, server)
我认为这可能与在 selectizeInputs 中使用 multiple = T 和 maxItems = 1 有关,或者与我编写触发条件的方式有关,但我不明白为什么它们是错误的。任何帮助将不胜感激。
最佳答案
编辑:2019-02-13
该错误已在 shinyFeeback
的 GitHub 版本中得到解决。如果您从 GitHub 安装软件包,您的原始代码应该可以工作:
devtools::install_github("merlinoa/shinyFeedback")
--
问题
你的想法是正确的!在第一个示例中,input$state1
是“Alaska”,input$state2
为空。 multiple = T
的 selectizeInput
的初始值为 NULL
。
当您的 feedbackDanger
条件比较两个值时,结果是空逻辑:
> "Alaska" == NULL
logical(0)
空逻辑作为空对象传递给 shinyFeedback
javascript 函数 checkFeedback
。空对象是 truthy ,因此“在 bool 上下文中遇到时被视为true
”。因此,当 checkFeedback
计算值时:
if (message.condition) {
....
该语句返回true
并显示feedbackDanger
消息。
解决方案
一个简单的解决方案是首先检查任一值是否为NULL
,然后检查是否相等,如下所示:
((!is.null(input$state1) & !is.null(input$state2)) && (input$state1 == input$state2))
编辑:我们使用&&
,以便我们首先评估任一值是否为NULL
。仅当两个值都不为 NULL 时,我们才会评估这些值是否相同。按此顺序求值可避免将空逻辑发送到 JS 函数。请参阅this answer有关 &
与 &&
的更多信息。 /编辑
完整的工作解决方案如下所示:
library(shiny)
library(shinyFeedback)
ui <- fluidPage(shinyFeedback::useShinyFeedback(),
selectizeInput('state1', 'Select state 1', choices = state.name,
multiple = T,
options = list(placeholder = "Search by name", maxItems = 1)),
selectizeInput('state2', 'Select state 2', choices = state.name,
multiple = T,
options = list(placeholder = "Search by name", maxItems = 1)),
actionButton("click", "Click"),
textOutput("states")
)
server <- function(input, output, session) {
values = reactiveValues(states = NULL)
observeEvent(input$click, {
condition <- ((!is.null(input$state1) & !is.null(input$state2)) && (input$state1 == input$state2))
feedbackDanger("state1", condition, "")
feedbackDanger("state2", condition, "State 1 and state 2 cannot be the same.")
req(!is.null(input$state1), !is.null(input$state2), input$state1 != input$state2)
values$states <- paste(input$state1, input$state2)
})
output$states <- renderText({
values$states
})
}
shinyApp(ui, server)
关于R Shiny - 如果两个输入具有相同的值,如何显示反馈消息(使用shinyFeedback),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54563960/