my.mat< - cbind(1:5,rnorm(5),6:10,rnorm(5)) colnames(my.mat)< - c(“Turn”,“Draw”,“Turn”,“Draw”) 打印(xtable(my.mat))
产量
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
\hline
& Turn & Draw & Turn & Draw \\
\hline
1 & 1.00 & -0.72 & 6.00 & 0.91 \\
2 & 2.00 & 0.57 & 7.00 & 0.56 \\
3 & 3.00 & 1.08 & 8.00 & 0.55 \\
4 & 4.00 & 0.95 & 9.00 & 0.46 \\
5 & 5.00 & 1.94 & 10.00 & 1.06 \\
\hline
\end{tabular}
\end{center}
\end{table}
我想过滤掉\ begin {table}和\ end {table}行。我可以使用gsub做到这一点,但是如何获得print的结果(xtable(...变成一个变量?
感谢Stack Overflow R社区的帮助!
编辑 - 取得进展
在我的.Rnw中,我有
\begin{tablehere}
{
<<echo=false,results=tex>>=
library(xtable)
my.mat <- cbind(1:5, rnorm(5), 6:10, rnorm(5))
colnames(my.mat) <- c("Turn", "Draw","Turn", "Draw")
#print(xtable(my.mat))
x <- capture.output(print(xtable(my.mat)))
x <- gsub("\\\\begin\\{tabular\\}.*", "", x, perl=TRUE)
x <- gsub("\\\\end\\{tabular\\}.*", "", x, perl=TRUE)
print(x)
@
}
\end{tablehere}
导致
\begin{tablehere}
{
[1] "% latex table generated in R 2.9.2 by xtable 1.5-5 package"
[2] "% Mon May 17 20:23:09 2010"
[3] "\\begin{table}[ht]"
[4] "\\begin{center}"
[5] ""
[6] " \\hline"
[7] " & Turn & Draw & Turn & Draw \\\\ "
[8] " \\hline"
[9] "1 & 1.00 & -1.76 & 6.00 & 0.70 \\\\ "
[10] " 2 & 2.00 & 1.58 & 7.00 & 2.57 \\\\ "
[11] " 3 & 3.00 & -1.80 & 8.00 & 0.47 \\\\ "
[12] " 4 & 4.00 & -2.25 & 9.00 & -0.63 \\\\ "
[13] " 5 & 5.00 & 1.99 & 10.00 & -0.35 \\\\ "
[14] " \\hline"
[15] ""
[16] "\\end{center}"
[17] "\\end{table}"
}
\end{tablehere}
这是如此接近。如何让R以正确的方式打印?
答案 0 :(得分:4)
cat(x,sep='\n')
而不是print(x)
答案 1 :(得分:1)
这对我有用:
x <- print(xtable(my.mat))
x <- gsub("\\\\begin\\{tabular\\}\\{rrrrr\\}", "", x)