【发布时间】:2013-08-16 19:14:05
【问题描述】:
我正在尝试使用 xtable 和 knitr 创建一个包含插入摘要行的表格。我想让这些插入的线条有不同的颜色。使用 add.to.row 选项,我设法插入行 或 更改颜色,但不能同时更改两者。
我在 xtable 中而不是提前插入行的原因是,由于原始数据集中的长度,我希望“总”行分布在两列中。因此,所需的解决方案将在摘要行中同时包含多列单元格和不同颜色的背景。 我的 Latex 学习曲线刚刚起步,我们将不胜感激。
这是一个例子,包括一个假数据集:
\usepackage{booktabs}
\usepackage{colortbl, xcolor}
\begin{document}
<<try, echo = FALSE, eval = TRUE, results = 'asis'>>=
library(xtable)
dat <- data.frame(type = c(rep("a", 5), rep("b", 5)), a = c(1:5, 1:5), b = 1:10, c = 21:30)
temp <- ddply(dat, .(type), summarize, SumB = sum(b))
rws <- which(dat$a == 5)
col <- rep("\\rowcolor[gray]{0.95}", length(rws)) ## colour definition prepared, but not used
#Making the command for inserting summary rows
temp$insert <- ""
for(i in 1:nrow(temp)){
temp[i,]$insert <- sprintf("\\multicolumn{3}{l}{Total %s} &
\\multicolumn{1}{c}{%d} \\\\ ", temp[i,]$type, temp[i,]$SumB)
}
print(xtable(dat, align = "llccc"),
include.rownames=FALSE,
booktabs = TRUE,
sanitize.text.function=function(x){x},
add.to.row = list(pos = as.list(rws),
command = paste(temp$insert, sep = ",")))
@
\end{document}
【问题讨论】: