Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you format the header of a table produced using the R DT (datatables) package

Tags:

javascript

r

The R package DT uses the datatables JavaScript library to draw nice-looking tables. I can determine the formatting of the cells in the table using the formatStyle() function, but there does not seem to be a function to format the column headers. Is there a way to format the headers of the table, for example fonts, alignments etc?

Many questions about DT on Stack Overflow are specific to R Shiny, and I am not using R Shiny.

like image 277
Chris Facer Avatar asked Nov 25 '15 00:11

Chris Facer


People also ask

What is DT package in R?

The R package DT provides an R interface to the JavaScript library DataTables. R data objects (matrices or data frames) can be displayed as tables on HTML pages, and DataTables provides filtering, pagination, sorting, and many other features in the tables.

How do I make my Datatable header fixed while scrolling?

fixedHeader-floating"). scrollLeft( $(this). scrollLeft() ); } }); DataTables creates a new table as the fixedHeader when you scroll down, what I'm doing here is to detect when the user scrolls horizontally on the $('#example') table and then I use scrollLeft() on the fixedHeader to match the scroll position.

What is DT in R studio?

DT is an interface to the JavaScript library DataTables based on the htmlwidgets framework, to present rectangular R data objects (such as data frames and matrices) as HTML tables. You can filter, search, and sort the data in the table.


1 Answers

You may use the "initComplete" function in "options" to callback a javascript code directly. Try the following R code to format the column headers to a 12 pixels font size:

datatable(
    iris,
    options = list(
        initComplete = JS("function(settings, json) {$(this.api().table().header()).css({'font-size' : '12px'});}")
        )
)

There are many other examples at http://rstudio.github.io/DT/

Regards.

like image 182
Kirk Avatar answered Sep 28 '22 01:09

Kirk