问题
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!
回答1:
The idea is to :
- Create a css where you format "stylize" your table using some css features
- Create a html table using
print.xtable
- 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;
}
回答2:
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
来源:https://stackoverflow.com/questions/20200082/formatting-html-table-in-r