在DT datatable中使用liveSearch的shinyWidget pickerInput

wkyowqbh  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(92)

我想将shinyWidgets中的pickerInputs插入到Shiny应用程序的DT数据表中。但是,当在数据表中呈现时,liveSearch选项不适用。此外,当我在datatable之外有一个单独的pickerInput时(下面取消注解fluidPage()内的pickerInput行),表内的pickerInputs不会呈现。我很感激你能帮我把这个弄出来。

library(shiny)
library(DT)
library(shinyWidgets)

shinyApp(
  ui = fluidPage(
    # pickerInput(inputId = "test", label = "Test", choices = c("option1", "option2")),
    DT::dataTableOutput("mytable")
  ),
  server = function(input, output, session) {
    df <- data.frame(
      col1 = as.character(pickerInput(
        inputId = "Picker",
        choices = c("option1", "option2", "option3", "zebra"),
        options = pickerOptions(liveSearch = T)
      ))
    )
    output$mytable <- DT::renderDataTable({
      DT::datatable(
        data = df,
        escape = F
      ) 
    })
  }
)

字符串

lp0sw83n

lp0sw83n1#

一种解决方案是将DT表直接放到UI中:

library(shiny)
library(DT)
library(shinyWidgets)

df <- data.frame(
  col1 = as.character(pickerInput(
    inputId = "Picker",
    label = "Picker",
    choices = c("option1", "option2", "option3", "zebra"),
    options = pickerOptions(liveSearch = TRUE)
  ))
)

shinyApp(
  ui = fluidPage(
    
    pickerInput(
      inputId = "test", 
      label = "Test", 
      choices = c("option1", "option2"),
      options = pickerOptions(liveSearch = TRUE)
    ),
    
    datatable(
      data = df,
      escape = F
    )
  ),
  server = function(input, output, session) { }
)

字符串
请注意,您必须在UI中放置另一个选择器输入(例如"test"),以便加载JavaScript依赖项。如果你不想要这个选择器输入,你可以用一些CSS(display: none;)隐藏它。


的数据

编辑:服务器端

library(shiny)
library(DT)
library(shinyWidgets)

shinyApp(
  ui = fluidPage(
    pickerInput(inputId = "test", label = "Test", choices = c("option1", "option2")),
    DTOutput("mytable")
  ),
  server = function(input, output, session) {
    df <- data.frame(
      col1 = as.character(pickerInput(
        inputId = "myPicker",
        choices = c("option1", "option2", "option3", "zebra"),
        options = pickerOptions(liveSearch = TRUE)
      ))
    )
    output$mytable <- renderDT({
      datatable(
        data = df,
        escape = FALSE,
        options = list(
          initComplete = JS('function() { $("#myPicker").selectpicker(); }')
        )
      ) 
    })
  }
)

相关问题