26

I am trying to create a responsive data table for my shiny application using DT package. I want to hide certain columns in advance. For example:

library("shiny")
library("DT")
shinyApp(
  ui = fluidPage(DT::dataTableOutput('tbl')),
  server = function(input, output) {
    output$tbl = DT::renderDataTable(
      iris,extensions="Responsive"
    )
  }
)

This output gives me 5 columns. It only hides columns when I narrow the page. But, I want to hide last 3 columns in advance and I just want to see first two columns every time. Is there a way to do that?

Update:

Example output

enter image description here

skorkmaz
  • 566
  • 1
  • 6
  • 12

2 Answers2

36

You can hide columns in your table using DT options or extensions.

If you want them to be hidden in advance but have a button to make them visible again, the ColVis extension should work well for you: link

If you just want thme stay hidden, add the following option (can't remember where I've seen its documentation right now..)

options=list(columnDefs = list(list(visible=FALSE, targets=columns2hide)))
user5029763
  • 1,903
  • 1
  • 15
  • 23
  • is it possible to use ColVis to hide some of the columns by default and once the user checks the box display them? – Rain Man Feb 21 '16 at 00:39
  • Third section on the posted link mixed with the code above should do the trick. – user5029763 Mar 07 '16 at 14:28
  • 4
    Note that when you fill in `colmuns2hide` you should but in the column number. Not sure if you can put in the column name. – John Paul Nov 01 '16 at 13:20
  • 2
    you can’t, you have to do `match(columns2hide, colnames(iris))`, maybe with a `na.omit` wrapped around it if `columns2hide` could contain a column not available in the data – flying sheep Aug 31 '17 at 15:07
  • 10
    Note that DT's column index seems to start at 0, not 1, so it's `match(columns2hide, colnames(iris))-1L`. – lukeA Jan 06 '18 at 20:35
  • hiding columns with `columnDefs = list(list(visible=FALSE, targets=columns2hide)` seems to be unstable. Any feedback from heavy users? – Lazarus Thurston Feb 11 '19 at 17:46
  • 2
    The column index can catch you off guard. It start from 0 but row name is counted as column 0. So the index depend on if you enabled row names. – dracodoc Jul 11 '19 at 18:17
  • I am trying to make the same thing work but am having issues. I am not sure what I should pass to targets. The data that is being rendered is a reactive object named "dataInput". I would like to hide multiple columns – wraymond Sep 15 '21 at 19:20
1

I have another way which I like for its readability. It does not solve the problem of column numbering though.

library("shiny")
library("DT")
library(magrittr)
columns2hide <- match('Sepal.Width', colnames(iris))
shinyApp(
  ui = fluidPage(DT::dataTableOutput('tbl')),
  server = function(input, output) {
    output$tbl = DT::renderDataTable(
      {
        dataTableProxy(outputId = 'tbl') %>%
          hideCols(hide = columns2hide)
        iris
      },
      extensions="Responsive"
    )
  }
)

dataTableProxy creates a proxy object that you can operate on with a couple of functions (see ?dataTableProxy). It can be handy for hiding/showing/selecting/add/... rows and columns of the table when clicking on a button, for example. Because it has deferUntilFlush = TRUE by default it waits with its handling of the table until its next generation. In this case this simply happens on the following line.

AdagioMolto
  • 143
  • 9