R : Add CSS color coding in a HTML table -
i need export data.frame html file , color backgroud of table cell based criteria.
sample data.frame :
name low high value1 value2 value3 ben 3 10 2 5 8 cat 3 10 3 9 4 dan 3 10 5 7 6
desire output :
i used below codes generate html, thank help
htmlheadertext ="sample report" htmlfile =htmlinitfile(outdir = getwd() , filename = "sample" , extension = "html" , title = "r output" , cssfile = paste(getwd(), "/html_tables.css", sep="") , htmlframe = false , usegrid = false , uselatex = false) html(htmlheadertext, file = htmlfile) html(dataset, row.names = false) htmlendfile()
i found solution need though may not best way it. added span in each cell , use jquery dynamically change background of cell.
htmlheadertext = "sample report" htmlfile =htmlinitfile(outdir = getwd() , filename = "sample" , extension = "html" , title = "r output" , cssfile = paste(getwd(), "/html_tables.css", sep="") , htmlframe = false , usegrid = true , uselatex = false) html('<script src="./jquery-1.7.2.min.js"></script>', file = htmlfile) html("<script> $(document ).ready(function() { $('.dataframe tr).each(function() { var low = $(this).find('td>span.low').text(); var high = $(this).find('td>span.high').text(); $(this).find('td>span.value').each(function() { $(this).css('color', 'white'); var value= $(this).text(); if (value> low && value< high) { $(this).parent().css('background-color', 'green'); } else { $(this).parent().css('background-color', 'red'); } cpk = number(math.round(cpk+'e'+4)+'e-'+4);; $(this).text(value); }); }) }); </script>", file = htmlfile) html(htmlheadertext, file = htmlfile) html(dataset, row.names = false, digit=5) htmlendfile()
Comments
Post a Comment