如何使两个selectizeInputs具有相同向量的动态选择,但要避免重复选择?

huangapple go评论56阅读模式
英文:

How to have dynamic choices in two selectizeInputs from the same vector but you want to avoid duplicate choices?

问题

以下是翻译好的部分:

我有一个闪亮的应用程序,其中有两个具有相同选择项的selectizeinputs,这些选择项来自共同的向量。在我的虚拟示例中,我使用alpha、beta等。我的目标是,从一个输入中排除已选择的选项,以防止在两个输入中都选择相同的选项。因此,如果在第一个输入中选择了alpha,那么在第二个输入中,alpha将从可能的选项中删除,因此不可能在两个输入中都选择alpha。

到目前为止,这是我的尝试,但是选择项仍然在alpha和beta之间交替。

我还尝试将“Alpha”设置为第一个输入中的默认选项,但如果我随后在第一个输入中选择不同的选项,然后在第二个输入中选择不同的选项,它将重新默认为“Alpha”在第一个输入中。

# 以下是代码示例
library(shiny)

ui <- fluidPage(
  
  selectizeInput("choice_1", label = "Choice 1", choices = NULL, selected = NULL),
  selectizeInput("choice_2", label = "Choice 2", choices = NULL, selected = NULL),

)

server <- function(input, output, session) {
  
  levels <- c("Alpha", "Beta", "Charlie", "Delta", "Echo")
  
  observeEvent(input$choice_2, {
    updateSelectizeInput(session,
                         "choice_1",
                         choices = levels[levels != input$choice_2])
  })
  
  observeEvent(input$choice_1, {
    updateSelectizeInput(session,
                         "choice_2",
                         choices = levels[levels != input$choice_1])
  })


}

# 运行应用程序
shinyApp(ui = ui, server = server)
英文:

I have a shiny app which has two selectizeinputs with identical choices from a common vector. In my dummy example, I use alpha, beta, .. echo. What I want, is to exclude a selected choice from one input from the other. So if alpha is selected in the first input, then alpha would be removed from the possible choices in the second input so it wouldn't be possible to choose alpha in both.

This is what I've attempted so far, however the choices just keeps alternating between alpha and beta.

I have also tried setting "Alpha" as the selected default input in the first input, but if I than select a different choice in the first input, than a different choice in the second input, it defaults back to "Alpha" in the first input.

library(shiny)

ui &lt;- fluidPage(
  
  selectizeInput(&quot;choice_1&quot;, label = &quot;Choice 1&quot;, choices = NULL, selected = NULL),
  selectizeInput(&quot;choice_2&quot;, label = &quot;Choice 2&quot;, choices = NULL, selected = NULL),

)


server &lt;- function(input, output, session) {
  
  levels &lt;- c(&quot;Alpha&quot;, &quot;Beta&quot;, &quot;Charlie&quot;, &quot;Delta&quot;, &quot;Echo&quot;)
  
  observeEvent(input$choice_2, {
    updateSelectizeInput(session,
                         &quot;choice_1&quot;,
                         choices = levels[levels != input$choice_2])
  })
  
  observeEvent(input$choice_1, {
    updateSelectizeInput(session,
                         &quot;choice_2&quot;,
                         choices = levels[levels != input$choice_1])
  })


}

# Run the application 
shinyApp(ui = ui, server = server)

答案1

得分: 1

避免无限循环的一种选项是设置 selected= 参数。问题在于当您的应用程序启动时,两个 selectizeInput 都会初始化为所有可用选项,当 selected=NULL 时,默认选择了第一个元素,即 Alpha,这会触发下一轮更新,现在对于两者来说,Alpha 都从选择中被移除,选择的元素都切换到了 Beta,依此类推。

library(shiny)

ui <- fluidPage(
  selectizeInput("choice_1", label = "Choice 1", choices = NULL, selected = NULL),
  selectizeInput("choice_2", label = "Choice 2", choices = NULL, selected = NULL),
)

server <- function(input, output, session) {
  levels <- c("Alpha", "Beta", "Charlie", "Delta", "Echo")

  observeEvent(input$choice_2, {
    updateSelectizeInput(session,
      "choice_1",
      choices = levels[levels != input$choice_2],
      selected = input$choice_1
    )
  })

  observeEvent(input$choice_1, {
    updateSelectizeInput(session,
      "choice_2",
      choices = levels[levels != input$choice_1],
      selected = input$choice_2
    )
  })
}

shinyApp(ui = ui, server = server)

[![enter image description here][1]][1]


<details>
<summary>英文:</summary>

One Option to avoid the infinite cycle would be to set the `selected=` argument. The issue is that when your app starts, both `selectizeInput`s are initialized with all available choices and when `selected=NULL` the first element, i.e. `Alpha`, gets selected by default for both which triggers the next update round in which now `Alpha` gets dropped from the choices for both and the selected element switches to `Beta` for both and so on and so forth.

library(shiny)

ui <- fluidPage(
selectizeInput("choice_1", label = "Choice 1", choices = NULL, selected = NULL),
selectizeInput("choice_2", label = "Choice 2", choices = NULL, selected = NULL),
)

server <- function(input, output, session) {
levels <- c("Alpha", "Beta", "Charlie", "Delta", "Echo")

observeEvent(input$choice_2, {
updateSelectizeInput(session,
"choice_1",
choices = levels[levels != input$choice_2],
selected = input$choice_1
)
})

observeEvent(input$choice_1, {
updateSelectizeInput(session,
"choice_2",
choices = levels[levels != input$choice_1],
selected = input$choice_2
)
})
}

shinyApp(ui = ui, server = server)


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/DMFv2.png

</details>



huangapple
  • 本文由 发表于 2023年3月8日 18:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定