sweave,xtable,longtable和交替的行颜色... add.to.row有问题 [英] sweave, xtable, longtable and alternating row colors...problems with `add.to.row`

查看:150
本文介绍了sweave,xtable,longtable和交替的行颜色... add.to.row有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此现有问题介绍了一种通过对来自乳胶表的输出进行后处理来替换乳胶表中行颜色的方法print.xtable(),但我认为可以通过使用print.xtable()add.to.row参数来实现相同的目的,如 stats所述. stackexchange ,无需进行后期处理,这对于Sweave来说很不错.该答案涉及为某一特定行的背景着色,但我认为它可以扩展为为所有奇数行着色.

This existing question covers a way to alternate row colors in a latex table by post-processing the output from print.xtable(), but I think it's possible to achieve the same thing by using the add.to.row argument of print.xtable() as described on stats.stackexchange, avoiding the need for post-processing, which is nice with Sweave. That answer deals with coloring the background of one specific row, but I think it can be extended to coloring all the odd rows.

我遇到的问题与add.to.row参数有关,使列表pos的长度等于字符向量command的长度. print.xtable()的帮助文件描述:

The problem I'm running into has to do with the add.to.row argument, making the length of list pos equal the length of character vector command. The help file for print.xtable() describes:

add.to.row:两个组件的列表.第一个组件(应 被称为"pos")是包含行的位置的列表 最后应该添加哪些额外的命令,第二个 组件(应称为命令")是一个字符 与第一个分量的长度相同的向量 包含应在末尾添加的命令 指定的行.默认值为``NULL'',即不添加 命令.

add.to.row: a list of two components. The first component (which should be called 'pos') is a list contains the position of rows on which extra commands should be added at the end, The second component (which should be called 'command') is a character vector of the same length of the first component which contains the command that should be added at the end of the specified rows. Default value is 'NULL', i.e. do not add commands.

在使用longtable环境时,可以使用此add.to.row参数定义应在每页上打印的表的页眉"行,如下所示:

when using the longtable environment, you can use this add.to.row argument to define the "header" rows of your table that should be printed on every page, like so:

library(xtable)
my.df=data.frame(a=c(1:10),b=letters[1:10])
print(xtable(my.data.frame,caption="My Table"),
      tabular.environment="longtable",
      floating=FALSE,
      hline.after=c(-1,nrow(my.data.frame)),
      add.to.row=list(pos=list(0),command="\\hline \\endhead ")

我需要保留此功能,并添加其他功能,每隔一行应获取命令\\rowcolor[gray]{0.8}

I need to keep this functionality, and add the additional functionality that every other row should get the command \\rowcolor[gray]{0.8}

听起来很简单. pos应该类似于list=(0,1,3,5,7,9),而command应该类似于c("\\hline \\endhead ","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}")

Sounds simple enough. pos should be something like list=(0,1,3,5,7,9) and command should be something like c("\\hline \\endhead ","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}")

当然,我想利用一些内置函数来构建"\\rowcolor[gray]{0.8}"的奇数行序列和重复行,所以我想到了:

Of course, I want to take advantage of some built in functions to build the odd-row sequence and the repetition of "\\rowcolor[gray]{0.8}", so I thought of:

pos=list(0,seq(from=1,to=nrow(my.df),by=2))

command=c("\\hline \\endhead ",
          rep("\\rowcolor[gray]{0.8}",length(seq(from=1,to=nrow(my.df),by=2))))

我的问题是上面的pos列表的计算结果为:

my problem is that the pos list above evaluates to:

> pos
[[1]]
[1] 0

[[2]]
[1] 1 3 5 7 9

长度为2 ...在这种情况下,长度必须为6.

which has length 2...it needs to have length 6 in this case.

推荐答案

诀窍是将列表弄平. 可能有一个更漂亮的方法,下面的方法可以解决问题.

The trick is to flatten out the list. There may be a prettier way, the following does the trick.

pos=list(as.list(c(0,seq(from=1,to=nrow(my.df),by=2))))[[1]]

整个包是:

library(xtable)
my.df=data.frame(a=c(1:10),b=letters[1:10])

print(xtable(my.df,caption="My Table"),
      tabular.environment="longtable",
      floating=FALSE,
      hline.after=c(-1,nrow(my.df)),
      add.to.row=list(
      pos=list(as.list(c(0,seq(from=1,to=nrow(my.df),by=2))))[[1]],
      command=c("\\hline \\endhead ",
      rep("\\rowcolor[gray]{0.8}",length(seq(from=1,to=nrow(my.df),by=2)))))
      )

产生

% latex table generated in R 2.14.2 by xtable 1.7-0 package
% Thu Jan 31 12:52:55 2013
\begin{longtable}{rrl}
  \hline
 & a & b \\ 
  \hline \endhead 1 &   1 & a \\ 
   \rowcolor[gray]{0.8}2 &   2 & b \\ 
  3 &   3 & c \\ 
   \rowcolor[gray]{0.8}4 &   4 & d \\ 
  5 &   5 & e \\ 
   \rowcolor[gray]{0.8}6 &   6 & f \\ 
  7 &   7 & g \\ 
   \rowcolor[gray]{0.8}8 &   8 & h \\ 
  9 &   9 & i \\ 
   \rowcolor[gray]{0.8}10 &  10 & j \\ 
   \hline
\hline
\caption{My Table}
\end{longtable}

乳胶的格式有些丑陋,并且由于\rowcolor应该位于行的 处,所以即使我们指定了1,3,5,9,我们也会在2处着色输出的4,6,8.

the formatting of the latex is a little ugly, and since \rowcolor is supposed to come before the row, even though we specified 1,3,5,9 we get coloring on 2,4,6,8 of the output.

这篇关于sweave,xtable,longtable和交替的行颜色... add.to.row有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