选择闪亮的样式,避免对搜索结果进行排序

时间:2019-06-27 18:44:49

标签: r shiny

在下面的可复制的闪亮应用程序中,可搜索的selectize字段按照字符串的长度对值进行重新排序。

enter image description here

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

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) {
  }
)

1 个答案:

答案 0 :(得分:-2)

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) {
  }
)