tags:

views:

60

answers:

2

Hello,

my.mat <- cbind(1:5, rnorm(5), 6:10, rnorm(5))
colnames(my.mat) <- c("Turn", "Draw","Turn", "Draw")
print(xtable(my.mat))

yields

\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}

I want to filter out the \begin{table} and \end{table} lines. I can do this using gsub, but how to I get the results of print(xtable(... into a variable?

Thanks for the help Stack Overflow R community!


EDIT - Making progress

In my .Rnw, I have

\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}

which leads to

\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}

which is so close. How do I get R to print the right way?

+1  A: 

This works for me:

x <- print(xtable(my.mat))
x <- gsub("\\\\begin\\{tabular\\}\\{rrrrr\\}", "", x)
Shane
I don't think that that works, because when you call print(xtable(my.mat)), it also prints the current table. Also, when I print the x object, I get[1] "% latex table generated in R 2.9.2 by xtable 1.5-5 ....it is the first entry in a vector. That won't print in LaTeX. Thanks thoug.
stevejb
From the help: "The function also (invisibly) returns a character vector of the results (which can be helpful for post-processing)." `x` as Shane suggests contains exactly the output of the print command.
Jonathan Chang
I think that the output from that will be latex. It's just stored in one character vector. You can suppress the print using `sink` or something like this: `capture.output(x <- print(xtable(my.mat)), file='NUL')`.
Shane
Thanks for the advice. I am pretty close, but haven't got it quite yet. I updated my post to show current status.
stevejb
+3  A: 

What about cat(x,sep='\n') instead of print(x)

wkmor1
That is exactly what I want. Thank you wkmor1
stevejb