【问题标题】:With shiny selectize, avoid sorting of search results使用闪亮的选择,避免对搜索结果进行排序
【发布时间】:2019-07-12 06:37:30
【问题描述】:

在下面的可重现闪亮应用程序中,可搜索的选择字段按字符串的长度重新排序值。

如果我在搜索字段中键入1,则“Gears”会出现在“Cylinders”上方,因为字符串更短。 但是,我希望它们按原始顺序排列,即 11 高于 12 高于 13。

selectize repo 中的一个线程建议添加类似sortField: [{field: 'name', direction: 'asc'}] 的内容,但我无法在闪亮的上下文中添加它。所以将options = list(sortField = list(field = 'name', direction = 'asc'))添加到selectizeInput()是没有效果的。

library(shiny)
choices <- c(
  "11 Cylinders" = "cyl",
  "12 Transmission" = "am",
  "13 Gears" = "gear"
)

shinyApp(
  ui = fluidPage(
    selectizeInput(
      "variable", 
      "Variable:", 
      choices
    )
  ),
  server = function(input, output) {
  }
)

【问题讨论】:

  • @Shree,你输入了“1”吗?
  • @ismirsehregal 抱歉,现在我明白了这个问题。谢谢!
  • 你可能想看看@yonicd 的更新答案(不幸的是,你不会在 SO 上收到通知)

标签: r shiny


【解决方案1】:
library(shiny)

# must have named vector for selectize.js to pick up on the injection
choices <- c(
  "11 Cylinders" = "cyl",
  "12 Transmission" = "am",
  "13 Gears" = "gear"
)

# define JS to inject for options
##asceding order
sort_asc <- I("[{field: 'name', direction: 'asc'},{field: '$score'}]")

##decending order
sort_desc <- I("[{field: 'name', direction: 'desc'},{field: '$score'}]")

JS_opts <- list(create=TRUE,
                labelField =  'name',
                searchField = 'name',
                sortField = sort_asc
                )

shinyApp(
  ui = fluidPage(
    selectizeInput(
      inputId = "variable", 
      label = "Variable:", 
      choices = choices,
      options = JS_opts
    )
  ),
  server = function(input, output) {
  }
)

【讨论】:

  • 请解释您的代码 sn-p 如何解决 OP 的问题。
  • 这会产生一个命名列表,可以在没有 split 的情况下将其定义为 choices &lt;- list( "11 Cylinders" = "cyl", "12 Transmission" = "am", "13 Gears" = "gear" ) - 但是传递命名列表而不是命名向量对所描述的行为没有任何影响。
  • 你是对的,命名列表没有帮助。我更新了答案以提供可重现的解决方案。
  • 之后,您的编辑工作正常,感谢分享!
猜你喜欢
  • 2011-10-16
  • 2015-12-31
  • 1970-01-01
  • 2017-10-27
  • 2014-02-21
  • 1970-01-01
  • 2013-04-28
  • 2016-05-21
  • 2021-08-19
相关资源
最近更新 更多