我已經使用了以下兩個答案向xtable中的交替行添加顏色或向長表添加頁腳,但是我需要弄清楚如何執行都。將兩條命令添加到xtable中的add.to.row參數
(1)How to only show table caption once in "list of table" for a table split onto multiple pages和
(2) R, knitr, xtable, alternating row colors
是否有同時使用這兩種方式?
我已經使用了以下兩個答案向xtable中的交替行添加顏色或向長表添加頁腳,但是我需要弄清楚如何執行都。將兩條命令添加到xtable中的add.to.row參數
(1)How to only show table caption once in "list of table" for a table split onto multiple pages和
(2) R, knitr, xtable, alternating row colors
是否有同時使用這兩種方式?
從你列出的答案看來,你用LaTeX作爲輸出。您可以通過爲每個命令分配一個位置來組合兩個或多個add.to.row命令。命令必須是模式爲character
的矢量,並且位置必須在列表中。在下面,我創建一個清單addtorow <- list()
,然後分配職位:addtorow$pos <- as.list(c(rowIndexForFirstCommands, rowIndexForSecondCommands))
及其相應的命令addtorow$command <- as.vector(c(firstCommands, secondCommands),mode="character")
。這是一個最小的工作示例:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{longtable}
\usepackage[table]{xcolor}
\begin{document}
<<yoman,echo=FALSE,results='asis'>>=
library(xtable)
#define a data frame
mydf <- data.frame(id = make.unique(rep(letters, length.out = 100), sep=''),
var1 = rnorm(100),
var2 = runif(100),
var3=rexp(100),
var4=rpois(100,1.4))
#define row indexes to be highlighted (each two),
#and repeat rowcolor command correspondingly
rws <- seq(1, (nrow(mydf)), by = 2)
col <- rep("\\rowcolor[gray]{0.95}", length(rws))
#create a list that will receive the instructions
#for add.to.row and add the two instructions
addtorow <- list()
#assign a position argument to addtorow
#rws are the row indexes for the row to be colored,
#0 is the row index for longtable argument
addtorow$pos <- as.list(c(
rws, #positions for first commands(highlighting rows)
0 #position for second command (longtable argument)
))
#assign corresponding commands to addtorow
addtorow$command <- as.vector(c(col, #first command (highlighting rows)
paste("\\hline \n",
"\\endhead \n",
"\\hline \n",
"{\\footnotesize Continued on next page} \n",
"\\endfoot \n",
"\\endlastfoot \n",
sep="")), #second command (longtable)
mode="character")
print(xtable(mydf,
caption = "My caption "),
tabular.environment = "longtable",
floating = FALSE,
include.colnames = TRUE,
include.rownames = TRUE,
add.to.row = addtorow,
hline.after=c(-1), # addtorow substitute default hline for first row
caption.placement="top")
@
\end{document}