Hide certain columns in a responsive data table using DT package

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

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.

Tags:

R

Dt

Shiny