jQuery DataTables

We can use the JavaScript library DataTables to generate enhanced tables in HTML. In the example below, we create a table for the mtcars data:

library(knitr)
kable(mtcars, 'html', table.attr='id="mtcars_table"')

Note we assigned an id to the table, and next we use the DataTables library to initialize the table and you will get an interactive table.

window.addEventListener('load', () => {
  $('#mtcars_table').DataTable();
});

By comparison, below is an ordinary table:

kable(head(mtcars), 'html')

This vignette is only a toy example. I'd recommend you to use the DT package instead: https://github.com/rstudio/DT



yihui/knitr documentation built on April 11, 2024, 2:29 a.m.