Remove 'search' option but leave 'search columns' option
Asked Answered
S

5

58

I would like to remove 'global search' option from my application, but leave 'column search' option. Any ideas? I've tried different paramethers like searching=FALSE, filtering='none'... None of this works properly.

My code:

server.R:

library("shiny")
library("DT")

data(iris)

shinyServer(function(input, output) {

    output$tabelka <- DT::renderDataTable({

        datatable(iris, filter="top", selection="multiple", escape=FALSE)

    })

})

ui.R

library("shiny")
library("DT")

shinyUI(fluidPage(

    DT::dataTableOutput("tabelka")

))

And picture which helps to understand my problem:

enter image description here

Stratum answered 25/2, 2016 at 10:18 Comment(4)
Take a look here, section 2.5.Boorman
@danas.zuokas, I've read this manual, but it didn't help me. Could you give me more specyfic clue?Stratum
Well I don't have the answer myself, thought it would lead you in the right direction. What I understand is that you can build up a table as you wish using custom container. I suggest reproducing the example and adapting it to your needs.Boorman
For those finding this on Google (like I just did): to hide the search/filter without disabling the actual API functionality in the regular HTML/JS web setup, use the dom option. SourceRadiomicrometer
I
45

DT options needs to be passed as a list. Further, by using the sDom initialisation variable it is possible to specify where in the DOM controls are placed. The standard setup looks like this:

    datatable(iris, filter="top", selection="multiple", escape=FALSE, 
      options = list(sDom  = '<"top">flrt<"bottom">ip'))

The syntax is a bit quirky, but basically the above says that f, l, r and t options are to be placed in the top div with the i and p options in the bottom div. Please refer to the docs at http://legacy.datatables.net/usage/options for a more thorough explanation.

Anyways, "f" is the "Filtering input" option (i.e. the search option) and by moving "f" to another div or omitting it we may move around or disable the search bar. A solution to your problem might look like this:

    datatable(iris, filter="top", selection="multiple", escape=FALSE, 
      options = list(sDom  = '<"top">lrt<"bottom">ip'))
Innerdirected answered 25/2, 2016 at 12:17 Comment(9)
Yes, I've tried it. 'Global search' disapears, but 'column search' does not work either...Stratum
Strange! The above works as expected for me. What versions of R, shiny and DT are you running? Maybe the dev version works better: devtools::install_github('rstudio/DT')Innerdirected
There are filtering boxes for columns, but they don't work (try, if it is the same in your version). My DT is quite new, I think.Stratum
This doesn't work for me either (DT_0.1.40). The global search disappears as expected but when I try to filter the columns whatever is displayed doesn't change (most likely a bug).Beneficiary
Yeah, my bad, as it turns out it doesn't work here either. Looks ok but nothing happens when filters are applied..Innerdirected
Turns out it is not an error, the searching option governs both search and filtering. datatables.net/reference/option. Using the "sDom" option it is possible to move around (or remove) elements. Have modified my answer to reflect this.Innerdirected
Thanks! Could you try to explain what exactly this code does. Is it written in Java?Stratum
No, worries! In the process of building a shiny application at work where DT is used quite extensively. Were looking for a solution to a similar issue when i stumbled on your question :)Innerdirected
why not to use bFilter=0 in options?Lignin
R
63

Slightly simpler syntax, for anyone else still looking:

datatable(head(iris), options = list(dom = 't'), filter = list(position = "top"))

Other options. To display table only, use dom = 't':

datatable(head(iris), options = list(dom = 't'))

To display table and filter (search box), the default setting:

datatable(head(iris), options = list(dom = 'ft'))

Source:

https://rstudio.github.io/DT/options.html

Robbegrillet answered 20/2, 2017 at 16:15 Comment(2)
This may turn off more features than desired, such as the page buttons.Cannonry
@Cannonry then use "tp" to turn on pagination. See legacy.datatables.net/usage/options#sDom for more options.Hilaria
I
45

DT options needs to be passed as a list. Further, by using the sDom initialisation variable it is possible to specify where in the DOM controls are placed. The standard setup looks like this:

    datatable(iris, filter="top", selection="multiple", escape=FALSE, 
      options = list(sDom  = '<"top">flrt<"bottom">ip'))

The syntax is a bit quirky, but basically the above says that f, l, r and t options are to be placed in the top div with the i and p options in the bottom div. Please refer to the docs at http://legacy.datatables.net/usage/options for a more thorough explanation.

Anyways, "f" is the "Filtering input" option (i.e. the search option) and by moving "f" to another div or omitting it we may move around or disable the search bar. A solution to your problem might look like this:

    datatable(iris, filter="top", selection="multiple", escape=FALSE, 
      options = list(sDom  = '<"top">lrt<"bottom">ip'))
Innerdirected answered 25/2, 2016 at 12:17 Comment(9)
Yes, I've tried it. 'Global search' disapears, but 'column search' does not work either...Stratum
Strange! The above works as expected for me. What versions of R, shiny and DT are you running? Maybe the dev version works better: devtools::install_github('rstudio/DT')Innerdirected
There are filtering boxes for columns, but they don't work (try, if it is the same in your version). My DT is quite new, I think.Stratum
This doesn't work for me either (DT_0.1.40). The global search disappears as expected but when I try to filter the columns whatever is displayed doesn't change (most likely a bug).Beneficiary
Yeah, my bad, as it turns out it doesn't work here either. Looks ok but nothing happens when filters are applied..Innerdirected
Turns out it is not an error, the searching option governs both search and filtering. datatables.net/reference/option. Using the "sDom" option it is possible to move around (or remove) elements. Have modified my answer to reflect this.Innerdirected
Thanks! Could you try to explain what exactly this code does. Is it written in Java?Stratum
No, worries! In the process of building a shiny application at work where DT is used quite extensively. Were looking for a solution to a similar issue when i stumbled on your question :)Innerdirected
why not to use bFilter=0 in options?Lignin
A
31
datatable(iris, filter="top", selection="multiple", escape=FALSE,
options = list(searching = FALSE))
Aranyaka answered 16/1, 2018 at 14:27 Comment(5)
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made. refOutfox
All the suggestions above don't get rid of the column space used by the search input form that was removed. The big "WHY" to remove the global search was to release the crucial column space on a full-width screen. Any hopes of getting that space released in shiny?Insuperable
@Stratum I think you should accept this as the correct answer.Hilaria
This disables searching for the entire table i.e. it stops column filters from working, too. The correct way is explained below by Yeahman269Babb
@Babb yeahman's answer is disabling my filter tooEugine
F
9

You have to act on the dom options of DT::datatable. The following works fine for me:

datatable(iris, filter="top", selection="multiple", escape=FALSE, options = list(dom = 'ltipr'))

By default DT::datatable has dom = 'lftipr', simply drop the 'f' which is for the filter ("search" button).

Learn more on dom here: https://datatables.net/reference/option/dom

Fruity answered 22/5, 2020 at 9:39 Comment(0)
M
0

According on what @Alex Nevsky commented, his solution is the best for me! I tried @Pewi solution, but it deleted me the download buttons.

I tried bFilter=0 with your example and it totally works!

library("shiny")
library("DT")

data(iris)

ui <- fluidPage(
  
  DT::dataTableOutput("tabelka")
  
)


server <- function(input, output) {
  
  output$tabelka <- DT::renderDataTable({
    
    datatable(iris, 
              filter="top", 
              selection="multiple", 
              escape=FALSE,
              options = list(bFilter=0))
  })
}


shinyApp(ui=ui, server=server)

Warning, if you want to filter your dataframe (filter="top"), this command is not good for you.

Markham answered 3/6, 2021 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.