1

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?

stevejb
  • 2,414
  • 5
  • 26
  • 41
  • Two things would help: can you include the `gsub()` code you're trying to use now? And what exactly do you mean, get the results of print(xtable(... into a variable? Do you mean: into a structure that you could pass to gsub? – Matt Parker May 17 '10 at 23:11
  • >Do you mean: into a structure that you could pass to gsub? – Matt Parker 23 mins ago Yes. I realize I can use Shane's method below, and I can suppress the output of print by sending the output to a text file, e.g. print(xtable(my.mat), file="tmp.R") but then when I print the x object, the latex code is stored as the first element of a vector, so I print it and I get a [1] "...the table..." where I just want to print ...the table... – stevejb May 17 '10 at 23:44

2 Answers2

4

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

wkmor1
  • 7,226
  • 3
  • 31
  • 23
1

This works for me:

x <- print(xtable(my.mat))
x <- gsub("\\\\begin\\{tabular\\}\\{rrrrr\\}", "", x)
Shane
  • 98,550
  • 35
  • 224
  • 217
  • 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 May 17 '10 at 23:41
  • 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 May 17 '10 at 23:58
  • 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 May 18 '10 at 00:04
  • Thanks for the advice. I am pretty close, but haven't got it quite yet. I updated my post to show current status. – stevejb May 18 '10 at 00:24