如何在允许多个选择的情况下保持 pickerInput 的选择顺序?

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

How to preserve the selection order of a pickerInput if multiple selections are allowed?

问题

让我们以我的代码为例。如果我使用 selectInput 选项,在那里我可以选择多个选项,我选择的第一个选项将首先列出,第二个选项将列在第二位,依此类推。

但是,如果我使用 pickerInput 选项,无论我选择的顺序如何,下拉列表中首先列出的选项将首先列出。是否有一种方法让 pickerInput 模拟类似于 selectInput 的行为?

例如,如果我首先选择了 Name 1,然后选择了 Name 3,无论是 selectInput 还是 pickerInput 都会给我输出 Sunday;Tuesday

然而,如果我首先选择了 Name 3,然后选择了 Name 1,selectInput 会给出 Tuesday;Sunday,但 pickerInput 会给出 Sunday;Tuesday

如何确保 pickerInput 排列的输出类似于 selectInput

以下是代码部分:

  1. library(shiny)
  2. library(shinydashboard)
  3. library(shinyWidgets)
  4. choices_df = data.frame(
  5. names = c('Name 1', 'Name 2', 'Name 3'),
  6. id = c("Sunday","Monday","Tuesday")
  7. )
  8. ui <- dashboardPage(
  9. header = dashboardHeader(),
  10. sidebar = dashboardSidebar(),
  11. body = dashboardBody(
  12. selectInput(
  13. "input",
  14. h5("输出应该是选项名称,而不是其值"),
  15. choices= setNames(choices_df$id,choices_df$names),
  16. multiple = TRUE
  17. ),
  18. textOutput("output"),
  19. pickerInput(
  20. "input2",
  21. h5("输出应该是选项名称,而不是其值"),
  22. choices= setNames(choices_df$id,choices_df$names),
  23. multiple = TRUE
  24. ),
  25. textOutput("output2")
  26. )
  27. )
  28. server <- function(input, output, session) {
  29. output$output <- renderPrint({paste(input$input, collapse = ";")})
  30. output$output2 <- renderPrint({paste(input$input2, collapse = ";")})
  31. }
  32. shinyApp(ui = ui, server = server)
英文:

Let's take my code as an example. If I use selectInput option where I can pick multiple choices, the first choice that I picked would be listed first, second choice would be listed second, etc.

However, if I use pickerInput option where I would pick multiple choices, no matter the order in which I pick the choice, whatever is listed first in the dropdown would be listed first instead. Is there a way for the pickerInput to emulate something similar to what selectInput does?

As an example, if I happened to pick Name 1 first and then Name 3 second, both selectInput and pickerInput would give me an output of Sunday;Tuesday.

However, if I pick Name 3 first and then Name 1 second, selectInput would give Tuesday;Sunday, but pickerInput would give Sunday;Tuesday.

How do I make sure pickerInput ordered the output similar to selectInput?

