将 xtable 输出拆分为子表

发布于 2024-12-04 18:27:35 字数 907 浏览 0 评论 0原文

有一个关于在有多列时将 xtable 与 Sweave 一起使用的问题。我正在处理的表格大约有 25 列和 5 行。确切的列数未知,因为它是动态的。

当我跑步时说,

table1 <- 表 (df$someField)

我得到一个基本上超过页面长度的表。

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

如果对此执行 xtable,并通过 Sweave 运行它,

xtable(table1, caption="some table")

它就会溢出。

我正在寻找类似的东西,

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

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

带有 \hline 等标记。基本上,将 xtable 分成多个部分,例如每个“子表”5 列。

我也在批处理作业中运行它,因此我无法对单个文件进行更改,无论它必须能够通过在 Rnw 文件上运行 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人迟暮 2024-12-11 18:27:35

以下是来自 taRifx 包中的 ?latex.table.by 的示例。您可以在 LaTeX 中使用 longtable 编写类似的内容,并使用 latex.table.by 代码作为原型。

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

无论如何,LaTeX 中的 longtable 包是关键。

编辑:看来您的列太多,而不是行太多。在这种情况下,首先尝试仅美化该页面。

在标题中:

\usepackage{lscape}

在桌子周围:

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

或者仅使用 sidewaystable

如果您的表格太宽而无法容纳一页,请尝试 supertabular 包,从描述来看,它可能会根据宽度处理跨多个页面的情况(但我从未使用过它,所以可以不确定)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文