2

In the xtable output, if I want some rows to have nothing in them I will put NA's in the elements of the matrix that correspond to the row that I want skipped.

However this will lead to xtable output of something like & & & & & & & & \\. What I want to know is how do I make it so it's ONLY \\ for that row that I wish to skip.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jase
  • 1,025
  • 1
  • 9
  • 34
  • I don't think you can trick xtable into doing that. As a matter of interest: Why? – vaettchen Oct 07 '12 at 06:04
  • Because I generate a `HUGE` number of tables in my work. My method of doing so is in notepad++ where I have the rownames ready at the top of my .txt, and at the bottom of the .txt I copy my ~80 rows of xtable output, hold down `alt` (highlighting the table), cut (ctrl+X), go to the top of the .txt, left click on the right of the top-most rowname, spacebar 20 times, then CTRL+V. This way I can construct a very large table automatically with all the multicolumns in place. Except the `&&&&` get in the way because `\multicolumn` takes up too many columns. – Jase Oct 07 '12 at 06:29
  • If you don't know how to do it, then how can I edit the `xtable()` function? If I can edit the function then at the very end I can do a grep() search for `& & & &` and replace the rows (maybe). – Jase Oct 07 '12 at 06:30
  • 1
    You find the source code [here](http://cran.r-project.org/web/packages/xtable/index.html). You could remove those empty rows entirely in `R` before `print( xtable( x ) )` but you _need_ the double backslash? – vaettchen Oct 07 '12 at 06:56
  • No, I don't. At each multicolumn spot I can put \\ after it in the template rownamnes. – Jase Oct 07 '12 at 07:13

2 Answers2

5

Still not sure whether I understand right but I give it a shot.

The most important assumption I'm making here is that your multicolumn rows in that template are always at the same spot. You use R to create a matrix or dataframe with no (useful) data in these rows.

For the purpose of this example, the multicolumn rows are 15, 30 and 60 of a dataframe of 80 rows, corresponding to your template of 80 rows.

What you could do: In R, eliminate those NA rows entirely, so that our dataframe now has only 77 rows.

You insert empty rows for your template via xtable with

> addtorow <- list()
> addtorow$pos <- list()
> addtorow$pos[[1]] <- c(14,29,59)
> addtorow$command <- "\\\\ \n"
> print( xtable( o ), add.to.row = addtorow, include.rownames=FALSE )

That should give you empty lines, no & but with \\ and thus be what you are looking for?

vaettchen
  • 7,299
  • 22
  • 41
0

If you want more flexibility you can build the table using a combination of xtable and writeLines. Let xtable write the first part to file (print.xtable, file = "bla.txt"), then write \\ with writeLines, and continue with xtable. To prevent writing the header twice, set only.contents to TRUE. Do mind to set append equal to TRUE in your call to print.xtable and writeLines.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149