KeshetE
KeshetE

Reputation: 395

Rename columns in Shiny dashboard datatable

I have a datatable in my Shiny app. By default, the column names are the column names of the data set. I want to change the column names of the display, without touching the data set itself.

I found this documentation which is exactly what I need, but I'm not sure how to convert this to R syntax.

This is the current way I render the table:

output$score_data_table <- renderDataTable({
    selectedArea_overview_TC()}, 
    options = list(orderClasses = TRUE, 
                   lengthMenu = list(c(15,25,50,100,-1), c('15','25','50','100','All')),
                   pageLength = 15,
                   order=list(1, 'desc'))
)

I've tried adding the columnDefs option in several ways but nothing worked.

Upvotes: 4

Views: 7931

Answers (2)

ARobertson
ARobertson

Reputation: 2897

I fell in the same trap. The documentation you actually need is columns.title.

output$score_data_table <- renderDataTable({
  selectedArea_overview_TC()
  },
  options = list(orderClasses = TRUE,
                 lengthMenu = list(c(15,25,50,100,-1), c('15','25','50','100','All')),
                 pageLength = 15,
                 order=list(1, 'desc')
                 columns = list(
                   list(title = 'newnameforcol1'),
                   NULL, # skip column 2
                   list(title = 'newnameforcol3'),
                 )
  )
)

Title might work with columnDefs as well. I'm not sure.

Upvotes: 4

user2109247
user2109247

Reputation: 71

You can use colnames in the renderDataTable. Something like:

  output$table1 <- DT::renderDataTable({
     datatable(messages(),
     colnames = c('Type', 'Message', 'Check', 'Entity',   'ID','File'), 
     options = list(pageLength = 50, autoWidth = TRUE,
     columnDefs = list(list(width = '800px', targets = c(2)))),filter='top')})  

Upvotes: 7

Related Questions