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.
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', '$')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With