虽然选择没有改变,但通过update函数触发selectInput事件

问题描述 投票:0回答:0

我想实现一个观察者,它使用

selectInput
对用户和更新函数 (
bindEvent
) 触发的事件做出反应。虽然这在大多数情况下都可以正常工作,但当更新的选择与之前的选择没有区别时,
updateSelectInput
不会触发事件,即将选择从
c(1, 2, 3)
更改为
c(1, 2, 3)
不会导致事件触发。

这是一个带有评论的代表:

library(shiny)
library(magrittr)

ui <- shiny::fluidPage(
  shiny::selectInput("pick1", label = "Pick input", choices = c(1, 2, 3)),
  shiny::actionButton("button1", "Update 1"),
  
  shiny::selectInput("pick2", label = "Pick input", choices = c(1, 2, 3)),
  shiny::actionButton("button2", "Update 2")
)

server <- function(input, output, session) {
  # Notice that the updated choices are the same as the default ones, so
  # nothing changes and no event is triggered.
  observe({
    updateSelectInput(inputId = "pick1", choices = c(1, 2, 3))
  }) %>%
    bindEvent(input$button1)
  
  # This only executes when the user explicitly changes the selection as the
  # updater does not trigger input$pick1
  observe({
    message("Something has triggered pick1 (probably not the updater)")
  }) %>%
    bindEvent(input$pick1)
  
  # When the choices differ, the updater does trigger an event
  observe({
    updateSelectInput(inputId = "pick2", choices = sample(1:100, size = 3))
  }) %>%
    bindEvent(input$button2)
  
  # This executes when the user changes the selection AND when the selection
  # is updated on the server side
  observe({
    message("Something has triggered pick2 (possibly the updater)")
  }) %>%
    bindEvent(input$pick2)
}

runApp(list(ui = ui, server = server))

这是有道理的,但是当更新的选择是动态生成的并且恰好与以前相同时,它会导致不一致。即使实际上没有任何变化,有什么方法可以强制触发事件?我已经用

shinyWidgets::pickerInput()
尝试过
shinyWidgets::updatePickerInput(..., clearOptions = TRUE)
,但这似乎对我没有任何帮助。

r shiny observers selectinput
© www.soinside.com 2019 - 2024. All rights reserved.