stephan mc
stephan mc

Reputation: 65

creating conditional formated tables for html in R using knitr

I want to get my data.frame from shown as a html table, using java script dataTables for a nicer look. I want to use knitr for translating the data.frame from R into html. I know, there is the function kable to construct such a table. But there i dont know, maybe it is not possible, how to define, that some cell has, e.g., the background coloured red, another one yellow. Can this be done. The problem is, for using the javascript dataTables, I need an id for the html table I created with knitr...

Upvotes: 2

Views: 276

Answers (1)

junkka
junkka

Reputation: 553

It is possible to add a id to a html table with kable through the table.attr param.

kable(head(cars), format = 'html', table.attr = 'id="foo"') 

and then in JavaScript

$(document).ready(function() {
  $('#foo').dataTable();
} );

To directly create a dataTable from a data.frame use dTable from the rCharts package.

dTable(cars) 

Upvotes: 1

Related Questions