Split xtable ouput into sub tables

十年热恋 提交于 2019-12-18 22:22:09

问题


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.

回答1:


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).



来源:https://stackoverflow.com/questions/7423423/split-xtable-ouput-into-sub-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!