Centering columns and removing some columns simultaneously in DT: Shiny/R

Hi Experts,

In order to center all columns
we could use

datatable(head(iris, 20), options = list(
  columnDefs = list(list(className = 'dt-center', targets = 1:5)),
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))

In order to remove column : 5 we could

datatable(head(iris, 20), options = list(
  columnDefs = list(list(targets = 5, visible = FALSE)),
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))

But I would center all the column but remove specific columns in the middle

I tried

datatable(head(iris, 20), options = list(
  columnDefs = list(list(className = 'dt-center', targets = 1:5)),
 columnDefs = list(list(className = 'dt-center', targets = 5, visible = FALSE)),
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))

and

datatable(head(iris, 20), options = list(
  columnDefs = list(list(className = 'dt-center', targets = 1:5))
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
)) %>% (options = list(
  columnDefs = list(list(className = 'dt-center', targets = 5, visible = FALSE))
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))

But these 2 approaches not producing the expected result.
Could some one help me

Just add your code inside the same list

library(DT)

datatable(head(iris, 20), options = list(
  columnDefs = list(list(className = 'dt-center', targets = 1:5),
                    list(targets = 5, visible = FALSE)),
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.