Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split xtable ouput into sub tables

Tags:

r

sweave

Have a question on using xtable with Sweave when there are multiple columns. A table I am working on has about 25 columns and 5 rows. The exact number of columns is not known as that is dynamic.

When I run say,

table1 <- table (df$someField)

I get a table that essentially exceeds the page length.

       ColA    ColB    ColC
---------------------------
RowA   1       2       3   ......
RowB   3       4       6   ......

If a do a xtable on this, and run it through Sweave,

xtable(table1, caption="some table")

it overflows.

What I am looking for is something like,

       ColA    ColB    ColC
---------------------------
RowA   1       2       3 
RowB   3       4       6 

       ColD    ColE    ColF 
---------------------------
RowA   11       9       34 
RowB   36       8       65  

with the \hline etc markups. Basically, split the xtable into parts by say 5 columns per "sub-table".

I am also running this in a batch job, so I won't be able to make changes to individual files, whatever the solution it has to be able to be generated by running Sweave on the Rnw file.

Thanks in advance,

Regards,

  • Raj.
like image 479
xbsd Avatar asked Sep 14 '11 21:09

xbsd


1 Answers

Here's an example of this from ?latex.table.by in the taRifx package. You can brew something similar using longtable in LaTeX and use the latex.table.by code as a prototype.

my.test.df <- data.frame(grp=rep(c("A","B"),10),data=runif(20))
library(xtable)
latex.table.by(my.test.df)
#   print(latex.table.by(test.df), include.rownames = FALSE, include.colnames = TRUE, sanitize.text.function = force)
#   then add \usepackage{multirow} to the preamble of your LaTeX document
#   for longtable support, add ,tabular.environment='longtable' to the print command (plus add in ,floating=FALSE), then \usepackage{longtable} to the LaTeX preamble

Regardless, the longtable package in LaTeX is the key.

Edit: It appears you have too many columns not too many rows. In that case, first try landscaping just that page.

In the header:

\usepackage{lscape}

Around your table:

\begin{landscape}
...
\end{landscape}

Or just use sidewaystable.

If your table is too wide to fit in one page, try the supertabular package, which from the description sounds like it might handle breaking over multiple pages based on width (but I've never used it so can't be sure).

like image 94
Ari B. Friedman Avatar answered Nov 15 '22 22:11

Ari B. Friedman