Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting html table in R

Tags:

html

r

xtable

I would like to improve the look of an html table that I generate in R using the package xtable:

 library(xtable)

 html.table = xtable(<mydataframe>)
 digits(html.table) = 2

I print the table using:

 html.tab = print(html.table, type = "html", floating = FALSE)
 cat(html.tab, file = <html link>)

I would like to be able to justify the text in the table, modify the color of the header column, change the font, ...

Is there any way i can achieve that in R?

Thank you!

like image 680
Mayou Avatar asked Dec 16 '22 03:12

Mayou


1 Answers

with xtable you can also give a class or id (or inline css) in the <TABLE> tag with the html.table.attributes argument. an example:

print(xtable(head(iris, 10)), type = "html", include.rownames = F, 
      html.table.attributes="class='table-bordered'")

this returns:

<!-- html table generated in R 3.0.1 by xtable 1.7-3 package -->
<!-- Fri Jul 11 12:18:15 2014 -->
<TABLE class='table table-bordered'>
<TR> <TH> Sepal.Length </TH> <TH> Sepal.Width </TH> <TH> Petal.Length </TH> <TH> Petal.Width </TH> <TH> Species </TH>  </TR>
<TR> <TD align="right"> 5.10 </TD> <TD align="right"> 3.50 </TD> <TD align="right"> 1.40 </TD> <TD align="right"> 0.20 </TD> <TD> setosa </TD> </TR>
<TR> <TD align="right"> 4.90 </TD> <TD align="right"> 3.00 </TD> <TD align="right"> 1.40 </TD> <TD align="right"> 0.20 </TD> <TD> setosa </TD> </TR>
<TR> <TD align="right"> 4.70 </TD> <TD align="right"> 3.20 </TD> <TD align="right"> 1.30 </TD> <TD align="right"> 0.20 </TD> <TD> setosa </TD> </TR>
<TR> <TD align="right"> 4.60 </TD> <TD align="right"> 3.10 </TD> <TD align="right"> 1.50 </TD> <TD align="right"> 0.20 </TD> <TD> setosa </TD> </TR>
<TR> <TD align="right"> 5.00 </TD> <TD align="right"> 3.60 </TD> <TD align="right"> 1.40 </TD> <TD align="right"> 0.20 </TD> <TD> setosa </TD> </TR>
<TR> <TD align="right"> 5.40 </TD> <TD align="right"> 3.90 </TD> <TD align="right"> 1.70 </TD> <TD align="right"> 0.40 </TD> <TD> setosa </TD> </TR>
<TR> <TD align="right"> 4.60 </TD> <TD align="right"> 3.40 </TD> <TD align="right"> 1.40 </TD> <TD align="right"> 0.30 </TD> <TD> setosa </TD> </TR>
<TR> <TD align="right"> 5.00 </TD> <TD align="right"> 3.40 </TD> <TD align="right"> 1.50 </TD> <TD align="right"> 0.20 </TD> <TD> setosa </TD> </TR>
<TR> <TD align="right"> 4.40 </TD> <TD align="right"> 2.90 </TD> <TD align="right"> 1.40 </TD> <TD align="right"> 0.20 </TD> <TD> setosa </TD> </TR>
<TR> <TD align="right"> 4.90 </TD> <TD align="right"> 3.10 </TD> <TD align="right"> 1.50 </TD> <TD align="right"> 0.10 </TD> <TD> setosa </TD> </TR>
</TABLE>

this class or id can be used in the ccs file, this can be useful if you create multiple tables for the html page


In addition you can use the print.results=FALSE argument to catch the character vector and use functions from the stringr package e.g. str_replace(), str_replace_all() to add classes, id's or inline css to other places in the table e.g. the <TD> tag

like image 115
tertra Avatar answered Dec 31 '22 07:12

tertra