Reputation: 586
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
Upvotes: 28
Views: 28030
Reputation: 11
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.
Upvotes: 1
Reputation: 192
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.
Upvotes: 1
Reputation: 1933
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)))
Upvotes: 37