12

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 ?

xbsd
  • 2,438
  • 4
  • 25
  • 35
  • 1
    This has already been answered (twice): http://stackoverflow.com/questions/4279956/header-on-each-page-of-big-table-of-xtable http://stackoverflow.com/questions/7423423/split-xtable-ouput-into-sub-tables – IRTFM Sep 16 '11 at 21:22
  • This question doesn't seem to be getting any more answers, has any one of those below solved your issue? If so, you can select it as accepted, which will improve your reputation on SO. – Waldir Leoncio Oct 26 '13 at 14:37

2 Answers2

27

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 a post on 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

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

Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
  • 1
    Is the floating=FALSE statement in print.xtable absolutely essential? I'd like to have an xtable capable of rolling over into a new page AND being in a sidewaystable environment so it is in landscape orientation? – W7GVR Jun 24 '15 at 21:30
  • @gvrocha: If I recall correctly, you're right. `floating` can be set to `TRUE` without a problem. – Waldir Leoncio Jun 25 '15 at 11:14
  • At least with xtable 1.7-4, `floating` is set to `FALSE` with a warning automatically. Therefore, there seems to be no way of encapsulating `longtable` in another environment (see also [this feature request](https://r-forge.r-project.org/tracker/index.php?func=detail&aid=2249&group_id=1228&atid=4864)). – SaschaH Aug 17 '15 at 19:13
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.

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
  • Specifically, you'd be looking for a way to replace the line break (\\) following the column headers with \endhead. I don't see that option, so one might have to do it manually. – joran Sep 16 '11 at 21:02