在生成带有 longtable 选项的 xtable 时,有没有办法重复顶行/设置标题?
例如,如果我有
tableSb <- xtable(df, caption="A Very Long Table", label="ALongTable")
print(tableSb, include.rownames=TRUE, tabular.environment="longtable", floating=FALSE)
这很好用,但是当表格翻转到新页面时,标题不会重复。有什么建议么 ?
为了实现这一点,您需要使用函数的add.to.row选项print(运行?print.xtable以获取更多信息)。
试试这个(改编自R-Forge 上的帖子)
addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- c(0)
addtorow$command <- c(paste(
"\\hline \n",
"\\endhead \n",
"\\hline \n",
"{\\footnotesize Continued on next page} \n",
"\\endfoot \n",
"\\endlastfoot \n",
sep=""))
x.big <- xtable(
x,
label = "tabbig",
caption = "Example of longtable spanning several pages")
print(
x.big,
tabular.environment = "longtable",
floating = FALSE,
include.rownames = FALSE, # because addtorow will substitute the default row names
add.to.row = addtorow, # this is where you actually make the substitution
hline.after=c(-1)) # because addtorow will substitute the default hline for the first row
这是一个有点笨拙的解决方案,但至少它会为您提供大量的定制。
查看 print.xtable 的代码,它所做的唯一考虑tabular.environment="longtable"是
floating=TRUE它不会发出特定于在后续页面上重复标题的代码。latex在Hmisc包裹中检查。我知道它也支持长表,但我不记得它是否正确重复标题。