knitr - How to get R to output body scripts to format HTML tables using a similar approach as gvisTable? -
i work out how generate formatted html tables in way not require use of styles placed in html header. i've broadly asked question already, received 1 answer mentioned using header , answer involves using pandoc (update: see bottom of post regarding new answer posted since asking question. i'd have r function writes html formatting required formatted html table in 1 place.
i've been playing around gvistable , capable of writing relevant information in 1 place:
the following code
```{r results='asis'} simpledata <- data.frame(matrix(1:9, nrow=3)) tab2 <- gvistable(simpledata, options=list(width = 600, height = 300)) print(tab2, "chart") ``` will output following r markdown document:
<!-- jsheader --> <script type="text/javascript" src="http://www.google.com/jsapi"> </script> <script type="text/javascript"> // jsdata function gvisdatatableid273f3d05cccd () { var data = new google.visualization.datatable(); var datajson = [ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]; data.addcolumn('number','x1'); data.addcolumn('number','x2'); data.addcolumn('number','x3'); data.addrows(datajson); return(data); } // jsdrawchart function drawcharttableid273f3d05cccd() { var data = gvisdatatableid273f3d05cccd(); var options = {}; options["allowhtml"] = true; options["width"] = 600; options["height"] = 300; var chart = new google.visualization.table( document.getelementbyid('tableid273f3d05cccd') ); chart.draw(data,options); } // jsdisplaychart function displaycharttableid273f3d05cccd() { google.load("visualization", "1", { packages:["table"] }); google.setonloadcallback(drawcharttableid273f3d05cccd); } // jschart displaycharttableid273f3d05cccd() <!-- jsfooter --> //--> </script> <!-- divchart --> <div id="tableid273f3d05cccd" style="width: 600px; height: 300px;"> </div> and when placed in context, gvistable produced. see the second table in output.
thus, simple r function has outputted necessary html creation of sophisticated table. i'd have same degree of table formatting control seen in latex, r markdown. , given such posts appear on blogs, through syndication, in rss readers, , on formatting commands in body.
questions
- is possible use approach similar gvis (e.g., scripts in html body) format html tables (e.g., lines, cell formatting, cell height , width, fonts, etc.)?
- are there existing r functions support production of such tables?
- do assumptions of question make sense, or there other way achieve broader aims of precise html table formatting r markdown?
update
joe has added answer previous question mentions 3 options (style in body; javascript embeds style in header; , scoped style blocks). guess main question remains whether there interfaces exist faciliate using such approaches r markdown code blocks.
from rstudio docs, can set knitr use custom stylesheet using:
# set custom css options(rstudio.markdowntohtml= function(inputfile, outputfile) { require(markdown) markdowntohtml(inputfile, outputfile, stylesheet='custom.css') } ) just replace stylesheet='custom.css' file path stylesheet.
then, can combine xtable , printwith option type="html" in chunk , you'll well-formatted table.
xtable produces latex if that's desirable.
Comments
Post a Comment