Build a DataTables-Compatible HTML Table
build_datatable_html.RdGenerates a complete <table> HTML string suitable for use with the
DataTables JavaScript library. Each column is
described via col_spec, which maps display headers to data columns and
optionally to a data-search attribute for per-cell searching.
Usage
build_datatable_html(
data,
col_spec,
tbl_id = NULL,
tbl_attr = list(),
header_wrap = NULL,
indent = 0L,
replace_na = ""
)Arguments
- data
A data frame containing the table data.
- col_spec
A named list defining each table column. Each name becomes the
<th>header text. Each element is a list with:content: name of the column indataused as<td>content (may be pre-rendered HTML).search: (optional) name of the column indataused as thedata-searchattribute value.NAvalues are always omitted (thedata-searchattribute is dropped entirely), regardless ofreplace_na.td_attr: (optional) named list of additional static attributes applied to every<td>in the column.
- tbl_id
The
idattribute for the<table>element, as a string.- tbl_attr
A named list of additional attributes for the
<table>element (e.g.list(style = "width:100%;", class = "display")).- header_wrap
An optional length-2 character vector
c(prefix, suffix)wrapping each<th>text. Useful for template engine syntax such as Babel/Jinja2, e.g.c('{{ _("', '") }}').- indent
Either an integer giving the starting indent level for the
<table>tag (each level adds two spaces; defaults to0L), or anhtml_indentobject as returned byget_html_indent()to match the indentation style of an existing HTML file.- replace_na
A string to replace
NAvalues with, orNULLto leave them as-is.