Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dt

shiny

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.

like image 529
matsuo_basho Avatar asked May 12 '16 20:05

matsuo_basho


Video Answer


1 Answers

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', '$')
like image 77
Xiongbing Jin Avatar answered Sep 20 '22 01:09

Xiongbing Jin