将xtable中的html代码保存在字符串中

时间:2014-04-22 06:24:20

标签: html r xtable

我正在使用http://myowelt.blogspot.com/2008/04/beautiful-correlation-tables-in-r.html生成关联表 我想在html

中打印输出
print(xtable(corstarsl(airquality)),type="html")
<!-- html table generated in R 3.0.2 by xtable 1.7-1 package -->
<!-- Tue Apr 22 11:45:02 2014 -->
<TABLE border=1>
<TR> <TH>  </TH> <TH> Ozone </TH> <TH> Solar.R </TH> <TH> Wind </TH> <TH> Temp </TH> <TH> Month    </TH>  </TR>
  <TR> <TD align="right"> Ozone </TD> <TD>  </TD> <TD>  </TD> <TD>  </TD> <TD>  </TD> <TD>  </TD>    </TR>
  <TR> <TD align="right"> Solar.R </TD> <TD>  0.35*** </TD> <TD>  </TD> <TD>  </TD> <TD>  </TD> <TD>  </TD> </TR>
  <TR> <TD align="right"> Wind </TD> <TD> -0.60*** </TD> <TD> -0.06  </TD> <TD>  </TD> <TD>  </TD> <TD>  </TD> </TR>
  <TR> <TD align="right"> Temp </TD> <TD>  0.70*** </TD> <TD>  0.28*** </TD> <TD> -0.46*** </TD> <TD>  </TD> <TD>  </TD> </TR>
  <TR> <TD align="right"> Month </TD> <TD>  0.16+  </TD> <TD> -0.08  </TD> <TD> -0.18*  </TD> <TD>  0.42*** </TD> <TD>  </TD> </TR>
  <TR> <TD align="right"> Day </TD> <TD> -0.01  </TD> <TD> -0.15+  </TD> <TD>  0.03  </TD> <TD> -0.13  </TD> <TD> -0.01  </TD> </TR>
   </TABLE>

我想将html代码保存在字符串中,以便稍后生成html输出。我尝试了capture output但是没有保存代码。

1 个答案:

答案 0 :(得分:1)

results = FALSE`参数也应该这样做。

示例:

x<- print(xtable(head(iris, 10)),type="html", print.results=FALSE)

唯一的缺点是print(x)看起来很难看:

"<!-- html table generated in R 3.0.1 by xtable 1.7-3 package -->\n<!-- Tue Jul 15    
 09:15:59 2014 -->\n<TABLE class='table' style=max-width:50%;>\n<TR> <TH> Sepal.Length 
 </TH> <TH> Sepal.Width </TH> <TH> Petal.Length </TH> <TH> Petal.Width </TH> <TH> 
 Species </TH>  </TR>\n  <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>\n  <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>\n  <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>\n  
 <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>\n  
 <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>\n  
 <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>\n   
 <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>\n  <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>\n  <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>\n  <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>\n   </TABLE>\n"

改为使用cat(x)

<!-- html table generated in R 3.0.1 by xtable 1.7-3 package -->
<!-- Tue Jul 15 09:15:59 2014 -->
<TABLE class='table' style=max-width:50%;>
<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>