定义text / html文件的名称,例如R中data.frame的名称

时间:2015-06-19 16:57:28

标签: r

我有一个data.frame CLI_CAP_X_PROD_AG:

      FAMILIA_PRD NUM_CLI CAPITAL_SOL PART_CAP_PER ATRASADA ATR_PER
      A             536     2616925        33.62   467830   17.88
      B             151     1613035        20.72   268223   16.63

当我尝试使用data.frame的名称保存文件时出现错误:

      print(temp,type = "html", include.rownames = FALSE, file = paste(CLI_CAP_X_PROD_AG,"_tab.html", sep=""))

因为R读取了CLI_CAP_X_PROD_AG的元素。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您应该将xtables存储在命名列表中并遍历列表。演示:

library('xtable');
li <- list(t1=xtable(data.frame(a=letters[1:3],b=1:3)),t2=xtable(data.frame(a=letters[4:6],b=4:6)));
names(li);
## [1] "t1" "t2"
li;
## $t1
## % latex table generated in R 3.1.3 by xtable 1.7-4 package
## % Fri Jun 19 13:48:30 2015
## \begin{table}[ht]
## \centering
## \begin{tabular}{rlr}
##   \hline
##  & a & b \\
##   \hline
## 1 & a &   1 \\
##   2 & b &   2 \\
##   3 & c &   3 \\
##    \hline
## \end{tabular}
## \end{table}
##
## $t2
## % latex table generated in R 3.1.3 by xtable 1.7-4 package
## % Fri Jun 19 13:48:30 2015
## \begin{table}[ht]
## \centering
## \begin{tabular}{rlr}
##   \hline
##  & a & b \\
##   \hline
## 1 & d &   4 \\
##   2 & e &   5 \\
##   3 & f &   6 \\
##    \hline
## \end{tabular}
## \end{table}
##
for (n in names(li)) print(li[[n]],type='html',include.rownames=F,file=paste0(n,'_tab.html'));
file.show('t1_tab.html',pager='cat');
## <!-- html table generated in R 3.1.3 by xtable 1.7-4 package -->
## <!-- Fri Jun 19 13:48:34 2015 -->
## <table border=1>
## <tr> <th> a </th> <th> b </th>  </tr>
##   <tr> <td> a </td> <td align="right">   1 </td> </tr>
##   <tr> <td> b </td> <td align="right">   2 </td> </tr>
##   <tr> <td> c </td> <td align="right">   3 </td> </tr>
##    </table>
##
file.show('t2_tab.html',pager='cat');
## <!-- html table generated in R 3.1.3 by xtable 1.7-4 package -->
## <!-- Fri Jun 19 13:48:34 2015 -->
## <table border=1>
## <tr> <th> a </th> <th> b </th>  </tr>
##   <tr> <td> d </td> <td align="right">   4 </td> </tr>
##   <tr> <td> e </td> <td align="right">   5 </td> </tr>
##   <tr> <td> f </td> <td align="right">   6 </td> </tr>
##    </table>
##