4

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mayou
  • 8,498
  • 16
  • 59
  • 98
  • 4
    What is the point of downvoting a post without including a comment explaining why? – Mayou Nov 25 '13 at 18:26
  • This answer [here](http://stackoverflow.com/questions/15403903/create-tables-with-conditional-formatting-with-rmarkdown-knitr/15405068#15405068) should help you. – agstudy Nov 25 '13 at 18:30
  • @agstudy Thank you very much. I am very beginner at HTML but this should help me understand. Thanks. – Mayou Nov 25 '13 at 18:37
  • I think it question of styling here (css) not HTML. Th HTML is created by print.xtable , you should decorate it with some css classes. – agstudy Nov 25 '13 at 18:39
  • I understand, thank you. Once the css file is created, I am not sure how to use it to format a table that I created using `xtable()`. I couldn't follow all the steps described in the link you provided, as I still don't understand much about the package `xtable`. Could you please help me with that? Thanks – Mayou Nov 25 '13 at 18:40
  • For an alternative to xtable, see htmlTable which can create almost any html-table, and has a css.cell argument, which takes a matrix with your css for each cell: https://cran.r-project.org/web/packages/htmlTable/vignettes/general.html – Rasmus Larsen Jun 26 '18 at 09:47

2 Answers2

9

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

tertra
  • 165
  • 4
  • 11
3

The idea is to :

  1. Create a css where you format "stylize" your table using some css features
  2. Create a html table using print.xtable
  3. Create a file including a link to the css file and the created html table

So here the code creating the "res.html" file:

## a dummy data.frame used as an example
library(xtable)
n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0))
## the html header  
## here I am using a link to mystyle.css 
html.head <- paste("<head>" ,
             '<link rel="stylesheet" type="text/css" href="mystyle.css"/>',
             "</head>",sep='\n')
## the html body
html.table <- paste(print(xtable(n),type='html','res.html'), 
                    collapse = "\n")
html.body <- paste("<body>", html.table,"</body>")
## the html file
write(paste(html.head,html.body,sep='\n'),"res.html")

the syle sheet file(mystyle.css) can contain be something like this :

table {
   max-width: 95%;
   border: 1px solid #ccc;
}

th {
  background-color: #000000; // background for table header 
  color: #ffffff;
}

td
{
   text-align:right;        // justify column
   background-color: #FF0000;
}
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Thank you so much for your thorough explanation. However, for some reason, it didn't work for me. Should I save the stylesheet in a specific directory, or can I save it anywhere and refer to it using its full path? – Mayou Nov 25 '13 at 19:53
  • Also, if I am writing other things along with the table, I am assuming that all the code should be included in `html.body`? – Mayou Nov 25 '13 at 19:57
  • @Mariam here href="mystyle.css" means that the style file in the same location as the html file. It is a relative path.( relative to the html location). For example, href="css/mystyle.css" means that the style is stored in css directory. – agstudy Nov 25 '13 at 20:35