Hide certain columns in a responsive data table using DT package
Asked Answered
M

3

28

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

Mayfield answered 5/9, 2015 at 3:22 Comment(0)
P
37

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)))
Plastic answered 6/9, 2015 at 21:36 Comment(8)
is it possible to use ColVis to hide some of the columns by default and once the user checks the box display them?Brecher
Third section on the posted link mixed with the code above should do the trick.Plastic
Note that when you fill in colmuns2hide you should but in the column number. Not sure if you can put in the column name.Bluet
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 dataMccann
Note that DT's column index seems to start at 0, not 1, so it's match(columns2hide, colnames(iris))-1L.Gyp
hiding columns with columnDefs = list(list(visible=FALSE, targets=columns2hide) seems to be unstable. Any feedback from heavy users?Farfamed
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.Truda
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 columnsSanctimonious
S
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.

Sweetmeat answered 14/8, 2020 at 21:6 Comment(0)
D
1
options=list(columnDefs = list(list(visible=FALSE, targets=columns2hide)))

colums2hide <- c('col_name_1','col_name_2')

This is how to do it with specifying column names vs column numbers.

Denicedenie answered 2/11, 2023 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.