matsuo_basho
matsuo_basho

Reputation: 3030

Change column names in DT package right before output to Shiny app

I would like to change the column names of a datable at the very last step before I output it in a Shiny app. The display names are quite long, and I do not want to change them while I'm manipulating the datatable. There are a lot more formatting changes to the datatable than below in my actual dataset.

Here is a dummy dataset:

library(DT)
test.df <- data.frame(a = runif(10), b = 21:20, c = 31:30, d = 31:40)
test.dt <- datatable(test.df) %>% formatPercentage('a', 0) %>% formatCurrency('c', '$')

Now, how would I change the column names to c('Col1', 'Col2', 'Col3', 'Col4')? Again, I would like this to be the last step before I output the datatable with a renderDataTable function.

If there's a way to create column aliases as opposed to changing the actual column names, that would work as well.

Upvotes: 17

Views: 19697

Answers (1)

Xiongbing Jin
Xiongbing Jin

Reputation: 12107

Just use the colnames argument of datatable. This only changes the display name, so you can still use the original column names in your formatting code.

test.dt <- datatable(test.df, colnames=c("aa", "cc")) %>% formatPercentage('a', 0) %>% formatCurrency('c', '$')

Upvotes: 20

Related Questions