manu p
manu p

Reputation: 985

Creating a function of DT table in shiny

I am trying to create a function for DT table where just specifying the columns name in the parameter, the column should get hidden

dt_table <- function(data, 
                     colhidden = c(a)){
  datatable(data, 
            options = list(
              columnDefs = list(
                list(visible=FALSE, targets=colhidden)
              )
            ))
}
 dt_table(iris,colhidden = c('Species'))

But unfortunately, the column is not getting hidden. Can anyone help me?

Upvotes: 0

Views: 28

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389215

targets needs the column number which you can get with match. Try -

library(DT)
dt_table <- function(data, colhidden) {
datatable(data, 
          options = list(
            columnDefs = list(
              list(visible=FALSE, targets=match(colhidden, colnames(data))))
            )
          )
}

dt_table(iris,colhidden = c('Species'))
dt_table(iris,colhidden = c('Species', 'Sepal.Length'))

Upvotes: 2

Related Questions