Note that there are some explanatory texts on larger screens.

plurals
  1. POusing gsub to modify output of xtable command
    primarykey
    data
    text
    <p>my.mat &lt;- cbind(1:5, rnorm(5), 6:10, rnorm(5)) colnames(my.mat) &lt;- c("Turn", "Draw","Turn", "Draw") print(xtable(my.mat))</p> <p>yields </p> <pre><code>\begin{table}[ht] \begin{center} \begin{tabular}{rrrrr} \hline &amp; Turn &amp; Draw &amp; Turn &amp; Draw \\ \hline 1 &amp; 1.00 &amp; -0.72 &amp; 6.00 &amp; 0.91 \\ 2 &amp; 2.00 &amp; 0.57 &amp; 7.00 &amp; 0.56 \\ 3 &amp; 3.00 &amp; 1.08 &amp; 8.00 &amp; 0.55 \\ 4 &amp; 4.00 &amp; 0.95 &amp; 9.00 &amp; 0.46 \\ 5 &amp; 5.00 &amp; 1.94 &amp; 10.00 &amp; 1.06 \\ \hline \end{tabular} \end{center} \end{table} </code></pre> <p>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?</p> <p>Thanks for the help Stack Overflow R community!</p> <hr> <p><strong>EDIT - Making progress</strong></p> <p>In my .Rnw, I have</p> <pre><code>\begin{tablehere} { &lt;&lt;echo=false,results=tex&gt;&gt;= library(xtable) my.mat &lt;- cbind(1:5, rnorm(5), 6:10, rnorm(5)) colnames(my.mat) &lt;- c("Turn", "Draw","Turn", "Draw") #print(xtable(my.mat)) x &lt;- capture.output(print(xtable(my.mat))) x &lt;- gsub("\\\\begin\\{tabular\\}.*", "", x, perl=TRUE) x &lt;- gsub("\\\\end\\{tabular\\}.*", "", x, perl=TRUE) print(x) @ } \end{tablehere} </code></pre> <p>which leads to</p> <pre><code>\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] " &amp; Turn &amp; Draw &amp; Turn &amp; Draw \\\\ " [8] " \\hline" [9] "1 &amp; 1.00 &amp; -1.76 &amp; 6.00 &amp; 0.70 \\\\ " [10] " 2 &amp; 2.00 &amp; 1.58 &amp; 7.00 &amp; 2.57 \\\\ " [11] " 3 &amp; 3.00 &amp; -1.80 &amp; 8.00 &amp; 0.47 \\\\ " [12] " 4 &amp; 4.00 &amp; -2.25 &amp; 9.00 &amp; -0.63 \\\\ " [13] " 5 &amp; 5.00 &amp; 1.99 &amp; 10.00 &amp; -0.35 \\\\ " [14] " \\hline" [15] "" [16] "\\end{center}" [17] "\\end{table}" } \end{tablehere} </code></pre> <p>which is so close. How do I get R to print the right way?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload