[R] latex table and R codes

Charles C. Berry cberry at tajo.ucsd.edu
Wed Nov 16 02:24:48 CET 2005


On Tue, 15 Nov 2005, Mostafa Ghaderi wrote:

> Dear R-help assistance;
> may you help me regrding to following inquiry!?
> you know what, i have generated three tables by xtable R function, right now 
> i am trying to make a single table by putting these tables togethere; actully 
> i am going to come upt with *.tex (latex) file. because i have more extera 
> non-R material, i am using  Sweave to read R instructions, and finally i hope 
> to end up with a single table containing all three tables in one table, would 
> you let me know how i should go for it!?( i mean combining Sweave arguments 
> and Latex expressions)
> thanks for your time;
> Mostafa

There are probably lots of ways to combine xtable()'d R objects to form a 
pleasing result using Sweave.

Some suggestions:

---
in R

> RSiteSearch("Sweave xtable")

will lead to many useful threads (but none specifically addressing your 
question, I think) about formatting tables.

---

capture.output() can be used in a code chunk to save the results of
print.xtable() calls and then later combine them -- see the example below.

see ?capture.output, too.
---

Explicit calls to print.xtable() are helpful in tuning or 
suppressing floats -- also in the example  --- and other things.

see ?print.table and print a copy of the function to see what it is doing 
in more detail.

---

A private copy of print.table() may help you add useful features.

---

You can use Sweave to make files to be included in other *.tex documents, 
but it also works the other way round.

-----

Here is an Sweave example of some of these tactics:

\documentclass{article}

\begin{document}

Start by putting together a (tabular) table:

@
<<make-tab1,echo=T,results=hide>>=

library(xtable)

tab1 <- capture.output(print.xtable(xtable(diag(2)),float=F))

@ %def

And now another table

@
<<make-tab2,echo=T,results=hide>>=

tab2 <- capture.output(print.xtable(xtable(diag(2)+2),float=F))

@ %def

Now print out a super table:


@
<<super-tab,echo=T,eval=F,results=verbatim>>=

cat( tab1, "  & upper right blank  \\\\ ", sep='\n' )

cat( "lower left blank & ", tab2, "\\\\" ,sep='\n' )

@ %def


\begin{tabular}[c]{|c|c|}

@
<<eval-super-tab,echo=F,results=tex>>=

<<super-tab>>

@ %def


\end{tabular}

\end{document}


--
Charles C. Berry                        (858) 534-2098
                                          Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	         UC San Diego
http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0717




More information about the R-help mailing list