【问题标题】:updateSelectInput without triggering eventupdateSelectInput 不触发事件
【发布时间】:2018-08-15 21:08:51
【问题描述】:

在我闪亮的应用程序中,我有两个 SelectInput 对象,它们的选择取决于两者(两个列表中可以有多个选择)。

当我与小部件 A 交互时,小部件 B 中的选择可能不再适用,因此我想相应地更新小部件 B 中的选择和选择。

有没有办法使用不触发事件的 updateSelectInput 更新选择和选择?换句话说,有没有办法避免使input$widgetB失效?

编辑:

下面的例子显示了我的问题。

我使用browser() 来跟踪反应链。如果小部件 B 仅选择了“X”,则小部件 A 应仅显示“A”和“B”作为选项(即“C”不再是选项)。同样,如果小部件 A 仅选择了“C”,则小部件 B 应仅显示“Y”和“Z”作为选项(即“X”不再是选项)。由于它的设置方式,反应链最终将所有选择恢复到初始设置。如果updateSelectInput 有办法在选择/选择更改时停止使相关小部件失效,我认为我不会遇到这个问题(请注意,如果我们替换当前选择,updateSelectInput 足够聪明,不会使相关小部件失效/selections 具有相同的值)。

有什么想法可以达到预期的结果吗?

library(shiny)

ui <- fluidPage(
  selectInput(
    "WidgetA",
    label = h3("Widget A"),
    choices = c("A", "B", "C"),
    selected = c("A", "B", "C"),
    multiple = TRUE
  ), 
  selectInput(
    "WidgetB",
    label = h3("Widget B"),
    choices = c("X", "Y", "Z"),
    selected = c("X", "Y", "Z"),
    multiple = TRUE
  ),
  tableOutput("table")
)

server <- function(input, output, session) {

  D <- data.frame(A = c("A","A","B","B","B","C","B","C","C","A"),
                  B = c("X","Y","Z","X","Y","Z","X","Y","Z","X"),
                  x = 1:10
                  )

  observeEvent(input$WidgetA,{
    # browser()

    B_choices <- unique(D$B[D$A %in% input$WidgetA])

    # Does not trigger update (i.e., it does not invalidate input$WidgetB) if choices AND selections remain the same
    updateSelectInput(session, "WidgetB", choices = B_choices, selected = B_choices)

    D_ <- D[D$A %in% input$WidgetA & D$B %in% input$WidgetB,]

    output$table <- renderTable(D_)
  })

  observeEvent(input$WidgetB,{
    # browser()

    A_choices <- unique(D$A[D$B %in% input$WidgetB])

    # Does not trigger update (i.e., it does not invalidate input$WidgetA) if choices AND selections remain the same
    updateSelectInput(session, "WidgetA", choices = A_choices, selected = A_choices)

    D_ <- D[D$A %in% input$WidgetA & D$B %in% input$WidgetB,]

    output$table <- renderTable(D_)
  })

}

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 寻求帮助时,您应该包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出。根本没有代码很难谈具体的解决方案。
  • 注意一定要了解你所需要的一切(没有可复制的最小示例),但似乎你正在寻找允许你停止反应的isolate 函数,请查看 Rstudio here 的文档跨度>
  • 感谢 cmets。我已经用一个具体的例子更新了这个问题。

标签: javascript r shiny


【解决方案1】:

您可以在事物的服务器端使用 renderUI 来呈现依赖于另一个值的 selectInput。然后,您使用 uiOutput 函数将其添加到您的 UI:

library(shiny)

ui <- fluidPage(

  selectInput(
    "widgetA",
    label = h3("Widget A"),
    choices = c(1,2,3,4)
  ),
  uiOutput("select2")
  )

server <- function(input, output) {
   output$select2 <- renderUI({
     if(input$widgetA %in% c(1,2)) {
    selectInput(
      "WidgetB",
      label = h3("Widget B"),
      choices = c("A", "B", "C")
    ) }
    else {
      selectInput(
        "WidgetB",
        label = h3("Widget B"),
        choices = c("X", "Y", "Z")
      )
    } 
  })
}
shinyApp(ui = ui, server = server)

【讨论】:

  • 感谢您的回答。我添加了一个示例,以便更具体、更清楚地说明我的问题是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
相关资源
最近更新 更多