简体   繁体   中英

R Shiny DT - enter search text programmatically

Is it possible to enter search box text through code? Required behaviour is: user enters text into textInput('search2', "Search 2") and this text replicates in the DT search box and search is performed on the DT.

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    textInput('search2', "Search 2"),
    DTOutput('dt')
  ),
  server = function(input, output, session) {
    output$dt = renderDT(iris)
  })

在此处输入图片说明

I do not want to filter the DT data another way (which I'm doing currently) - specifically I'm looking to use the search box functionality of DT.

You can create a datatableProxy of your DT, which allows to manipulate an existing DT instance. Use the function updateSearch :

library(shiny)
library(DT)    

shinyApp(
  ui = fluidPage(
    textInput('search2', "Search 2"),
    DTOutput('dt')
  ),
  server = function(input, output, session) {

    DTproxy <- dataTableProxy("dt")
    output$dt = renderDT(iris)

    observeEvent(input$search2, {
      updateSearch(DTproxy, keywords = list(global = input$search2, columns = NULL))
    })

  })

在此处输入图片说明

In addition to @shosaco his answer:

Hide the searchbox of the DataTable by adding the following CSS:

.dataTables_filter {
visibility: hidden;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM