该xtable
包可以生成 HTML 输出以及 LaTeX 输出。
# install.packages("xtable")
library("xtable")
sample_table <- mtcars[1:3,1:3]
print(xtable(sample_table), type="html", file="example.html")
在文件中给出example.html
:
<!-- html table generated in R 3.0.1 by xtable 1.7-1 package -->
<!-- Fri Jul 19 09:08:15 2013 -->
<TABLE border=1>
<TR> <TH> </TH> <TH> mpg </TH> <TH> cyl </TH> <TH> disp </TH> </TR>
<TR> <TD align="right"> Mazda RX4 </TD> <TD align="right"> 21.00 </TD> <TD align="right"> 6.00 </TD> <TD align="right"> 160.00 </TD> </TR>
<TR> <TD align="right"> Mazda RX4 Wag </TD> <TD align="right"> 21.00 </TD> <TD align="right"> 6.00 </TD> <TD align="right"> 160.00 </TD> </TR>
<TR> <TD align="right"> Datsun 710 </TD> <TD align="right"> 22.80 </TD> <TD align="right"> 4.00 </TD> <TD align="right"> 108.00 </TD> </TR>
</TABLE>
这可以通过更多选项来进一步简化xtable
和print.xtable
:
print(xtable(sample_table, align="llll"),
type="html", html.table.attributes="")
给
<!-- html table generated in R 3.0.1 by xtable 1.7-1 package -->
<!-- Fri Jul 19 09:13:33 2013 -->
<TABLE >
<TR> <TH> </TH> <TH> mpg </TH> <TH> cyl </TH> <TH> disp </TH> </TR>
<TR> <TD> Mazda RX4 </TD> <TD> 21.00 </TD> <TD> 6.00 </TD> <TD> 160.00 </TD> </TR>
<TR> <TD> Mazda RX4 Wag </TD> <TD> 21.00 </TD> <TD> 6.00 </TD> <TD> 160.00 </TD> </TR>
<TR> <TD> Datsun 710 </TD> <TD> 22.80 </TD> <TD> 4.00 </TD> <TD> 108.00 </TD> </TR>
</TABLE>
(可以将其定向到带有file
参数 to的文件,print.xtable
如上例所示。)