Search code examples
rdt

How to change alignment of columns in datatable?


I am creating a dataTableOutput in a shiny dashboard. All the columns are left aligned by default and I want to change it to center align. I am using the following code in my server.R file for the same:

output$data<- renderDataTable({data()},
                            options = list(
                             columnDefs = list(list(className = 'dt-center', targets = '_all'))))

This code is not showing any error but the alignment is not changing. I have searched a lot but the only way to do this seems to be the one I am using. Is there any way to change the alignment?


Solution

  • From the helper functions section on DT documentation:

    You can pass arbitrary CSS properties to formatStyle()

    See ?formatStyle for additional info.

    library(DT)
    
    datatable(diamonds[1:10,1:3]) %>%
      formatStyle('cut', `text-align` = 'center')
    

    enter image description here