Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print.xtable with multi-line header?

Tags:

r

knitr

xtable

I've been trying to create a table with a header that contains a like break. I am not too ambitious here and do not really care whether I just break extremely long strings or just have a two-line header in general.

dat <- matrix(round(rnorm(9, 20, 10)), 3, 3)
colnames(dat) <- c("some very long colname","short","another toooooooolong colname")
require(xtable)
m <- xtable(dat)

print(m,                  
              floating=FALSE, 
              hline.after=NULL,                  
              size="\\footnotesize",
              tabular.environment="tabular",
              add.to.row=list(pos=list(-1,0,nrow(m),0
                                       ), 
              command=c('\\toprule ',
                        '\\midrule ',
                        '\\bottomrule',
                        '\\\\ \\rowcolor[gray]{.9}'
                        )
                              )
              )

Is there a way to line break the header, because my table is not fitting on the page anymore, though I use landscape already. Some sanitize.rownames based trick? Fiddled around with \shortstack but could not get it to go...

like image 453
Matt Bannert Avatar asked Jan 28 '13 19:01

Matt Bannert


Video Answer


1 Answers

(Writing my comment up into a proper answer)

In the call to xtable, you can specify various alignment arguments, one of which is a parbox of a fixed width in which text will be wrapped. That is specified with p{1in} where the value specified within the braces is how wide the box (column) will be. You have to manually specify this (rather than have it adapt to the available space), but it does give you one option for wrapping text.

like image 145
Brian Diggs Avatar answered Oct 13 '22 01:10

Brian Diggs