Repeat headers when using xtable with longtable option

試著忘記壹切 提交于 2019-12-04 18:28:01

问题


Is there a way to repeat the top row / set headers when generating an xtable with a longtable option ?

For eg., if I have

tableSb <- xtable(df, caption="A Very Long Table", label="ALongTable")
print(tableSb, include.rownames=TRUE, tabular.environment="longtable", floating=FALSE)

This works fine, but when the tables roll over into a new page the headers are not repeated. Any suggestions ?


回答1:


In order to accomplish this, you'll need to use the add.to.row option of the print function (run ?print.xtable for more information).

Try this (adapted from https://r-forge.r-project.org/tracker/?func=detail&atid=4864&aid=1627&group_id=1228)

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

It's a bit clumsy of a solution, but at least it'll provide you with plenty of customization.




回答2:


Looking at the code for print.xtable, the only considerations it makes when tabular.environment="longtable" are

  • Emitting a warning if you also set floating=TRUE
  • Putting the caption in the right place for a longtable (top or bottom)

It does not emit the code specific for repeating the headers on subsequent pages. Check out latex in the Hmisc package. I know it has support for longtables as well, but I don't recall if it repeats headers correctly.



来源:https://stackoverflow.com/questions/7450141/repeat-headers-when-using-xtable-with-longtable-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!