Code is below:

  1. library(shiny)
  2. library(shinydashboard)
  3. library(shinyWidgets)
  4. choices_df = data.frame(
  5. names = c(&#39;Name 1&#39;, &#39;Name 2&#39;, &#39;Name 3&#39;),
  6. id = c(&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;)#seq(3)
  7. )
  8. ui &lt;- dashboardPage(
  9. header = dashboardHeader(),
  10. sidebar = dashboardSidebar(),
  11. body = dashboardBody(
  12. selectInput(
  13. &quot;input&quot;,
  14. h5(&quot;The output should give the choice name instead of its value&quot;),
  15. choices= setNames(choices_df$id,choices_df$names),
  16. multiple = TRUE
  17. ),
  18. textOutput(&quot;output&quot;),
  19. pickerInput(
  20. &quot;input2&quot;,
  21. h5(&quot;The output should give the choice name instead of its value&quot;),
  22. choices= setNames(choices_df$id,choices_df$names),
  23. multiple = TRUE
  24. ),
  25. textOutput(&quot;output2&quot;)
  26. )
  27. )
  28. server &lt;- function(input, output, session) {
  29. #output$output &lt;- renderPrint({paste(choices_df$names[choices_df$id==input$input])})
  30. output$output &lt;- renderPrint({paste(input$input, collapse = &quot;;&quot;)})
  31. output$output2 &lt;- renderPrint({paste(input$input2, collapse = &quot;;&quot;)})
  32. }
  33. shinyApp(ui = ui, server = server)

答案1

得分: 1

需要使用 pickerInput 吗?目前不可能,因为 pickerInput 使用的是 bootstrap-select 库,该库不包含这种灵活性。请查看 此处

一个没有使用 pickerInput 的解决方案是使用 virtualSelectInput

  1. virtualSelectInput(
  2. "input2",
  3. h5("输出应该显示选择项的名称而不是其值"),
  4. choices = prepare_choices(choices_df, names, id),
  5. multiple = TRUE
  6. )
英文:

Do you need to do it with pickerInput? At the moment, it's not possible since pickerInput uses the bootstrap-select library which doesn't include that flexibility. See here.

One solution without pickerInput is to use the virtualSelectInput.

  1. virtualSelectInput(
  2. &quot;input2&quot;,
  3. h5(&quot;The output should give the choice name instead of its value&quot;),
  4. choices = prepare_choices(choices_df, names, id),
  5. multiple = TRUE
  6. )

答案2

得分: 0

以下是您要翻译的内容:

您可以添加以下的JS代码,以保持pickerInput()的选择顺序,类似于selectInput()的行为。

  1. $(function(){
  2. $('#input2').on('change.bs.select loaded.bs.select',
  3. function(event) {
  4. $(this).find('option:selected').prependTo(this);
  5. });
  6. });

如何在允许多个选择的情况下保持 pickerInput 的选择顺序?

  1. library(shiny)
  2. library(shinydashboard)
  3. library(shinyWidgets)
  4. js <- HTML(
  5. "
  6. $(function(){
  7. $('#input2').on('change.bs.select loaded.bs.select',
  8. function(event) {
  9. $(this).find('option:selected').prependTo(this);
  10. });
  11. });
  12. "
  13. )
  14. choices_df = data.frame(
  15. names = c('Name 1', 'Name 2', 'Name 3'),
  16. id = c("Sunday", "Monday", "Tuesday")#seq(3)
  17. )
  18. ui <- dashboardPage(
  19. tags$head(tags$script(js)),
  20. header = dashboardHeader(),
  21. sidebar = dashboardSidebar(),
  22. body = dashboardBody(
  23. selectInput(
  24. "input",
  25. h5(
  26. "selectInput: 输出应该是选择的名称而不是其值"
  27. ),
  28. choices = setNames(choices_df$id, choices_df$names),
  29. multiple = TRUE
  30. ),
  31. textOutput("output"),
  32. pickerInput(
  33. "input2",
  34. h5(
  35. "pickerInput: 输出应该是选择的名称而不是其值"
  36. ),
  37. choices = setNames(choices_df$id, choices_df$names),
  38. multiple = TRUE
  39. ),
  40. textOutput("output2")
  41. )
  42. )
  43. server <- function(input, output, session) {
  44. #output$output <- renderPrint({paste(choices_df$names[choices_df$id==input$input])})
  45. output$output <-
  46. renderPrint({
  47. paste(input$input, collapse = ";")
  48. })
  49. output$output2 <-
  50. renderPrint({
  51. paste(input$input2, collapse = ";")
  52. })
  53. }
  54. shinyApp(ui = ui, server = server)
英文:

You could add the following JS in order to retain the selection order of a pickerInput() similar to the behaviour of a selectInput().

  1. $(function(){
  2. $(&#39;#input2&#39;).on(&#39;change.bs.select loaded.bs.select&#39;,
  3. function(event) {
  4. $(this).find(&#39;option:selected&#39;).prependTo(this);
  5. });
  6. });

如何在允许多个选择的情况下保持 pickerInput 的选择顺序?

  1. library(shiny)
  2. library(shinydashboard)
  3. library(shinyWidgets)
  4. js &lt;- HTML(
  5. &quot;
  6. $(function(){
  7. $(&#39;#input2&#39;).on(&#39;change.bs.select loaded.bs.select&#39;,
  8. function(event) {
  9. $(this).find(&#39;option:selected&#39;).prependTo(this);
  10. });
  11. });
  12. &quot;
  13. )
  14. choices_df = data.frame(
  15. names = c(&#39;Name 1&#39;, &#39;Name 2&#39;, &#39;Name 3&#39;),
  16. id = c(&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;)#seq(3)
  17. )
  18. ui &lt;- dashboardPage(
  19. tags$head(tags$script(js)),
  20. header = dashboardHeader(),
  21. sidebar = dashboardSidebar(),
  22. body = dashboardBody(
  23. selectInput(
  24. &quot;input&quot;,
  25. h5(
  26. &quot;selectInput: The output should give the choice name instead of its value&quot;
  27. ),
  28. choices = setNames(choices_df$id, choices_df$names),
  29. multiple = TRUE
  30. ),
  31. textOutput(&quot;output&quot;),
  32. pickerInput(
  33. &quot;input2&quot;,
  34. h5(
  35. &quot;pickerInput: The output should give the choice name instead of its value&quot;
  36. ),
  37. choices = setNames(choices_df$id, choices_df$names),
  38. multiple = TRUE
  39. ),
  40. textOutput(&quot;output2&quot;)
  41. )
  42. )
  43. server &lt;- function(input, output, session) {
  44. #output$output &lt;- renderPrint({paste(choices_df$names[choices_df$id==input$input])})
  45. output$output &lt;-
  46. renderPrint({
  47. paste(input$input, collapse = &quot;;&quot;)
  48. })
  49. output$output2 &lt;-
  50. renderPrint({
  51. paste(input$input2, collapse = &quot;;&quot;)
  52. })
  53. }
  54. shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年6月30日 01:50:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583489.html
匿名

发表评论

匿名网友

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

确定